2024-01-22 14:41:55 +00:00
|
|
|
package benefactoraddressvalidator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
|
2024-01-22 14:41:55 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2024-06-08 14:25:24 +00:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2024-01-22 14:41:55 +00:00
|
|
|
)
|
|
|
|
|
2024-08-10 17:47:21 +00:00
|
|
|
//go:generate mockery --name BenefactorSvc
|
2024-01-22 14:41:55 +00:00
|
|
|
type BenefactorSvc interface {
|
|
|
|
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
|
|
|
|
}
|
2024-08-10 17:47:21 +00:00
|
|
|
|
|
|
|
//go:generate mockery --name Repository
|
2024-01-22 14:41:55 +00:00
|
|
|
type Repository interface {
|
|
|
|
IsExistCityByID(ctx context.Context, id uint) (bool, error)
|
2024-06-08 14:25:24 +00:00
|
|
|
IsExistAddressByID(ctx context.Context, addressID uint, benefactorID uint) (bool, error)
|
2024-01-22 14:41:55 +00:00
|
|
|
}
|
|
|
|
type Validator struct {
|
|
|
|
benefactorSvc BenefactorSvc
|
|
|
|
repository Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(benefactorSvc BenefactorSvc, repository Repository) Validator {
|
|
|
|
return Validator{benefactorSvc: benefactorSvc, repository: repository}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesBenefactorExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
benefactorID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-08-10 17:47:21 +00:00
|
|
|
res, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID})
|
2024-08-01 10:20:18 +00:00
|
|
|
if err != nil {
|
2024-08-10 17:47:21 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.Existed {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
2024-08-10 17:47:21 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
2024-01-22 14:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesCityExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
cityID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
isExisted, err := v.repository.IsExistCityByID(ctx, cityID)
|
|
|
|
if err != nil {
|
2024-08-10 17:47:21 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
if !isExisted {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
2024-01-22 14:41:55 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-22 14:41:55 +00:00
|
|
|
}
|
2024-06-08 14:25:24 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesAddressExist(ctx context.Context, benefactorID uint) validation.RuleFunc {
|
2024-06-08 14:25:24 +00:00
|
|
|
return func(value interface{}) error {
|
|
|
|
addressID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
isExisted, err := v.repository.IsExistAddressByID(ctx, addressID, benefactorID)
|
2024-06-08 14:25:24 +00:00
|
|
|
if err != nil {
|
2024-08-10 17:47:21 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
2024-06-08 14:25:24 +00:00
|
|
|
}
|
|
|
|
if !isExisted {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|