workshop/internal/models/bonuse.go
Dmitry Sirotkin 083c02116b init
2025-03-21 19:49:54 +03:00

39 lines
800 B
Go

package models
import (
u "gocommunity.ru/workshop/internal/utils"
"github.com/jinzhu/gorm"
)
type Bonuse struct {
gorm.Model
DiscountPercent uint16 `json:"discount_percent"`
ClientId uint `gorm:"column:client_id"`
}
func (bonuse *Bonuse) ValidateBonuse() (map[string]any, bool) {
if bonuse.ClientId <= 0 {
return u.Message(false, "value should be > 0"), false
}
if bonuse.DiscountPercent <= 0 || bonuse.DiscountPercent > 100 {
return u.Message(false, "value should be betwen 0 and 100!"), false
}
return u.Message(true, "success"), true
}
func (bonuse *Bonuse) CreateBonuse() map[string]interface{} {
if response, ok := bonuse.ValidateBonuse(); !ok {
return response
}
GetDB().Create(bonuse)
resp := u.Message(true, "success")
resp["bonuse"] = bonuse
return resp
}