2024-01-22 14:41:55 +00:00
|
|
|
package benefactoraddressvalidator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore"
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type BenefactorSvc interface {
|
|
|
|
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
|
|
|
|
}
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesBenefactorExist(value interface{}) error {
|
|
|
|
benefactorID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
_, err := v.benefactorSvc.BenefactorExistByID(context.Background(), param.BenefactorExistByIDRequest{ID: benefactorID})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
// TODO: check benefactor ID given from user most check with claims (benefactorID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesCityExist(value interface{}) error {
|
|
|
|
cityID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
isExisted, err := v.repository.IsExistCityByID(context.Background(), cityID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
if !isExisted {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-08 14:25:24 +00:00
|
|
|
|
|
|
|
func (v Validator) doesAddressExist(benefactorID uint) validation.RuleFunc {
|
|
|
|
|
|
|
|
return func(value interface{}) error {
|
|
|
|
addressID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
isExisted, err := v.repository.IsExistAddressByID(context.Background(), addressID, benefactorID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
if !isExisted {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|