niki/validator/admin/kind_box/validator.go

97 lines
2.1 KiB
Go

package adminkindbox
import (
"fmt"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
type Repository interface {
KindBoxRequestExist(id uint) (bool, error)
EmployeeExist(id uint) (bool, error)
BenefactorExist(id uint) (bool, error)
KindBoxExist(id uint) (bool, error)
KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
PendingStatus(id uint) (bool, error)
CheckStatus(status string) (bool, error)
}
type Validator struct {
repo Repository
}
func New(repo Repository) Validator {
return Validator{repo: repo}
}
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
receiverID := value.(uint)
_, err := v.repo.KindBoxRequestExist(receiverID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesEmployeeExist(value interface{}) error {
senderID := value.(uint)
_, err := v.repo.EmployeeExist(senderID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
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) doesKindBoxExist(value interface{}) error {
kindboxID := value.(uint)
_, err := v.repo.KindBoxExist(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
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) hasCorrectStatus(value interface{}) error {
status := value.(string)
_, err := v.repo.CheckStatus(status)
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
}