2023-12-27 21:55:15 +00:00
|
|
|
package userkindboxreq
|
2023-12-21 13:00:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MinKindBoxReq = 1
|
|
|
|
MaxKindBoxReq = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
2023-12-28 13:43:06 +00:00
|
|
|
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)
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Validator struct {
|
|
|
|
repo Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo Repository) Validator {
|
|
|
|
return Validator{repo: repo}
|
|
|
|
}
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
func (v Validator) doesBenefactorExist(value interface{}) error {
|
2023-12-28 13:43:06 +00:00
|
|
|
benefactorID := value.(uint)
|
2023-12-27 21:55:15 +00:00
|
|
|
_, err := v.repo.BenefactorExist(benefactorID)
|
2023-12-21 13:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesTypeExist(value interface{}) error {
|
2023-12-28 13:43:06 +00:00
|
|
|
typeID := value.(uint)
|
2023-12-22 21:25:16 +00:00
|
|
|
_, err := v.repo.TypeExist(typeID)
|
2023-12-21 13:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-27 21:55:15 +00:00
|
|
|
|
|
|
|
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
|
2023-12-28 13:43:06 +00:00
|
|
|
kindBoxReqID := value.(uint)
|
2023-12-27 21:55:15 +00:00
|
|
|
_, err := v.repo.KindBoxReqExist(kindBoxReqID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
|
|
|
|
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error {
|
|
|
|
kbId := kindBoxID.(uint)
|
|
|
|
bId := benefactorID.(uint)
|
|
|
|
_, err := v.repo.KindBoxBelongToBenefactor(bId, 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
|
|
|
|
}
|