2024-01-02 14:04:16 +00:00
|
|
|
package benefactorkindboxvalidator
|
2023-12-21 13:00:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2023-12-30 19:51:22 +00:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2023-12-21 13:00:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
2023-12-28 13:43:06 +00:00
|
|
|
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)
|
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 {
|
2024-01-01 07:22:14 +00:00
|
|
|
benefactorID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-12-27 21:55:15 +00:00
|
|
|
_, err := v.repo.KindBoxExist(kindBoxID)
|
2023-12-21 13:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
|
2023-12-30 19:51:22 +00:00
|
|
|
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)
|
2023-12-30 19:51:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
|
2023-12-30 19:51:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
}
|