fix(delivery): fix domain address benefactor

This commit is contained in:
mohammad mahdi rezaei 2024-01-22 18:11:55 +03:30
parent 6e0d616036
commit d829192bd6
32 changed files with 192 additions and 205 deletions

View File

@ -1,10 +1,11 @@
package adminhandler
import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
"net/http"
)
func (h Handler) LoginByPhoneNumber(c echo.Context) error {

View File

@ -1,10 +1,11 @@
package adminhandler
import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
"net/http"
)
func (h Handler) Register(c echo.Context) error {

View File

@ -1,4 +1,4 @@
package benefactorhandler
package benefactoraddresshandler
import (
"net/http"
@ -10,13 +10,14 @@ import (
)
func (h Handler) AddAddress(c echo.Context) error {
claims := claim.GetClaimsFromEchoContext(c)
req := param.BenefactorAddAddressRequest{BenefactorID: claims.UserID}
req := param.BenefactorAddAddressRequest{}
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
claims := claim.GetClaimsFromEchoContext(c)
req.BenefactorID = claims.UserID
if fieldErrors, err := h.benefactorVld.ValidateAddAddress(req); err != nil {
if fieldErrors, err := h.addressVld.ValidateAddAddress(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
@ -24,7 +25,7 @@ func (h Handler) AddAddress(c echo.Context) error {
"errors": fieldErrors,
})
}
resp, sErr := h.benefactorAddressSvc.Add(c.Request().Context(), req)
resp, sErr := h.addressSvc.Add(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)

View File

@ -1,4 +1,4 @@
package benefactorbasehandler
package benefactoraddresshandler
import (
"net/http"

View File

@ -1,4 +1,4 @@
package benefactorbasehandler
package benefactoraddresshandler
import (
"net/http"

View File

@ -0,0 +1,28 @@
package benefactoraddresshandler
import (
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
)
type Handler struct {
authConfig authservice.Config
authSvc authservice.Service
addressSvc benefactoraddressservice.Service
addressVld benefactoraddressvalidator.Validator
}
func New(
authConfig authservice.Config,
authSvc authservice.Service,
addressSvc benefactoraddressservice.Service,
addressVld benefactoraddressvalidator.Validator,
) Handler {
return Handler{
authConfig: authConfig,
authSvc: authSvc,
addressSvc: addressSvc,
addressVld: addressVld,
}
}

View File

@ -0,0 +1,16 @@
package benefactoraddresshandler
import (
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
"git.gocasts.ir/ebhomengo/niki/entity"
"github.com/labstack/echo/v4"
)
func (h Handler) SetRoutes(e *echo.Echo) {
r := e.Group("/address")
r.GET("/provinces", h.GetAllProvinces)
r.GET("/cities", h.GetAllCities)
r.POST("/", h.AddAddress, middleware.Auth(h.authSvc, h.authConfig),
middleware.BenefactorAuthorization(entity.UserBenefactorRole))
}

View File

@ -1,15 +0,0 @@
package benefactorbasehandler
import (
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
)
type Handler struct {
addressSvc benefactoraddressservice.Service
}
func New(addressSvc benefactoraddressservice.Service) Handler {
return Handler{
addressSvc: addressSvc,
}
}

View File

@ -1,10 +0,0 @@
package benefactorbasehandler
import "github.com/labstack/echo/v4"
func (h Handler) SetRoutes(e *echo.Echo) {
r := e.Group("/base")
r.GET("/provinces", h.GetAllProvinces)
r.GET("/cities", h.GetAllCities)
}

View File

@ -2,7 +2,6 @@ package benefactorhandler
import (
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
)
@ -12,20 +11,17 @@ type Handler struct {
authSvc authservice.Service
benefactorSvc benefactorservice.Service
benefactorVld benefactorvalidator.Validator
benefactorAddressSvc benefactoraddressservice.Service
}
func New(authConfig authservice.Config,
authSvc authservice.Service,
benefactorSvc benefactorservice.Service,
benefactorVld benefactorvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service,
) Handler {
return Handler{
authConfig: authConfig,
authSvc: authSvc,
benefactorSvc: benefactorSvc,
benefactorVld: benefactorVld,
benefactorAddressSvc: benefactorAddressSvc,
}
}

View File

@ -1,8 +1,6 @@
package benefactorhandler
import (
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware"
"git.gocasts.ir/ebhomengo/niki/entity"
"github.com/labstack/echo/v4"
)
@ -11,6 +9,4 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.POST("/send-otp", h.SendOtp)
r.POST("/login-register", h.loginOrRegister)
r.POST("/address", h.AddAddress, middleware.Auth(h.authSvc, h.authConfig),
middleware.BenefactorAuthorization(entity.UserBenefactorRole))
}

View File

@ -2,19 +2,20 @@ package httpserver
import (
"fmt"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
config "git.gocasts.ir/ebhomengo/niki/config"
benefactorbasehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/base"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin"
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
echo "github.com/labstack/echo/v4"
@ -26,7 +27,7 @@ type Server struct {
Router *echo.Echo
benefactorHandler benefactorhandler.Handler
benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler
benefactorBaseHandler benefactorbasehandler.Handler
benefactorAddressHandler benefactoraddresshandler.Handler
adminHandler adminhandler.Handler
}
@ -38,6 +39,7 @@ func New(
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service,
benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service,
benefactorAddressVld benefactoraddressvalidator.Validator,
adminSvc adminservice.Service,
adminVld adminvalidator.Validator,
adminAuthSvc adminauthservice.Service,
@ -45,9 +47,9 @@ func New(
return Server{
Router: echo.New(),
config: cfg,
benefactorHandler: benefactorhandler.New(cfg.Auth, authSvc, benefactorSvc, benefactorVld, benefactorAddressSvc),
benefactorHandler: benefactorhandler.New(cfg.Auth, authSvc, benefactorSvc, benefactorVld),
benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld),
benefactorBaseHandler: benefactorbasehandler.New(benefactorAddressSvc),
benefactorAddressHandler: benefactoraddresshandler.New(cfg.Auth, authSvc, benefactorAddressSvc, benefactorAddressVld),
adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld),
}
}
@ -61,7 +63,7 @@ func (s Server) Serve() {
s.Router.GET("/health-check", s.healthCheck)
s.benefactorHandler.SetRoutes(s.Router)
s.benefactorKindBoxReqHandler.SetRoutes(s.Router)
s.benefactorBaseHandler.SetRoutes(s.Router)
s.benefactorAddressHandler.SetRoutes(s.Router)
s.adminHandler.SetRoutes(s.Router)
// Start server

View File

@ -14,7 +14,6 @@ type Admin struct {
}
func (a *Admin) GetPassword() string {
return a.password
}

13
main.go
View File

@ -20,6 +20,7 @@ import (
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
_ "github.com/go-sql-driver/mysql"
@ -31,10 +32,10 @@ func main() {
mgr := migrator.New(cfg.Mysql)
mgr.Up()
authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc,
authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, benefactorAddressVld,
adminSvc, adminVld, adminAuthSvc := setupServices(cfg)
server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld,
benefactorAddressSvc, adminSvc, adminVld, adminAuthSvc)
benefactorAddressSvc, benefactorAddressVld, adminSvc, adminVld, adminAuthSvc)
server.Serve()
}
@ -42,7 +43,9 @@ func main() {
func setupServices(cfg config.Config) (
authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator,
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service, adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc adminauthservice.Service,
benefactorAddressSvc benefactoraddressservice.Service,
benefactorAddressVld benefactoraddressvalidator.Validator,
adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc adminauthservice.Service,
) {
authSvc = authservice.New(cfg.Auth)
@ -54,11 +57,11 @@ func setupServices(cfg config.Config) (
kavenegarSmsProvider := smsprovider.New(cfg.KavenegarSmsProvider)
otpSmsProvider := kavenegarotp.New(kavenegarSmsProvider)
authGenerator := authservice.New(cfg.Auth)
benefactorSvc = benefactorservice.New(cfg.BenefactorSvc, RedisOtp, otpSmsProvider, authGenerator, benefactorMysql)
benefactorAddressMysql := mysqladdress.New(MysqlRepo)
benefactorAddressSvc = benefactoraddressservice.New(benefactorAddressMysql)
benefactorVld = benefactorvalidator.New(benefactorSvc, benefactorAddressSvc)
benefactorAddressVld = benefactoraddressvalidator.New(benefactorSvc, benefactorAddressMysql)
benefactorVld = benefactorvalidator.New()
benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo)
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql)

View File

@ -1,8 +0,0 @@
package addressparam
type CityExistByIDRequest struct {
ID uint
}
type CityExistByIDResponse struct {
Existed bool
}

View File

@ -1,8 +0,0 @@
package addressparam
type ProvinceExistByIDRequest struct {
ID uint
}
type ProvinceExistByIDResponse struct {
Existed bool
}

View File

@ -2,6 +2,7 @@ package mysqladmin
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

View File

@ -4,11 +4,12 @@ import (
"context"
"database/sql"
"errors"
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
"time"
)
func (d DB) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) {

View File

@ -2,6 +2,7 @@ package adminservice
import (
"context"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
@ -36,5 +37,4 @@ func (s Service) LoginWithPhoneNumber(ctx context.Context, req adminserviceparam
RefreshToken: refreshToken,
},
}, nil
}

View File

@ -2,6 +2,7 @@ package adminservice
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"

View File

@ -3,6 +3,7 @@ package adminservice
import (
"context"
"fmt"
"git.gocasts.ir/ebhomengo/niki/config"
"git.gocasts.ir/ebhomengo/niki/entity"
"golang.org/x/crypto/bcrypt"
@ -39,6 +40,7 @@ func GenerateHash(password *string) error {
return nil
}
func CompareHash(hashedPassword, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}

View File

@ -1,10 +1,11 @@
package adminauthservice
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"github.com/golang-jwt/jwt/v4"
"strings"
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
"github.com/golang-jwt/jwt/v4"
)
type Config struct {

View File

@ -1,19 +0,0 @@
package benefactoraddressservice
import (
"context"
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) CityExistByID(ctx context.Context, req addressparam.CityExistByIDRequest) (addressparam.CityExistByIDResponse, error) {
const op = "benefactoraddressservice.CityExistByID"
isExisted, err := s.repo.IsExistCityByID(ctx, req.ID)
if err != nil {
return addressparam.CityExistByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return addressparam.CityExistByIDResponse{Existed: isExisted}, nil
}

View File

@ -1,19 +0,0 @@
package benefactoraddressservice
import (
"context"
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) ProvinceExistByID(ctx context.Context, req addressparam.ProvinceExistByIDRequest) (addressparam.ProvinceExistByIDResponse, error) {
const op = "benefactoraddressservice.ProvinceExistByID"
isExisted, err := s.repo.IsExistProvinceByID(ctx, req.ID)
if err != nil {
return addressparam.ProvinceExistByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return addressparam.ProvinceExistByIDResponse{Existed: isExisted}, nil
}

View File

@ -11,8 +11,6 @@ type Repository interface {
GetAddressByID(ctx context.Context, id uint) (*entity.Address, error)
GetAllProvinces(ctx context.Context) ([]entity.Province, error)
GetAllCities(ctx context.Context) ([]entity.City, error)
IsExistProvinceByID(ctx context.Context, id uint) (bool, error)
IsExistCityByID(ctx context.Context, id uint) (bool, error)
}
type Service struct {

View File

@ -2,11 +2,12 @@ package adminvalidator
import (
"errors"
"regexp"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
validation "github.com/go-ozzo/ozzo-validation/v4"
"regexp"
)
func (v Validator) ValidateLoginWithPhoneNumberRequest(req adminserviceparam.LoginWithPhoneNumberRequest) (map[string]string, error) {
@ -15,7 +16,7 @@ func (v Validator) ValidateLoginWithPhoneNumberRequest(req adminserviceparam.Log
if err := validation.ValidateStruct(&req,
// TODO - add regex
validation.Field(&req.Password, validation.Required, validation.NotNil,
validation.Length(8, 0)),
validation.Length(minLengthPassword, maxLengthPassword)),
validation.Field(&req.PhoneNumber,
validation.Required,

View File

@ -2,12 +2,13 @@ package adminvalidator
import (
"errors"
"regexp"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/go-ozzo/ozzo-validation/is"
validation "github.com/go-ozzo/ozzo-validation/v4"
"regexp"
)
func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest) (map[string]string, error) {
@ -15,13 +16,13 @@ func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest
if err := validation.ValidateStruct(&req,
// TODO - add length of code config from benefactor config
validation.Field(&req.FirstName,
validation.Length(3, 40)),
validation.Length(minLengthFirstName, maxLengthFirstName)),
validation.Field(&req.LastName,
validation.Length(3, 40)),
validation.Length(minLengthLastName, maxLengthLastName)),
// TODO - add regex
validation.Field(&req.Password, validation.Required, validation.NotNil,
validation.Length(8, 0)),
validation.Length(minLengthPassword, maxLengthPassword)),
validation.Field(&req.Gender, validation.By(v.IsGenderValid)),
validation.Field(&req.Role, validation.By(v.IsRoleValid), validation.Required),
validation.Field(&req.Status, validation.By(v.IsStatusValid), validation.Required),

View File

@ -3,12 +3,19 @@ package adminvalidator
import (
"context"
"fmt"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
const (
phoneNumberRegex = "^09\\d{9}$"
minLengthFirstName = 3
maxLengthFirstName = 40
minLengthLastName = 3
maxLengthLastName = 40
minLengthPassword = 8
maxLengthPassword = 32
)
type Repository interface {
@ -35,6 +42,7 @@ func (v Validator) doesAdminExistByPhoneNumber(value interface{}) error {
if !adminExisted {
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect)
}
return nil
}
@ -50,6 +58,7 @@ func (v Validator) IsPhoneNumberUnique(value interface{}) error {
if adminExisted {
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique)
}
return nil
}
@ -65,6 +74,7 @@ func (v Validator) doesAdminExistByEmail(value interface{}) error {
if adminExisted {
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique)
}
return nil
}
@ -74,7 +84,7 @@ func (v Validator) IsRoleValid(value interface{}) error {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if isValid := role.IsValid(); isValid != true {
if isValid := role.IsValid(); !isValid {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
}
@ -90,7 +100,7 @@ func (v Validator) IsGenderValid(value interface{}) error {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if isValid := gender.IsValid(); isValid != true {
if isValid := gender.IsValid(); !isValid {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
}
@ -103,7 +113,7 @@ func (v Validator) IsStatusValid(value interface{}) error {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if isValid := status.IsValid(); isValid != true {
if isValid := status.IsValid(); !isValid {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
}

View File

@ -1,4 +1,4 @@
package benefactorvalidator
package benefactoraddressvalidator
import (
"errors"

View File

@ -0,0 +1,70 @@
package benefactoraddressvalidator
import (
"context"
"fmt"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
type BenefactorSvc interface {
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
}
type Repository interface {
IsExistCityByID(ctx context.Context, id uint) (bool, error)
IsExistProvinceByID(ctx context.Context, id uint) (bool, error)
}
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) doesProvinceExist(value interface{}) error {
provinceID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
isExisted, err := v.repository.IsExistProvinceByID(context.Background(), provinceID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
if !isExisted {
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)
}
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
}

View File

@ -1,75 +1,11 @@
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
}
type Validator struct{}
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
func New() Validator {
return Validator{}
}