niki/validator/benefactor/kind_box_req/validator.go

82 lines
1.7 KiB
Go

package userkindboxreqvalidator
import (
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
const (
MinKindBoxReq = 1
MaxKindBoxReq = 100
)
type Repository interface {
BenefactorExist(id uint) (bool, error)
KindBoxReqExist(id uint) (bool, error)
TypeExist(id uint) (bool, error)
KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error)
PendingStatus(id uint) (bool, error)
}
type Validator struct {
repo Repository
}
func New(repo Repository) Validator {
return Validator{repo: repo}
}
func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID := value.(uint)
_, err := v.repo.BenefactorExist(benefactorID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesTypeExist(value interface{}) error {
typeID := value.(uint)
_, err := v.repo.TypeExist(typeID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
kindBoxReqID := value.(uint)
_, err := v.repo.KindBoxReqExist(kindBoxReqID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error {
kbId := value.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbId)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
}
func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID := value.(uint)
_, err := v.repo.PendingStatus(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}