From d829192bd667f027524960ec7b424f681d2ea556 Mon Sep 17 00:00:00 2001 From: mohammad mahdi rezaei Date: Mon, 22 Jan 2024 18:11:55 +0330 Subject: [PATCH] fix(delivery): fix domain address benefactor --- delivery/http_server/admin/admin/login.go | 3 +- delivery/http_server/admin/admin/register.go | 3 +- .../{benefactor => address}/add_address.go | 11 +-- .../{base => address}/get_all_cities.go | 2 +- .../{base => address}/get_all_provinces.go | 2 +- .../http_server/benefactor/address/handler.go | 28 ++++++++ .../http_server/benefactor/address/route.go | 16 +++++ .../http_server/benefactor/base/handler.go | 15 ---- delivery/http_server/benefactor/base/route.go | 10 --- .../benefactor/benefactor/handler.go | 20 +++--- .../benefactor/benefactor/route.go | 4 -- delivery/http_server/server.go | 20 +++--- entity/admin.go | 1 - main.go | 13 ++-- param/benefactor/address/city_exit_by_id.go | 8 --- .../benefactor/address/province_exit_by_id.go | 8 --- repository/mysql/admin/create.go | 1 + repository/mysql/admin/exist_admin.go | 1 + repository/mysql/admin/get.go | 3 +- service/admin/admin/login.go | 2 +- service/admin/admin/register.go | 1 + service/admin/admin/service.go | 2 + service/auth/admin/service.go | 5 +- service/benefactor/address/city_exit_by_id.go | 19 ----- .../benefactor/address/province_exit_by_id.go | 19 ----- service/benefactor/address/service.go | 2 - validator/admin/admin/login.go | 7 +- validator/admin/admin/register.go | 11 +-- validator/admin/admin/validator.go | 18 +++-- .../{benefactor => address}/add_address.go | 2 +- validator/benefactor/address/validator.go | 70 +++++++++++++++++++ validator/benefactor/benefactor/validator.go | 70 +------------------ 32 files changed, 192 insertions(+), 205 deletions(-) rename delivery/http_server/benefactor/{benefactor => address}/add_address.go (73%) rename delivery/http_server/benefactor/{base => address}/get_all_cities.go (93%) rename delivery/http_server/benefactor/{base => address}/get_all_provinces.go (93%) create mode 100644 delivery/http_server/benefactor/address/handler.go create mode 100644 delivery/http_server/benefactor/address/route.go delete mode 100644 delivery/http_server/benefactor/base/handler.go delete mode 100644 delivery/http_server/benefactor/base/route.go delete mode 100644 param/benefactor/address/city_exit_by_id.go delete mode 100644 param/benefactor/address/province_exit_by_id.go delete mode 100644 service/benefactor/address/city_exit_by_id.go delete mode 100644 service/benefactor/address/province_exit_by_id.go rename validator/benefactor/{benefactor => address}/add_address.go (97%) create mode 100644 validator/benefactor/address/validator.go diff --git a/delivery/http_server/admin/admin/login.go b/delivery/http_server/admin/admin/login.go index f68c4ab..5dd3021 100644 --- a/delivery/http_server/admin/admin/login.go +++ b/delivery/http_server/admin/admin/login.go @@ -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 { diff --git a/delivery/http_server/admin/admin/register.go b/delivery/http_server/admin/admin/register.go index 1e9b330..6ba5a0f 100644 --- a/delivery/http_server/admin/admin/register.go +++ b/delivery/http_server/admin/admin/register.go @@ -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 { diff --git a/delivery/http_server/benefactor/benefactor/add_address.go b/delivery/http_server/benefactor/address/add_address.go similarity index 73% rename from delivery/http_server/benefactor/benefactor/add_address.go rename to delivery/http_server/benefactor/address/add_address.go index 7f8c668..c9beb4f 100644 --- a/delivery/http_server/benefactor/benefactor/add_address.go +++ b/delivery/http_server/benefactor/address/add_address.go @@ -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) diff --git a/delivery/http_server/benefactor/base/get_all_cities.go b/delivery/http_server/benefactor/address/get_all_cities.go similarity index 93% rename from delivery/http_server/benefactor/base/get_all_cities.go rename to delivery/http_server/benefactor/address/get_all_cities.go index 39667a7..b66cde0 100644 --- a/delivery/http_server/benefactor/base/get_all_cities.go +++ b/delivery/http_server/benefactor/address/get_all_cities.go @@ -1,4 +1,4 @@ -package benefactorbasehandler +package benefactoraddresshandler import ( "net/http" diff --git a/delivery/http_server/benefactor/base/get_all_provinces.go b/delivery/http_server/benefactor/address/get_all_provinces.go similarity index 93% rename from delivery/http_server/benefactor/base/get_all_provinces.go rename to delivery/http_server/benefactor/address/get_all_provinces.go index 881aa79..00fb1cc 100644 --- a/delivery/http_server/benefactor/base/get_all_provinces.go +++ b/delivery/http_server/benefactor/address/get_all_provinces.go @@ -1,4 +1,4 @@ -package benefactorbasehandler +package benefactoraddresshandler import ( "net/http" diff --git a/delivery/http_server/benefactor/address/handler.go b/delivery/http_server/benefactor/address/handler.go new file mode 100644 index 0000000..5357b9a --- /dev/null +++ b/delivery/http_server/benefactor/address/handler.go @@ -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, + } +} diff --git a/delivery/http_server/benefactor/address/route.go b/delivery/http_server/benefactor/address/route.go new file mode 100644 index 0000000..d8c6a06 --- /dev/null +++ b/delivery/http_server/benefactor/address/route.go @@ -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)) +} diff --git a/delivery/http_server/benefactor/base/handler.go b/delivery/http_server/benefactor/base/handler.go deleted file mode 100644 index 833d21e..0000000 --- a/delivery/http_server/benefactor/base/handler.go +++ /dev/null @@ -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, - } -} diff --git a/delivery/http_server/benefactor/base/route.go b/delivery/http_server/benefactor/base/route.go deleted file mode 100644 index 0578b41..0000000 --- a/delivery/http_server/benefactor/base/route.go +++ /dev/null @@ -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) -} diff --git a/delivery/http_server/benefactor/benefactor/handler.go b/delivery/http_server/benefactor/benefactor/handler.go index e0663b4..d6947a0 100644 --- a/delivery/http_server/benefactor/benefactor/handler.go +++ b/delivery/http_server/benefactor/benefactor/handler.go @@ -2,30 +2,26 @@ 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" ) type Handler struct { - authConfig authservice.Config - authSvc authservice.Service - benefactorSvc benefactorservice.Service - benefactorVld benefactorvalidator.Validator - benefactorAddressSvc benefactoraddressservice.Service + authConfig authservice.Config + authSvc authservice.Service + benefactorSvc benefactorservice.Service + benefactorVld benefactorvalidator.Validator } 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, + authConfig: authConfig, + authSvc: authSvc, + benefactorSvc: benefactorSvc, + benefactorVld: benefactorVld, } } diff --git a/delivery/http_server/benefactor/benefactor/route.go b/delivery/http_server/benefactor/benefactor/route.go index 7a59b57..0f4fca0 100644 --- a/delivery/http_server/benefactor/benefactor/route.go +++ b/delivery/http_server/benefactor/benefactor/route.go @@ -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)) } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index e47305f..261dddb 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -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 diff --git a/entity/admin.go b/entity/admin.go index 8e78ed9..97c0780 100644 --- a/entity/admin.go +++ b/entity/admin.go @@ -14,7 +14,6 @@ type Admin struct { } func (a *Admin) GetPassword() string { - return a.password } diff --git a/main.go b/main.go index 7c1e9b2..c834341 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/param/benefactor/address/city_exit_by_id.go b/param/benefactor/address/city_exit_by_id.go deleted file mode 100644 index 6e7098b..0000000 --- a/param/benefactor/address/city_exit_by_id.go +++ /dev/null @@ -1,8 +0,0 @@ -package addressparam - -type CityExistByIDRequest struct { - ID uint -} -type CityExistByIDResponse struct { - Existed bool -} diff --git a/param/benefactor/address/province_exit_by_id.go b/param/benefactor/address/province_exit_by_id.go deleted file mode 100644 index f16e90d..0000000 --- a/param/benefactor/address/province_exit_by_id.go +++ /dev/null @@ -1,8 +0,0 @@ -package addressparam - -type ProvinceExistByIDRequest struct { - ID uint -} -type ProvinceExistByIDResponse struct { - Existed bool -} diff --git a/repository/mysql/admin/create.go b/repository/mysql/admin/create.go index a664fe3..9c4724a 100644 --- a/repository/mysql/admin/create.go +++ b/repository/mysql/admin/create.go @@ -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" diff --git a/repository/mysql/admin/exist_admin.go b/repository/mysql/admin/exist_admin.go index 87d298b..85dc0ef 100644 --- a/repository/mysql/admin/exist_admin.go +++ b/repository/mysql/admin/exist_admin.go @@ -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" ) diff --git a/repository/mysql/admin/get.go b/repository/mysql/admin/get.go index fcb1ad2..b8b0626 100644 --- a/repository/mysql/admin/get.go +++ b/repository/mysql/admin/get.go @@ -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) { diff --git a/service/admin/admin/login.go b/service/admin/admin/login.go index 3efe316..f8b6d87 100644 --- a/service/admin/admin/login.go +++ b/service/admin/admin/login.go @@ -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 - } diff --git a/service/admin/admin/register.go b/service/admin/admin/register.go index 92b5da6..62acf92 100644 --- a/service/admin/admin/register.go +++ b/service/admin/admin/register.go @@ -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" diff --git a/service/admin/admin/service.go b/service/admin/admin/service.go index 1aaeb99..e8617ab 100644 --- a/service/admin/admin/service.go +++ b/service/admin/admin/service.go @@ -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)) } diff --git a/service/auth/admin/service.go b/service/auth/admin/service.go index bfd6797..aada499 100644 --- a/service/auth/admin/service.go +++ b/service/auth/admin/service.go @@ -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 { diff --git a/service/benefactor/address/city_exit_by_id.go b/service/benefactor/address/city_exit_by_id.go deleted file mode 100644 index 0ee3501..0000000 --- a/service/benefactor/address/city_exit_by_id.go +++ /dev/null @@ -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 -} diff --git a/service/benefactor/address/province_exit_by_id.go b/service/benefactor/address/province_exit_by_id.go deleted file mode 100644 index a188bd2..0000000 --- a/service/benefactor/address/province_exit_by_id.go +++ /dev/null @@ -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 -} diff --git a/service/benefactor/address/service.go b/service/benefactor/address/service.go index a7310ff..646e45e 100644 --- a/service/benefactor/address/service.go +++ b/service/benefactor/address/service.go @@ -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 { diff --git a/validator/admin/admin/login.go b/validator/admin/admin/login.go index 3140644..0b04d54 100644 --- a/validator/admin/admin/login.go +++ b/validator/admin/admin/login.go @@ -2,20 +2,21 @@ 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) { const op = "adminvalidator.ValidateRegisterRequest" if err := validation.ValidateStruct(&req, - //TODO - add regex + // 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, diff --git a/validator/admin/admin/register.go b/validator/admin/admin/register.go index 4d6b092..b1d1427 100644 --- a/validator/admin/admin/register.go +++ b/validator/admin/admin/register.go @@ -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 + // 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), diff --git a/validator/admin/admin/validator.go b/validator/admin/admin/validator.go index 8388332..b295ee7 100644 --- a/validator/admin/admin/validator.go +++ b/validator/admin/admin/validator.go @@ -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}$" + 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) } diff --git a/validator/benefactor/benefactor/add_address.go b/validator/benefactor/address/add_address.go similarity index 97% rename from validator/benefactor/benefactor/add_address.go rename to validator/benefactor/address/add_address.go index feb0e63..f18c2c6 100644 --- a/validator/benefactor/benefactor/add_address.go +++ b/validator/benefactor/address/add_address.go @@ -1,4 +1,4 @@ -package benefactorvalidator +package benefactoraddressvalidator import ( "errors" diff --git a/validator/benefactor/address/validator.go b/validator/benefactor/address/validator.go new file mode 100644 index 0000000..2764d67 --- /dev/null +++ b/validator/benefactor/address/validator.go @@ -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 +} diff --git a/validator/benefactor/benefactor/validator.go b/validator/benefactor/benefactor/validator.go index 4b25699..5fe5015 100644 --- a/validator/benefactor/benefactor/validator.go +++ b/validator/benefactor/benefactor/validator.go @@ -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{} }