From e3109b197285b3d45293fa6a61d36b4ec9299300 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Mon, 22 Jan 2024 11:44:57 +0330 Subject: [PATCH] add service and repo layer for accepting kind box request --- delivery/http_server/admin/admin/login.go | 3 +- delivery/http_server/admin/admin/register.go | 3 +- .../http_server/admin/kind_box_req/accept.go | 35 ++++ .../http_server/admin/kind_box_req/get.go | 52 +++--- .../http_server/admin/kind_box_req/get_all.go | 46 +++--- .../http_server/admin/kind_box_req/route.go | 5 +- delivery/http_server/server.go | 16 +- entity/admin.go | 1 - entity/kind_box_req.go | 1 + main.go | 10 +- param/admin/kind_box_req/accept.go | 25 +++ repository/mysql/admin/create.go | 1 + repository/mysql/admin/exist_admin.go | 1 + repository/mysql/admin/get.go | 3 +- repository/mysql/kind_box_req/kind_box_req.go | 29 +++- repository/mysql/kind_box_req/scan.go | 19 +++ service/admin/admin/login.go | 2 +- service/admin/admin/register.go | 1 + service/admin/admin/service.go | 2 + service/admin/kind_box_req/accept.go | 35 ++++ service/admin/kind_box_req/delete.go | 37 ++--- service/admin/kind_box_req/get.go | 37 ++--- service/admin/kind_box_req/get_all.go | 37 ++--- service/admin/kind_box_req/service.go | 15 +- service/auth/admin/service.go | 5 +- validator/admin/admin/login.go | 5 +- validator/admin/admin/register.go | 5 +- validator/admin/admin/validator.go | 1 + validator/admin/kind_box_req/add.go | 88 +++++----- validator/admin/kind_box_req/delete.go | 86 +++++----- validator/admin/kind_box_req/get.go | 84 +++++----- validator/admin/kind_box_req/update.go | 96 +++++------ validator/admin/kind_box_req/validator.go | 150 +++++++++--------- 33 files changed, 547 insertions(+), 389 deletions(-) create mode 100644 delivery/http_server/admin/kind_box_req/accept.go create mode 100644 param/admin/kind_box_req/accept.go create mode 100644 repository/mysql/kind_box_req/scan.go create mode 100644 service/admin/kind_box_req/accept.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/admin/kind_box_req/accept.go b/delivery/http_server/admin/kind_box_req/accept.go new file mode 100644 index 0000000..1e3f7d4 --- /dev/null +++ b/delivery/http_server/admin/kind_box_req/accept.go @@ -0,0 +1,35 @@ +package adminkindboxreqhandler + +import ( + "net/http" + "strconv" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + "github.com/labstack/echo/v4" +) + +func (h Handler) Accept(c echo.Context) error { + var req param.KindBoxReqAcceptRequest + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + num, _ := strconv.ParseUint(c.Param("id"), 36, 10) + req.ID = uint(num) + // if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetRequest(req); err != nil { + // msg, code := httpmsg.Error(err) + // + // return c.JSON(code, echo.Map{ + // "message": msg, + // "errors": fieldErrors, + // }) + //} + resp, sErr := h.adminKindBoxReqSvc.Accept(c.Request().Context(), req) + if sErr != nil { + msg, code := httpmsg.Error(sErr) + + return echo.NewHTTPError(code, msg) + } + + return c.JSON(http.StatusCreated, resp) +} diff --git a/delivery/http_server/admin/kind_box_req/get.go b/delivery/http_server/admin/kind_box_req/get.go index f4ac896..fa66c9a 100644 --- a/delivery/http_server/admin/kind_box_req/get.go +++ b/delivery/http_server/admin/kind_box_req/get.go @@ -1,32 +1,24 @@ package adminkindboxreqhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" - echo "github.com/labstack/echo/v4" -) - -func (h Handler) Get(c echo.Context) error { - var req param.KindBoxReqGetRequest - if bErr := c.Bind(&req); bErr != nil { - return echo.NewHTTPError(http.StatusBadRequest) - } - if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetRequest(req); err != nil { - msg, code := httpmsg.Error(err) - - return c.JSON(code, echo.Map{ - "message": msg, - "errors": fieldErrors, - }) - } - resp, sErr := h.adminKindBoxReqSvc.Get(c.Request().Context(), req) - if sErr != nil { - msg, code := httpmsg.Error(sErr) - - return echo.NewHTTPError(code, msg) - } - - return c.JSON(http.StatusCreated, resp) -} +// func (h Handler) Get(c echo.Context) error { +// var req param.KindBoxReqGetRequest +// if bErr := c.Bind(&req); bErr != nil { +// return echo.NewHTTPError(http.StatusBadRequest) +// } +// if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetRequest(req); err != nil { +// msg, code := httpmsg.Error(err) +// +// return c.JSON(code, echo.Map{ +// "message": msg, +// "errors": fieldErrors, +// }) +// } +// resp, sErr := h.adminKindBoxReqSvc.Get(c.Request().Context(), req) +// if sErr != nil { +// msg, code := httpmsg.Error(sErr) +// +// return echo.NewHTTPError(code, msg) +// } +// +// return c.JSON(http.StatusCreated, resp) +//} diff --git a/delivery/http_server/admin/kind_box_req/get_all.go b/delivery/http_server/admin/kind_box_req/get_all.go index 305705d..241e186 100644 --- a/delivery/http_server/admin/kind_box_req/get_all.go +++ b/delivery/http_server/admin/kind_box_req/get_all.go @@ -1,25 +1,25 @@ package adminkindboxreqhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" - echo "github.com/labstack/echo/v4" -) - -func (h Handler) GetAll(c echo.Context) error { - var req param.KindBoxReqGetAllRequest - if bErr := c.Bind(&req); bErr != nil { - return echo.NewHTTPError(http.StatusBadRequest) - } - - resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req) - if sErr != nil { - msg, code := httpmsg.Error(sErr) - - return echo.NewHTTPError(code, msg) - } - - return c.JSON(http.StatusCreated, resp) -} +// import ( +// "net/http" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" +// echo "github.com/labstack/echo/v4" +//) +// +// func (h Handler) GetAll(c echo.Context) error { +// var req param.KindBoxReqGetAllRequest +// if bErr := c.Bind(&req); bErr != nil { +// return echo.NewHTTPError(http.StatusBadRequest) +// } +// +// resp, sErr := h.adminKindBoxReqSvc.GetAll(c.Request().Context(), req) +// if sErr != nil { +// msg, code := httpmsg.Error(sErr) +// +// return echo.NewHTTPError(code, msg) +// } +// +// return c.JSON(http.StatusCreated, resp) +//} diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index ef9c179..60ff249 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -9,8 +9,9 @@ func (h Handler) SetRoutes(e *echo.Echo) { //nolint:gocritic //r.POST("/", h.Add).Name = "admin-addkindboxreq" - r.GET("/:id", h.Get).Name = "admin-getkindboxreqbyid" - r.GET("/", h.GetAll).Name = "admin-getallkindboxreq" + //r.GET("/:id", h.Get).Name = "admin-getkindboxreqbyid" + //r.GET("/", h.GetAll).Name = "admin-getallkindboxreq" //nolint:gocritic //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" + r.PATCH("/accept-kind-box-req/:id", h.Accept) } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index e47305f..a5c83f0 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -2,19 +2,22 @@ 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" + adminhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/admin" + adminkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/kind_box_req" benefactorbasehandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/base" 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" + adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" + 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" + adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" 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" @@ -28,6 +31,7 @@ type Server struct { benefactorKindBoxReqHandler benefactorkindboxreqhandler.Handler benefactorBaseHandler benefactorbasehandler.Handler adminHandler adminhandler.Handler + adminKindBoxReqHandler adminkindboxreqhandler.Handler } func New( @@ -41,6 +45,8 @@ func New( adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc adminauthservice.Service, + adminKinBoxReqSvc adminkindboxreqservice.Service, + adminKinBoxReqVld adminkindboxreqvalidator.Validator, ) Server { return Server{ Router: echo.New(), @@ -49,6 +55,7 @@ func New( benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld), benefactorBaseHandler: benefactorbasehandler.New(benefactorAddressSvc), adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld), + adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, authSvc, adminKinBoxReqSvc, adminKinBoxReqVld), } } @@ -63,6 +70,7 @@ func (s Server) Serve() { s.benefactorKindBoxReqHandler.SetRoutes(s.Router) s.benefactorBaseHandler.SetRoutes(s.Router) s.adminHandler.SetRoutes(s.Router) + s.adminKindBoxReqHandler.SetRoutes(s.Router) // Start server address := fmt.Sprintf(":%d", s.config.HTTPServer.Port) 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/entity/kind_box_req.go b/entity/kind_box_req.go index cbb0384..59e4c64 100644 --- a/entity/kind_box_req.go +++ b/entity/kind_box_req.go @@ -12,4 +12,5 @@ type KindBoxReq struct { Description string ReferDate time.Time AddressID uint + CreatedAt time.Time } diff --git a/main.go b/main.go index 7c1e9b2..c5c6ace 100644 --- a/main.go +++ b/main.go @@ -14,12 +14,14 @@ import ( mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req" redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp" adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" + adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" 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" + adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" 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" @@ -32,9 +34,9 @@ func main() { mgr.Up() authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, - adminSvc, adminVld, adminAuthSvc := setupServices(cfg) + adminSvc, adminVld, adminAuthSvc, adminKindBoxReqSvc, adminKindBoxReqVld := setupServices(cfg) server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, - benefactorAddressSvc, adminSvc, adminVld, adminAuthSvc) + benefactorAddressSvc, adminSvc, adminVld, adminAuthSvc, adminKindBoxReqSvc, adminKindBoxReqVld) server.Serve() } @@ -43,6 +45,7 @@ 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, + adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator, ) { authSvc = authservice.New(cfg.Auth) @@ -64,6 +67,9 @@ func setupServices(cfg config.Config) ( benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql) benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc) + adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql) + adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql) + adminAuthSvc = adminauthservice.New(cfg.AdminAuth) adminMysql := mysqladmin.New(MysqlRepo) adminVld = adminvalidator.New(adminMysql) diff --git a/param/admin/kind_box_req/accept.go b/param/admin/kind_box_req/accept.go new file mode 100644 index 0000000..ad480bb --- /dev/null +++ b/param/admin/kind_box_req/accept.go @@ -0,0 +1,25 @@ +package adminkindboxreqparam + +import ( + "time" + + "git.gocasts.ir/ebhomengo/niki/entity" +) + +type KindBoxReqAcceptRequest struct { + ID uint `json:"id"` + Description string `json:"description"` + CountAccepted uint `json:"count_accepted"` +} + +type KindBoxReqAcceptResponse struct { + ID uint `json:"id"` + KindBoxType entity.KindBoxType `json:"kind_box_type"` + CountRequested uint `json:"count_requested"` + CountAccepted uint `json:"count_accepted"` + BenefactorID uint `json:"benefactor_id"` + Status entity.KindBoxReqStatus `json:"status"` + Description string `json:"description"` + ReferDate time.Time `json:"refer_date"` + AddressID uint `json:"address_id"` +} 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/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index d9e2fc8..88291e2 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -2,7 +2,6 @@ package mysqlkindboxreq 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" @@ -25,3 +24,31 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en return kindBoxReq, nil } + +func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint, description string) error { + op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") + statement, err := d.conn.Conn(). + Prepare(`update kind_box_reqs set count_accepted = ? , description = ? , status = ? where id = ?`) + if err != nil { + return richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + _, eErr := statement.ExecContext(ctx, countAccepted, description, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID) + if eErr != nil { + return richerror.New(op).WithErr(eErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return nil +} + +func (d DB) FindByID(ctx context.Context, id uint) (entity.KindBoxReq, error) { + op := richerror.Op("mysqlkindboxreq.findByID") + row := d.conn.Conn().QueryRowContext(ctx, `select * from kind_box_reqs where id = ?`, id) + k, err := scanKindBoxReq(row) + if err != nil { + return entity.KindBoxReq{}, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + return k, nil +} diff --git a/repository/mysql/kind_box_req/scan.go b/repository/mysql/kind_box_req/scan.go new file mode 100644 index 0000000..8cda054 --- /dev/null +++ b/repository/mysql/kind_box_req/scan.go @@ -0,0 +1,19 @@ +package mysqlkindboxreq + +import ( + "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/repository/mysql" +) + +func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) { + var kindBoxReq entity.KindBoxReq + var kindBoxStatus string + var kindBoxType string + err := scanner.Scan(&kindBoxReq.ID, &kindBoxReq.BenefactorID, &kindBoxType, &kindBoxReq.AddressID, &kindBoxReq.CountRequested, &kindBoxReq.CountAccepted, + &kindBoxReq.Description, + &kindBoxReq.ReferDate, &kindBoxStatus, &kindBoxReq.CreatedAt) + + kindBoxReq.Status = entity.MapToKindBoxReqStatus(kindBoxStatus) + kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType) + return kindBoxReq, err +} 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/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go new file mode 100644 index 0000000..dc8efb9 --- /dev/null +++ b/service/admin/kind_box_req/accept.go @@ -0,0 +1,35 @@ +package adminkindboxreqservice + +import ( + "context" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) (param.KindBoxReqAcceptResponse, error) { + const op = "adminkindboxreqservice.Accept" + err := s.repo.AcceptKindBoxReq(ctx, req.ID, req.CountAccepted, req.Description) + if err != nil { + return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) + } + + // fire new event to create a kind-box. + //get kind box req + + kindBoxReq, gErr := s.repo.FindByID(ctx, req.ID) + if gErr != nil { + return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) + } + + return param.KindBoxReqAcceptResponse{ + ID: kindBoxReq.ID, + KindBoxType: kindBoxReq.KindBoxType, + CountRequested: kindBoxReq.CountRequested, + CountAccepted: kindBoxReq.CountAccepted, + BenefactorID: kindBoxReq.BenefactorID, + Status: kindBoxReq.Status, + Description: kindBoxReq.Description, + ReferDate: kindBoxReq.ReferDate, + AddressID: kindBoxReq.AddressID, + }, nil +} diff --git a/service/admin/kind_box_req/delete.go b/service/admin/kind_box_req/delete.go index 7c99b55..bf7481e 100644 --- a/service/admin/kind_box_req/delete.go +++ b/service/admin/kind_box_req/delete.go @@ -1,20 +1,21 @@ package adminkindboxreqservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) { - // TODO: Does business domain need to delete an kindboxreq ? - const op = "adminkindboxreqservice.Delete" - - dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) - if dErr != nil { - return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxReqDeleteResponse{}, nil -} +// +// import ( +// "context" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +// func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) { +// // TODO: Does business domain need to delete an kindboxreq ? +// const op = "adminkindboxreqservice.Delete" +// +// dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) +// if dErr != nil { +// return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxReqDeleteResponse{}, nil +//} diff --git a/service/admin/kind_box_req/get.go b/service/admin/kind_box_req/get.go index c1d52b8..f79f458 100644 --- a/service/admin/kind_box_req/get.go +++ b/service/admin/kind_box_req/get.go @@ -1,20 +1,21 @@ package adminkindboxreqservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) { - const op = "adminkindboxreqservice.Get" - - // TODO : ref to service.Update() - kindBoxReq, err := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) - if err != nil { - return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil -} +// +// import ( +// "context" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +// func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) { +// const op = "adminkindboxreqservice.Get" +// +// // TODO : ref to service.Update() +// kindBoxReq, err := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID) +// if err != nil { +// return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil +//} diff --git a/service/admin/kind_box_req/get_all.go b/service/admin/kind_box_req/get_all.go index a7d4061..f16af1b 100644 --- a/service/admin/kind_box_req/get_all.go +++ b/service/admin/kind_box_req/get_all.go @@ -1,20 +1,21 @@ package adminkindboxreqservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -// TODO: Pagination, Filters, Sort. -func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest) (param.KindBoxReqGetAllResponse, error) { - const op = "adminkindboxreqservice.GetAll" - - allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx) - if err != nil { - return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil -} +// +// import ( +// "context" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +//// TODO: Pagination, Filters, Sort. +// func (s Service) GetAll(ctx context.Context, _ param.KindBoxReqGetAllRequest) (param.KindBoxReqGetAllResponse, error) { +// const op = "adminkindboxreqservice.GetAll" +// +// allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx) +// if err != nil { +// return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil +//} diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 2b1c5ed..118707a 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -2,17 +2,18 @@ package adminkindboxreqservice import ( "context" - - entity "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/entity" ) type Repository interface { - AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) - UpdateKindBoxReq(ctx context.Context, kindBoxReqID uint, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) + // AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) + // UpdateKindBoxReq(ctx context.Context, kindBoxReqID uint, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) // TODO: can benefactor cancel its request ? - DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error - GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) - GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) + // DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error + // GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) + // GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) + AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint, description string) error + FindByID(ctx context.Context, id uint) (entity.KindBoxReq, error) } // TODO: check validation. 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/validator/admin/admin/login.go b/validator/admin/admin/login.go index 3140644..f2bc096 100644 --- a/validator/admin/admin/login.go +++ b/validator/admin/admin/login.go @@ -2,18 +2,19 @@ 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)), diff --git a/validator/admin/admin/register.go b/validator/admin/admin/register.go index 4d6b092..5d6967c 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) { @@ -19,7 +20,7 @@ func (v Validator) ValidateRegisterRequest(req adminserviceparam.RegisterRequest validation.Field(&req.LastName, validation.Length(3, 40)), - //TODO - add regex + // TODO - add regex validation.Field(&req.Password, validation.Required, validation.NotNil, validation.Length(8, 0)), validation.Field(&req.Gender, validation.By(v.IsGenderValid)), diff --git a/validator/admin/admin/validator.go b/validator/admin/admin/validator.go index 8388332..7aba1f1 100644 --- a/validator/admin/admin/validator.go +++ b/validator/admin/admin/validator.go @@ -3,6 +3,7 @@ package adminvalidator import ( "context" "fmt" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" ) diff --git a/validator/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go index 0a7a466..f44c259 100644 --- a/validator/admin/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -1,46 +1,46 @@ package adminkindboxreqvalidator -import ( - "errors" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - 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" -) - -func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) { - const op = "adminkindboxreqvalidator.ValidateAddRequest" - - if err := validation.ValidateStruct(&req, - - validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), - - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.TypeID, - validation.Required, - validation.By(v.doesTypeExist)), - ); err != nil { - fieldErrors := make(map[string]string) - - var errV validation.Errors - if errors.As(err, &errV) { - for key, value := range errV { - if value != nil { - fieldErrors[key] = value.Error() - } - } - } - - return fieldErrors, richerror.New(op). - WithMessage(errmsg.ErrorMsgInvalidInput). - WithKind(richerror.KindInvalid). - WithMeta(map[string]interface{}{"req": req}). - WithErr(err) - } - - return map[string]string{}, nil -} +// import ( +// "errors" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// 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" +//) +// +// func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[string]string, error) { +// const op = "adminkindboxreqvalidator.ValidateAddRequest" +// +// if err := validation.ValidateStruct(&req, +// +// validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), +// +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.TypeID, +// validation.Required, +// validation.By(v.doesTypeExist)), +// ); err != nil { +// fieldErrors := make(map[string]string) +// +// var errV validation.Errors +// if errors.As(err, &errV) { +// for key, value := range errV { +// if value != nil { +// fieldErrors[key] = value.Error() +// } +// } +// } +// +// return fieldErrors, richerror.New(op). +// WithMessage(errmsg.ErrorMsgInvalidInput). +// WithKind(richerror.KindInvalid). +// WithMeta(map[string]interface{}{"req": req}). +// WithErr(err) +// } +// +// return map[string]string{}, nil +//} diff --git a/validator/admin/kind_box_req/delete.go b/validator/admin/kind_box_req/delete.go index de63922..72dea66 100644 --- a/validator/admin/kind_box_req/delete.go +++ b/validator/admin/kind_box_req/delete.go @@ -1,45 +1,45 @@ package adminkindboxreqvalidator -import ( - "errors" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - 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" -) - -func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) { - const op = "adminkindboxreqvalidator.ValidateDeleteRequest" - - if err := validation.ValidateStruct(&req, - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.KindBoxReqID, - validation.Required, - validation.By(v.hasPendingStatus), - validation.By(v.doesKindBoxRequestExist), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), - ); err != nil { - fieldErrors := make(map[string]string) - - var errV validation.Errors - if errors.As(err, &errV) { - for key, value := range errV { - if value != nil { - fieldErrors[key] = value.Error() - } - } - } - - return fieldErrors, richerror.New(op). - WithMessage(errmsg.ErrorMsgInvalidInput). - WithKind(richerror.KindInvalid). - WithMeta(map[string]interface{}{"req": req}). - WithErr(err) - } - - return map[string]string{}, nil -} +// import ( +// "errors" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// 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" +//) +// +// func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map[string]string, error) { +// const op = "adminkindboxreqvalidator.ValidateDeleteRequest" +// +// if err := validation.ValidateStruct(&req, +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.KindBoxReqID, +// validation.Required, +// validation.By(v.hasPendingStatus), +// validation.By(v.doesKindBoxRequestExist), +// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), +// ); err != nil { +// fieldErrors := make(map[string]string) +// +// var errV validation.Errors +// if errors.As(err, &errV) { +// for key, value := range errV { +// if value != nil { +// fieldErrors[key] = value.Error() +// } +// } +// } +// +// return fieldErrors, richerror.New(op). +// WithMessage(errmsg.ErrorMsgInvalidInput). +// WithKind(richerror.KindInvalid). +// WithMeta(map[string]interface{}{"req": req}). +// WithErr(err) +// } +// +// return map[string]string{}, nil +//} diff --git a/validator/admin/kind_box_req/get.go b/validator/admin/kind_box_req/get.go index c85f43e..1ffd8a6 100644 --- a/validator/admin/kind_box_req/get.go +++ b/validator/admin/kind_box_req/get.go @@ -1,44 +1,44 @@ package adminkindboxreqvalidator -import ( - "errors" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - 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" -) - -func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[string]string, error) { - const op = "adminkindboxreqvalidator.ValidateGetRequest" - - if err := validation.ValidateStruct(&req, - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.KindBoxReqID, - validation.Required, - validation.By(v.doesKindBoxRequestExist), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), - ); err != nil { - fieldErrors := make(map[string]string) - - var errV validation.Errors - if errors.As(err, &errV) { - for key, value := range errV { - if value != nil { - fieldErrors[key] = value.Error() - } - } - } - - return fieldErrors, richerror.New(op). - WithMessage(errmsg.ErrorMsgInvalidInput). - WithKind(richerror.KindInvalid). - WithMeta(map[string]interface{}{"req": req}). - WithErr(err) - } - - return map[string]string{}, nil -} +// import ( +// "errors" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// 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" +//) +// +// func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[string]string, error) { +// const op = "adminkindboxreqvalidator.ValidateGetRequest" +// +// if err := validation.ValidateStruct(&req, +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.KindBoxReqID, +// validation.Required, +// validation.By(v.doesKindBoxRequestExist), +// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), +// ); err != nil { +// fieldErrors := make(map[string]string) +// +// var errV validation.Errors +// if errors.As(err, &errV) { +// for key, value := range errV { +// if value != nil { +// fieldErrors[key] = value.Error() +// } +// } +// } +// +// return fieldErrors, richerror.New(op). +// WithMessage(errmsg.ErrorMsgInvalidInput). +// WithKind(richerror.KindInvalid). +// WithMeta(map[string]interface{}{"req": req}). +// WithErr(err) +// } +// +// return map[string]string{}, nil +//} diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go index 9559899..90ba01f 100644 --- a/validator/admin/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -1,50 +1,50 @@ package adminkindboxreqvalidator -import ( - "errors" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" - 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" -) - -func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) { - const op = "adminkindboxreqvalidator.ValidateUpdateRequest" - - if err := validation.ValidateStruct(&req, - validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), - - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.KindBoxReqID, - validation.Required, - validation.By(v.doesKindBoxRequestExist), - validation.By(v.hasPendingStatus), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), - - validation.Field(&req.TypeID, - validation.By(v.doesTypeExist)), - ); err != nil { - fieldErrors := make(map[string]string) - - var errV validation.Errors - if errors.As(err, &errV) { - for key, value := range errV { - if value != nil { - fieldErrors[key] = value.Error() - } - } - } - - return fieldErrors, richerror.New(op). - WithMessage(errmsg.ErrorMsgInvalidInput). - WithKind(richerror.KindInvalid). - WithMeta(map[string]interface{}{"req": req}). - WithErr(err) - } - - return map[string]string{}, nil -} +// import ( +// "errors" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" +// 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" +//) +// +// func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) { +// const op = "adminkindboxreqvalidator.ValidateUpdateRequest" +// +// if err := validation.ValidateStruct(&req, +// validation.Field(&req.CountRequested, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), +// +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.KindBoxReqID, +// validation.Required, +// validation.By(v.doesKindBoxRequestExist), +// validation.By(v.hasPendingStatus), +// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), +// +// validation.Field(&req.TypeID, +// validation.By(v.doesTypeExist)), +// ); err != nil { +// fieldErrors := make(map[string]string) +// +// var errV validation.Errors +// if errors.As(err, &errV) { +// for key, value := range errV { +// if value != nil { +// fieldErrors[key] = value.Error() +// } +// } +// } +// +// return fieldErrors, richerror.New(op). +// WithMessage(errmsg.ErrorMsgInvalidInput). +// WithKind(richerror.KindInvalid). +// WithMeta(map[string]interface{}{"req": req}). +// WithErr(err) +// } +// +// return map[string]string{}, nil +//} diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 83023f5..d734d7f 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -1,23 +1,16 @@ package adminkindboxreqvalidator -import ( - "fmt" - - errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" - validation "github.com/go-ozzo/ozzo-validation/v4" -) - const ( MinKindBoxReq = 1 MaxKindBoxReq = 100 ) type Repository interface { - BenefactorExist(id int) (bool, error) - KindBoxRequestExist(id int) (bool, error) - TypeExist(id int) (bool, error) - KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) - PendingStatus(id uint) (bool, error) + // BenefactorExist(id int) (bool, error) + // KindBoxRequestExist(id int) (bool, error) + // TypeExist(id int) (bool, error) + // KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) + // PendingStatus(id uint) (bool, error) } type Validator struct { @@ -28,69 +21,70 @@ func New(repo Repository) Validator { return Validator{repo: repo} } -func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID, ok := value.(int) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - _, err := v.repo.BenefactorExist(benefactorID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} - -func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { - return func(value interface{}) error { - kbID, ok := value.(uint) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil - } -} - -func (v Validator) doesKindBoxRequestExist(value interface{}) error { - kindboxreqID, ok := value.(int) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - _, err := v.repo.KindBoxRequestExist(kindboxreqID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} - -func (v Validator) doesTypeExist(value interface{}) error { - typeID, ok := value.(int) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - _, err := v.repo.TypeExist(typeID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} - -func (v Validator) hasPendingStatus(value interface{}) error { - kindboxID, ok := value.(uint) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - _, err := v.repo.PendingStatus(kindboxID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} +// +// func (v Validator) doesBenefactorExist(value interface{}) error { +// benefactorID, ok := value.(int) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.BenefactorExist(benefactorID) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// +// return nil +//} +// +// func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { +// return func(value interface{}) error { +// kbID, ok := value.(uint) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// +// return nil +// } +//} +// +// func (v Validator) doesKindBoxRequestExist(value interface{}) error { +// kindboxreqID, ok := value.(int) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.KindBoxRequestExist(kindboxreqID) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// +// return nil +//} +// +// func (v Validator) doesTypeExist(value interface{}) error { +// typeID, ok := value.(int) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.TypeExist(typeID) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// +// return nil +//} +// +//func (v Validator) hasPendingStatus(value interface{}) error { +// kindboxID, ok := value.(uint) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.PendingStatus(kindboxID) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// +// return nil +//}