forked from ebhomengo/niki
1
0
Fork 0
niki/validator/admin/kind_box_req/validator.go

129 lines
2.9 KiB
Go
Raw Normal View History

package adminkindboxreqvalidator
import (
"errors"
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
const (
MinKindBoxReq = 1
MaxKindBoxReq = 100
)
type Repository interface {
// BenefactorExist(id int) (bool, error)
KindBoxRequestExist(id uint) (bool, error)
// TypeExist(id int) (bool, error)
// KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
// PendingStatus(id uint) (bool, error)
CheckKindBoxReqStatusForAccepting(id uint) error
2024-01-22 15:21:13 +00:00
CheckKindBoxReqStatusForRejecting(id uint) error
}
type Validator struct {
repo Repository
}
func New(repo Repository) Validator {
return Validator{repo: repo}
}
// func (v Validator) doesBenefactorExist(value interface{}) error {
// benefactorID, ok := value.(int)
// 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) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
// return func(value interface{}) error {
// kbID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
// _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
// }
// }
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
kindboxreqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if isExist, err := v.repo.KindBoxRequestExist(kindboxreqID); !isExist || err != nil {
if err != nil {
return err
}
if !isExist {
return errors.New("kind box request is not exist")
}
}
return nil
}
2024-01-22 15:21:13 +00:00
func (v Validator) CheckKindBoxReqStatusForAccepting(value interface{}) error {
kindboxreqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
err := v.repo.CheckKindBoxReqStatusForAccepting(kindboxreqID)
if err != nil {
return err
}
return nil
}
2024-01-22 15:21:13 +00:00
func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error {
kindboxreqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
err := v.repo.CheckKindBoxReqStatusForRejecting(kindboxreqID)
if err != nil {
return err
}
return nil
}
//
// func (v Validator) doesTypeExist(value interface{}) error {
// typeID, ok := value.(int)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
// }
// _, err := v.repo.TypeExist(typeID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}
//
// func (v Validator) hasPendingStatus(value interface{}) error {
// kindboxID, ok := value.(uint)
// if !ok {
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
// }
// _, err := v.repo.PendingStatus(kindboxID)
// if err != nil {
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
// }
//
// return nil
//}