niki/validator/benefactor/kind_box/validator.go

64 lines
1.4 KiB
Go
Raw Normal View History

package benefactorkindboxvalidator
import (
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type Repository interface {
BenefactorExist(id uint) (bool, error)
KindBoxExist(id uint) (bool, error)
2024-01-01 07:22:14 +00:00
KindBoxBelongToBenefactor(bID uint, kbID uint) (bool, error)
}
type Validator struct {
repo Repository
}
func New(repo Repository) Validator {
return Validator{repo: repo}
}
func (v Validator) doesBenefactorExist(value interface{}) error {
2024-01-01 07:22:14 +00:00
benefactorID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.BenefactorExist(benefactorID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesKindBoxExist(value interface{}) error {
2024-01-01 07:22:14 +00:00
kindBoxID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxExist(kindBoxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
return func(value interface{}) error {
2024-01-01 07:22:14 +00:00
kbID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
_, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
}