niki/validator/benefactor/benefactor/validator.go

76 lines
2.2 KiB
Go

package benefactorvalidator
import (
"context"
"fmt"
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
const (
phoneNumberRegex = "^09\\d{9}$"
)
type BenefactorSvc interface {
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
}
type BenefactorAddressSvc interface {
ProvinceExistByID(ctx context.Context, request addressparam.ProvinceExistByIDRequest) (addressparam.ProvinceExistByIDResponse, error)
CityExistByID(ctx context.Context, request addressparam.CityExistByIDRequest) (addressparam.CityExistByIDResponse, error)
}
type Validator struct {
benefactorSvc BenefactorSvc
benefactorAddressSvc BenefactorAddressSvc
}
func New(benefactorSvc BenefactorSvc, benefactorAddressSvc BenefactorAddressSvc) Validator {
return Validator{benefactorSvc: benefactorSvc, benefactorAddressSvc: benefactorAddressSvc}
}
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)
}
return nil
}
func (v Validator) doesProvinceExist(value interface{}) error {
provinceID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
province, err := v.benefactorAddressSvc.ProvinceExistByID(context.Background(), addressparam.ProvinceExistByIDRequest{ID: provinceID})
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
if !province.Existed {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) doesCityExist(value interface{}) error {
cityID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
city, err := v.benefactorAddressSvc.CityExistByID(context.Background(), addressparam.CityExistByIDRequest{ID: cityID})
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
if !city.Existed {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}