forked from ebhomengo/niki
1
0
Fork 0

pull from dev

This commit is contained in:
Abolfazl Nourzad 2024-01-25 20:24:42 +03:30
commit 79be4d0a0e
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
23 changed files with 183 additions and 192 deletions

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package benefactorbasehandler package benefactoraddresshandler
import ( import (
"net/http" "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,30 +2,26 @@ package benefactorhandler
import ( import (
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" 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" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
) )
type Handler struct { type Handler struct {
authConfig authservice.Config authConfig authservice.Config
authSvc authservice.Service authSvc authservice.Service
benefactorSvc benefactorservice.Service benefactorSvc benefactorservice.Service
benefactorVld benefactorvalidator.Validator benefactorVld benefactorvalidator.Validator
benefactorAddressSvc benefactoraddressservice.Service
} }
func New(authConfig authservice.Config, func New(authConfig authservice.Config,
authSvc authservice.Service, authSvc authservice.Service,
benefactorSvc benefactorservice.Service, benefactorSvc benefactorservice.Service,
benefactorVld benefactorvalidator.Validator, benefactorVld benefactorvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service,
) Handler { ) Handler {
return Handler{ return Handler{
authConfig: authConfig, authConfig: authConfig,
authSvc: authSvc, authSvc: authSvc,
benefactorSvc: benefactorSvc, benefactorSvc: benefactorSvc,
benefactorVld: benefactorVld, benefactorVld: benefactorVld,
benefactorAddressSvc: benefactorAddressSvc,
} }
} }

View File

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

View File

@ -10,11 +10,12 @@ import (
) )
func (h Handler) Add(c echo.Context) error { func (h Handler) Add(c echo.Context) error {
claims := claim.GetClaimsFromEchoContext(c) req := param.KindBoxReqAddRequest{}
req := param.KindBoxReqAddRequest{BenefactorID: claims.UserID}
if bErr := c.Bind(&req); bErr != nil { if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest) return echo.NewHTTPError(http.StatusBadRequest)
} }
claims := claim.GetClaimsFromEchoContext(c)
req.BenefactorID = claims.UserID
if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateAddRequest(req); err != nil { if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateAddRequest(req); err != nil {
msg, code := httpmsg.Error(err) msg, code := httpmsg.Error(err)

View File

@ -6,7 +6,7 @@ import (
config "git.gocasts.ir/ebhomengo/niki/config" config "git.gocasts.ir/ebhomengo/niki/config"
adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin"
adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req" adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req"
benefactorbasehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/base" benefactoraddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/address"
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor" benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req" benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
@ -18,6 +18,7 @@ import (
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req" benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
@ -29,7 +30,7 @@ type Server struct {
Router *echo.Echo Router *echo.Echo
benefactorHandler benefactorhandler.Handler benefactorHandler benefactorhandler.Handler
benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler
benefactorBaseHandler benefactorbasehandler.Handler benefactorAddressHandler benefactoraddresshandler.Handler
adminHandler adminhandler.Handler adminHandler adminhandler.Handler
adminKindBoxReqHandler adminkindboxreqhandler.Handler adminKindBoxReqHandler adminkindboxreqhandler.Handler
} }
@ -42,6 +43,7 @@ func New(
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service,
benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
benefactorAddressSvc benefactoraddressservice.Service, benefactorAddressSvc benefactoraddressservice.Service,
benefactorAddressVld benefactoraddressvalidator.Validator,
adminSvc adminservice.Service, adminSvc adminservice.Service,
adminVld adminvalidator.Validator, adminVld adminvalidator.Validator,
adminAuthSvc adminauthservice.Service, adminAuthSvc adminauthservice.Service,
@ -51,9 +53,9 @@ func New(
return Server{ return Server{
Router: echo.New(), Router: echo.New(),
config: cfg, 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), 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), adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld),
adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, authSvc, adminKinBoxReqSvc, adminKinBoxReqVld), adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, authSvc, adminKinBoxReqSvc, adminKinBoxReqVld),
} }
@ -68,7 +70,7 @@ func (s Server) Serve() {
s.Router.GET("/health-check", s.healthCheck) s.Router.GET("/health-check", s.healthCheck)
s.benefactorHandler.SetRoutes(s.Router) s.benefactorHandler.SetRoutes(s.Router)
s.benefactorKindBoxReqHandler.SetRoutes(s.Router) s.benefactorKindBoxReqHandler.SetRoutes(s.Router)
s.benefactorBaseHandler.SetRoutes(s.Router) s.benefactorAddressHandler.SetRoutes(s.Router)
s.adminHandler.SetRoutes(s.Router) s.adminHandler.SetRoutes(s.Router)
s.adminKindBoxReqHandler.SetRoutes(s.Router) s.adminKindBoxReqHandler.SetRoutes(s.Router)

27
main.go
View File

@ -24,6 +24,7 @@ import (
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req" benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -35,10 +36,21 @@ func main() {
mgr := migrator.New(cfg.Mysql) mgr := migrator.New(cfg.Mysql)
mgr.Up() mgr.Up()
authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, authSvc,
adminSvc, adminVld, adminAuthSvc, adminKindBoxReqSvc, adminKindBoxReqVld := setupServices(cfg) benefactorSvc,
benefactorVld,
benefactorKindBoxReqSvc,
benefactorKindBoxReqVld,
benefactorAddressSvc,
benefactorAddressVld,
adminSvc,
adminVld,
adminAuthSvc,
adminKindBoxReqSvc,
adminKindBoxReqVld := setupServices(cfg)
server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld,
benefactorAddressSvc, adminSvc, adminVld, adminAuthSvc, adminKindBoxReqSvc, adminKindBoxReqVld) benefactorAddressSvc, benefactorAddressVld, adminSvc, adminVld, adminAuthSvc, adminKindBoxReqSvc, adminKindBoxReqVld)
server.Serve() server.Serve()
} }
@ -46,8 +58,12 @@ func main() {
func setupServices(cfg config.Config) ( func setupServices(cfg config.Config) (
authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator,
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.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,
adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator, adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator,
) { ) {
authSvc = authservice.New(cfg.Auth) authSvc = authservice.New(cfg.Auth)
@ -63,7 +79,8 @@ func setupServices(cfg config.Config) (
benefactorSvc = benefactorservice.New(cfg.BenefactorSvc, RedisOtp, otpSmsProvider, authGenerator, benefactorMysql) benefactorSvc = benefactorservice.New(cfg.BenefactorSvc, RedisOtp, otpSmsProvider, authGenerator, benefactorMysql)
benefactorAddressMysql := mysqladdress.New(MysqlRepo) benefactorAddressMysql := mysqladdress.New(MysqlRepo)
benefactorAddressSvc = benefactoraddressservice.New(benefactorAddressMysql) benefactorAddressSvc = benefactoraddressservice.New(benefactorAddressMysql)
benefactorVld = benefactorvalidator.New(benefactorSvc, benefactorAddressSvc) benefactorAddressVld = benefactoraddressvalidator.New(benefactorSvc, benefactorAddressMysql)
benefactorVld = benefactorvalidator.New()
benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo) benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo)
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql) 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

@ -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) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error)
GetAllProvinces(ctx context.Context) ([]entity.Province, error) GetAllProvinces(ctx context.Context) ([]entity.Province, error)
GetAllCities(ctx context.Context) ([]entity.City, 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 { type Service struct {

View File

@ -16,7 +16,7 @@ func (v Validator) ValidateLoginWithPhoneNumberRequest(req adminserviceparam.Log
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
// TODO - add regex // TODO - add regex
validation.Field(&req.Password, validation.Required, validation.NotNil, validation.Field(&req.Password, validation.Required, validation.NotNil,
validation.Length(8, 0)), validation.Length(minLengthPassword, maxLengthPassword)),
validation.Field(&req.PhoneNumber, validation.Field(&req.PhoneNumber,
validation.Required, validation.Required,

View File

@ -16,13 +16,13 @@ func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
// TODO - add length of code config from benefactor config // TODO - add length of code config from benefactor config
validation.Field(&req.FirstName, validation.Field(&req.FirstName,
validation.Length(3, 40)), validation.Length(minLengthFirstName, maxLengthFirstName)),
validation.Field(&req.LastName, validation.Field(&req.LastName,
validation.Length(3, 40)), validation.Length(minLengthLastName, maxLengthLastName)),
// TODO - add regex // TODO - add regex
validation.Field(&req.Password, validation.Required, validation.NotNil, 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.Gender, validation.By(v.IsGenderValid)),
validation.Field(&req.Role, validation.By(v.IsRoleValid), validation.Required), validation.Field(&req.Role, validation.By(v.IsRoleValid), validation.Required),
validation.Field(&req.Status, validation.By(v.IsStatusValid), validation.Required), validation.Field(&req.Status, validation.By(v.IsStatusValid), validation.Required),

View File

@ -9,7 +9,13 @@ import (
) )
const ( const (
phoneNumberRegex = "^09\\d{9}$" phoneNumberRegex = "^09\\d{9}$"
minLengthFirstName = 3
maxLengthFirstName = 40
minLengthLastName = 3
maxLengthLastName = 40
minLengthPassword = 8
maxLengthPassword = 32
) )
type Repository interface { type Repository interface {
@ -36,6 +42,7 @@ func (v Validator) doesAdminExistByPhoneNumber(value interface{}) error {
if !adminExisted { if !adminExisted {
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect) return fmt.Errorf(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect)
} }
return nil return nil
} }
@ -51,6 +58,7 @@ func (v Validator) IsPhoneNumberUnique(value interface{}) error {
if adminExisted { if adminExisted {
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique) return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique)
} }
return nil return nil
} }
@ -66,6 +74,7 @@ func (v Validator) doesAdminExistByEmail(value interface{}) error {
if adminExisted { if adminExisted {
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique) return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique)
} }
return nil return nil
} }
@ -75,7 +84,7 @@ func (v Validator) IsRoleValid(value interface{}) error {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
if isValid := role.IsValid(); isValid != true { if isValid := role.IsValid(); !isValid {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput) return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
} }
@ -91,7 +100,7 @@ func (v Validator) IsGenderValid(value interface{}) error {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
if isValid := gender.IsValid(); isValid != true { if isValid := gender.IsValid(); !isValid {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput) return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
} }
@ -104,7 +113,7 @@ func (v Validator) IsStatusValid(value interface{}) error {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
if isValid := status.IsValid(); isValid != true { if isValid := status.IsValid(); !isValid {
return fmt.Errorf(errmsg.ErrorMsgInvalidInput) return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
} }

View File

@ -1,4 +1,4 @@
package benefactorvalidator package benefactoraddressvalidator
import ( import (
"errors" "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 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 ( const (
phoneNumberRegex = "^09\\d{9}$" phoneNumberRegex = "^09\\d{9}$"
) )
type BenefactorSvc interface { type Validator struct{}
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 { func New() Validator {
return Validator{benefactorSvc: benefactorSvc, benefactorAddressSvc: benefactorAddressSvc} return Validator{}
}
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
} }