From e3109b197285b3d45293fa6a61d36b4ec9299300 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Mon, 22 Jan 2024 11:44:57 +0330 Subject: [PATCH 01/17] 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 +//} From 12a8e0ecf5b8983ee963bb81977e3cc7ce99d8c4 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Mon, 22 Jan 2024 14:13:16 +0330 Subject: [PATCH 02/17] minor change & delete description field from param.KindBoxReqAcceptRequest --- delivery/http_server/admin/kind_box_req/accept.go | 6 +++++- param/admin/kind_box_req/accept.go | 5 ++--- repository/mysql/kind_box_req/kind_box_req.go | 6 +++--- service/admin/kind_box_req/accept.go | 2 +- service/admin/kind_box_req/service.go | 2 +- service/admin/kind_box_req/update.go | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/delivery/http_server/admin/kind_box_req/accept.go b/delivery/http_server/admin/kind_box_req/accept.go index 1e3f7d4..b5f3075 100644 --- a/delivery/http_server/admin/kind_box_req/accept.go +++ b/delivery/http_server/admin/kind_box_req/accept.go @@ -1,6 +1,7 @@ package adminkindboxreqhandler import ( + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" "net/http" "strconv" @@ -14,7 +15,10 @@ func (h Handler) Accept(c echo.Context) error { if bErr := c.Bind(&req); bErr != nil { return echo.NewHTTPError(http.StatusBadRequest) } - num, _ := strconv.ParseUint(c.Param("id"), 36, 10) + num, cErr := strconv.ParseUint(c.Param("id"), 10, 64) + if cErr != nil { + return c.JSON(http.StatusBadRequest, errmsg.ErrorMsgInvalidInput) + } req.ID = uint(num) // if fieldErrors, err := h.adminKindBoxReqVld.ValidateGetRequest(req); err != nil { // msg, code := httpmsg.Error(err) diff --git a/param/admin/kind_box_req/accept.go b/param/admin/kind_box_req/accept.go index ad480bb..60573b9 100644 --- a/param/admin/kind_box_req/accept.go +++ b/param/admin/kind_box_req/accept.go @@ -7,9 +7,8 @@ import ( ) type KindBoxReqAcceptRequest struct { - ID uint `json:"id"` - Description string `json:"description"` - CountAccepted uint `json:"count_accepted"` + ID uint `json:"id"` + CountAccepted uint `json:"count_accepted"` } type KindBoxReqAcceptResponse struct { diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 88291e2..cbe7fea 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -25,15 +25,15 @@ 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 { +func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error { op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") statement, err := d.conn.Conn(). - Prepare(`update kind_box_reqs set count_accepted = ? , description = ? , status = ? where id = ?`) + Prepare(`update kind_box_reqs set count_accepted = ? , 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) + _, eErr := statement.ExecContext(ctx, countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID) if eErr != nil { return richerror.New(op).WithErr(eErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index dc8efb9..54f3960 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -8,7 +8,7 @@ import ( 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) + err := s.repo.AcceptKindBoxReq(ctx, req.ID, req.CountAccepted) if err != nil { return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 118707a..878b03e 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -12,7 +12,7 @@ type Repository interface { // 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 + AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error FindByID(ctx context.Context, id uint) (entity.KindBoxReq, error) } diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go index 72f6998..0cd9d0e 100644 --- a/service/admin/kind_box_req/update.go +++ b/service/admin/kind_box_req/update.go @@ -9,7 +9,7 @@ package adminkindboxreqservice // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" //) // -// func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { +//func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { // // TODO: can benefactor update its request ? // // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? // // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected From 5708f0c4140c2bf22a986e77f171a3afb208b5e1 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Mon, 22 Jan 2024 17:37:51 +0330 Subject: [PATCH 03/17] add validation for accepting kind-box-req --- .../http_server/admin/kind_box_req/accept.go | 19 ++-- pkg/err_msg/message.go | 1 + repository/mysql/kind_box_req/kind_box_req.go | 41 ++++++++- service/admin/kind_box_req/accept.go | 5 +- service/admin/kind_box_req/service.go | 3 +- service/admin/kind_box_req/update.go | 2 +- validator/admin/kind_box_req/accept.go | 42 +++++++++ validator/admin/kind_box_req/validator.go | 90 ++++++++++++------- 8 files changed, 155 insertions(+), 48 deletions(-) create mode 100644 validator/admin/kind_box_req/accept.go diff --git a/delivery/http_server/admin/kind_box_req/accept.go b/delivery/http_server/admin/kind_box_req/accept.go index b5f3075..85ab582 100644 --- a/delivery/http_server/admin/kind_box_req/accept.go +++ b/delivery/http_server/admin/kind_box_req/accept.go @@ -1,11 +1,11 @@ package adminkindboxreqhandler import ( - errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" "net/http" "strconv" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "github.com/labstack/echo/v4" ) @@ -20,14 +20,15 @@ func (h Handler) Accept(c echo.Context) error { return c.JSON(http.StatusBadRequest, errmsg.ErrorMsgInvalidInput) } 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, - // }) - //} + if fieldErrors, err := h.adminKindBoxReqVld.ValidateAcceptRequest(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) diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index f716923..a7744c4 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -14,4 +14,5 @@ const ( ErrorMsgOtpCodeIsNotValid = "verification code is not valid" ErrorMsgCantScanQueryResult = "can't scan query result" ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" + ErrorMsgReAcceptKindBoxReq = "accepted request cannot be re-accepted" ) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index cbe7fea..ae937a8 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -2,6 +2,9 @@ package mysqlkindboxreq import ( "context" + "database/sql" + "errors" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -29,6 +32,7 @@ func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccept op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`) + defer statement.Close() if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) @@ -42,13 +46,46 @@ func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccept return nil } -func (d DB) FindByID(ctx context.Context, id uint) (entity.KindBoxReq, error) { - op := richerror.Op("mysqlkindboxreq.findByID") +func (d DB) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) { + op := richerror.Op("mysqlkindboxreq.GetByID") 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 } + +func (d DB) KindBoxRequestExist(id uint) (bool, error) { + op := richerror.Op("mysqlkindboxreq.KindBoxRequestExist") + row := d.conn.Conn().QueryRow(`select * from kind_box_reqs where id = ?`, id) + _, sErr := scanKindBoxReq(row) + if sErr != nil { + if errors.Is(sErr, sql.ErrNoRows) { + return false, nil + } + + return false, richerror.New(op).WithErr(sErr).WithMessage(errmsg.ErrorMsgCantScanQueryResult). + WithKind(richerror.KindUnexpected).WithMeta(map[string]any{"id": id}) + } + + return true, nil +} + +func (d DB) CheckKindBoxReqStatusForAccepting(id uint) error { + op := richerror.Op("mysqlkindboxreq.CheckKindBoxReqStatusForAccepting") + + k, err := d.GetByID(context.Background(), id) + if err != nil { + return richerror.New(op).WithErr(err) + } + if k.Status == entity.KindBoxReqAcceptedStatus { + return richerror.New(op).WithMessage(errmsg.ErrorMsgReAcceptKindBoxReq).WithMeta(map[string]interface{}{ + "id": id, + }).WithKind(richerror.KindInvalid) + } + + return nil +} diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index 54f3960..f25116e 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -2,6 +2,7 @@ package adminkindboxreqservice import ( "context" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -14,9 +15,9 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) } // fire new event to create a kind-box. - //get kind box req + // get kind box req - kindBoxReq, gErr := s.repo.FindByID(ctx, req.ID) + kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 878b03e..077c203 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -2,6 +2,7 @@ package adminkindboxreqservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" ) @@ -13,7 +14,7 @@ type Repository interface { // GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) // GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error - FindByID(ctx context.Context, id uint) (entity.KindBoxReq, error) + GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) } // TODO: check validation. diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go index 0cd9d0e..72f6998 100644 --- a/service/admin/kind_box_req/update.go +++ b/service/admin/kind_box_req/update.go @@ -9,7 +9,7 @@ package adminkindboxreqservice // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" //) // -//func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { +// func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { // // TODO: can benefactor update its request ? // // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? // // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected diff --git a/validator/admin/kind_box_req/accept.go b/validator/admin/kind_box_req/accept.go new file mode 100644 index 0000000..d41a40d --- /dev/null +++ b/validator/admin/kind_box_req/accept.go @@ -0,0 +1,42 @@ +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) ValidateAcceptRequest(req param.KindBoxReqAcceptRequest) (map[string]string, error) { + const op = "adminkindboxreqvalidator.ValidateAcceptRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.checkKindBoxReqStatus)), + + validation.Field(&req.CountAccepted, + validation.Required, + validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq)), + ), + ); 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 d734d7f..515e34c 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -1,5 +1,12 @@ package adminkindboxreqvalidator +import ( + "errors" + "fmt" + + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" +) + const ( MinKindBoxReq = 1 MaxKindBoxReq = 100 @@ -7,10 +14,11 @@ const ( type Repository interface { // BenefactorExist(id int) (bool, error) - // KindBoxRequestExist(id int) (bool, error) + KindBoxRequestExist(id uint) (bool, error) // TypeExist(id int) (bool, error) // KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) // PendingStatus(id uint) (bool, error) + CheckKindBoxReqStatusForAccepting(id uint) error } type Validator struct { @@ -21,47 +29,63 @@ 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) +// func (v Validator) doesBenefactorExist(value interface{}) error { +// benefactorID, ok := value.(int) // if !ok { -// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) // } -// _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) +// _, err := v.repo.BenefactorExist(benefactorID) // 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) -// } +// 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 -//} +// return nil +// } +// } +func (v Validator) doesKindBoxRequestExist(value interface{}) error { + kindboxreqID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if isExist, err := v.repo.KindBoxRequestExist(kindboxreqID); !isExist || err != nil { + if err != nil { + return err + } + if !isExist { + return errors.New("kind box request is not exist") + } + } + + return nil +} + +func (v Validator) checkKindBoxReqStatus(value interface{}) error { + kindboxreqID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + err := v.repo.CheckKindBoxReqStatusForAccepting(kindboxreqID) + if err != nil { + return err + } + + return nil +} + // // func (v Validator) doesTypeExist(value interface{}) error { // typeID, ok := value.(int) @@ -76,7 +100,7 @@ func New(repo Repository) Validator { // return nil //} // -//func (v Validator) hasPendingStatus(value interface{}) error { +// func (v Validator) hasPendingStatus(value interface{}) error { // kindboxID, ok := value.(uint) // if !ok { // return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) From 1823191c4e3dafa365fbe9c93230c27f8b219c07 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Mon, 22 Jan 2024 18:51:13 +0330 Subject: [PATCH 04/17] rejecting kind-box-request --- .../http_server/admin/kind_box_req/reject.go | 40 ++++++++++++++++++ .../http_server/admin/kind_box_req/route.go | 1 + param/admin/kind_box_req/reject.go | 24 +++++++++++ pkg/err_msg/message.go | 1 + repository/mysql/kind_box_req/kind_box_req.go | 35 +++++++++++++++- repository/mysql/kind_box_req/scan.go | 1 + service/admin/kind_box_req/reject.go | 36 ++++++++++++++++ service/admin/kind_box_req/service.go | 1 + validator/admin/kind_box_req/accept.go | 2 +- validator/admin/kind_box_req/reject.go | 41 +++++++++++++++++++ validator/admin/kind_box_req/validator.go | 16 +++++++- 11 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 delivery/http_server/admin/kind_box_req/reject.go create mode 100644 param/admin/kind_box_req/reject.go create mode 100644 service/admin/kind_box_req/reject.go create mode 100644 validator/admin/kind_box_req/reject.go diff --git a/delivery/http_server/admin/kind_box_req/reject.go b/delivery/http_server/admin/kind_box_req/reject.go new file mode 100644 index 0000000..2aa1bd4 --- /dev/null +++ b/delivery/http_server/admin/kind_box_req/reject.go @@ -0,0 +1,40 @@ +package adminkindboxreqhandler + +import ( + "net/http" + "strconv" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + "github.com/labstack/echo/v4" +) + +func (h Handler) Reject(c echo.Context) error { + var req param.KindBoxReqRejectRequest + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + num, cErr := strconv.ParseUint(c.Param("id"), 10, 64) + if cErr != nil { + return c.JSON(http.StatusBadRequest, errmsg.ErrorMsgInvalidInput) + } + req.ID = uint(num) + if fieldErrors, err := h.adminKindBoxReqVld.ValidateRejectRequest(req); err != nil { + msg, code := httpmsg.Error(err) + + return c.JSON(code, echo.Map{ + "message": msg, + "errors": fieldErrors, + }) + } + + resp, sErr := h.adminKindBoxReqSvc.Reject(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 60ff249..c0bcc53 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -14,4 +14,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { //nolint:gocritic //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" r.PATCH("/accept-kind-box-req/:id", h.Accept) + r.PATCH("/reject-kind-box-req/:id", h.Reject) } diff --git a/param/admin/kind_box_req/reject.go b/param/admin/kind_box_req/reject.go new file mode 100644 index 0000000..56119b1 --- /dev/null +++ b/param/admin/kind_box_req/reject.go @@ -0,0 +1,24 @@ +package adminkindboxreqparam + +import ( + "time" + + "git.gocasts.ir/ebhomengo/niki/entity" +) + +type KindBoxReqRejectRequest struct { + ID uint `json:"id"` + Description string `json:"description"` +} + +type KindBoxReqRejectResponse 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/pkg/err_msg/message.go b/pkg/err_msg/message.go index a7744c4..5d24ce5 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -15,4 +15,5 @@ const ( ErrorMsgCantScanQueryResult = "can't scan query result" ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" ErrorMsgReAcceptKindBoxReq = "accepted request cannot be re-accepted" + ErrorMsgReRejectKindBoxReq = "rejected request cannot be re-rejected" ) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index ae937a8..b0f8e15 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -28,7 +28,7 @@ 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) error { +func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error { op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`) @@ -89,3 +89,36 @@ func (d DB) CheckKindBoxReqStatusForAccepting(id uint) error { return nil } + +func (d DB) CheckKindBoxReqStatusForRejecting(id uint) error { + op := richerror.Op("mysqlkindboxreq.CheckKindBoxReqStatusForRejecting") + k, err := d.GetByID(context.Background(), id) + if err != nil { + return richerror.New(op).WithErr(err) + } + if k.Status == entity.KindBoxReqRejectedStatus { + return richerror.New(op).WithMessage(errmsg.ErrorMsgReRejectKindBoxReq).WithMeta(map[string]interface{}{ + "id": id, + }).WithKind(richerror.KindInvalid) + } + + return nil +} + +func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error { + op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") + statement, err := d.conn.Conn(). + Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`) + defer statement.Close() + if err != nil { + return richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + _, eErr := statement.ExecContext(ctx, description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID) + if eErr != nil { + return richerror.New(op).WithErr(eErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return nil +} diff --git a/repository/mysql/kind_box_req/scan.go b/repository/mysql/kind_box_req/scan.go index 8cda054..5a165d5 100644 --- a/repository/mysql/kind_box_req/scan.go +++ b/repository/mysql/kind_box_req/scan.go @@ -15,5 +15,6 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) { kindBoxReq.Status = entity.MapToKindBoxReqStatus(kindBoxStatus) kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType) + return kindBoxReq, err } diff --git a/service/admin/kind_box_req/reject.go b/service/admin/kind_box_req/reject.go new file mode 100644 index 0000000..1734bca --- /dev/null +++ b/service/admin/kind_box_req/reject.go @@ -0,0 +1,36 @@ +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) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) (param.KindBoxReqRejectResponse, error) { + const op = "adminkindboxreqservice.Reject" + err := s.repo.RejectKindBoxReq(ctx, req.ID, req.Description) + if err != nil { + return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err) + } + + // fire new event to create a kind-box. + // get kind box req + + kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) + if gErr != nil { + return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err) + } + + return param.KindBoxReqRejectResponse{ + 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/service.go b/service/admin/kind_box_req/service.go index 077c203..80a415b 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -15,6 +15,7 @@ type Repository interface { // GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) + RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error } // TODO: check validation. diff --git a/validator/admin/kind_box_req/accept.go b/validator/admin/kind_box_req/accept.go index d41a40d..86ea377 100644 --- a/validator/admin/kind_box_req/accept.go +++ b/validator/admin/kind_box_req/accept.go @@ -13,7 +13,7 @@ func (v Validator) ValidateAcceptRequest(req param.KindBoxReqAcceptRequest) (map const op = "adminkindboxreqvalidator.ValidateAcceptRequest" if err := validation.ValidateStruct(&req, - validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.checkKindBoxReqStatus)), + validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.CheckKindBoxReqStatusForAccepting)), validation.Field(&req.CountAccepted, validation.Required, diff --git a/validator/admin/kind_box_req/reject.go b/validator/admin/kind_box_req/reject.go new file mode 100644 index 0000000..2822223 --- /dev/null +++ b/validator/admin/kind_box_req/reject.go @@ -0,0 +1,41 @@ +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) ValidateRejectRequest(req param.KindBoxReqRejectRequest) (map[string]string, error) { + const op = "adminkindboxreqvalidator.ValidateRejectRequest" + + if err := validation.ValidateStruct(&req, + validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.CheckKindBoxReqStatusForRejecting)), + + validation.Field(&req.Description, + validation.Required, + ), + ); 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 515e34c..75d54db 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -19,6 +19,7 @@ type Repository interface { // KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) // PendingStatus(id uint) (bool, error) CheckKindBoxReqStatusForAccepting(id uint) error + CheckKindBoxReqStatusForRejecting(id uint) error } type Validator struct { @@ -73,7 +74,7 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error { return nil } -func (v Validator) checkKindBoxReqStatus(value interface{}) error { +func (v Validator) CheckKindBoxReqStatusForAccepting(value interface{}) error { kindboxreqID, ok := value.(uint) if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) @@ -86,6 +87,19 @@ func (v Validator) checkKindBoxReqStatus(value interface{}) error { return nil } +func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error { + kindboxreqID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + err := v.repo.CheckKindBoxReqStatusForRejecting(kindboxreqID) + if err != nil { + return err + } + + return nil +} + // // func (v Validator) doesTypeExist(value interface{}) error { // typeID, ok := value.(int) From dc9d9312275ac82139ed29f34bd7f313c3452498 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Tue, 23 Jan 2024 11:09:58 +0330 Subject: [PATCH 05/17] add kind box migration and service --- .../http_server/admin/kind_box_req/route.go | 1 + entity/kind_box.go | 9 ++-- param/admin/kind_box/add.go | 2 - param/admin/kind_box_req/accept.go | 24 +++++---- repository/mysql/kind_box/kind_box.go | 9 ++++ .../1705992494_add_kind_boxes_table.sql | 24 +++++++++ service/admin/kind_box/add.go | 2 - service/admin/kind_box/delete.go | 36 ++++++------- service/admin/kind_box/get.go | 34 ++++++------ service/admin/kind_box/get_all.go | 32 +++++------ service/admin/kind_box/service.go | 8 +-- service/admin/kind_box/update.go | 54 +++++++++---------- service/admin/kind_box_req/accept.go | 33 +++++++----- service/admin/kind_box_req/service.go | 4 +- 14 files changed, 158 insertions(+), 114 deletions(-) create mode 100644 repository/mysql/migration/1705992494_add_kind_boxes_table.sql diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index c0bcc53..578dd01 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -13,6 +13,7 @@ func (h Handler) SetRoutes(e *echo.Echo) { //r.GET("/", h.GetAll).Name = "admin-getallkindboxreq" //nolint:gocritic //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" + // todo - add acl r.PATCH("/accept-kind-box-req/:id", h.Accept) r.PATCH("/reject-kind-box-req/:id", h.Reject) } diff --git a/entity/kind_box.go b/entity/kind_box.go index 6656fbf..69d3bb8 100644 --- a/entity/kind_box.go +++ b/entity/kind_box.go @@ -4,12 +4,13 @@ import "time" type KindBox struct { ID uint - KindBoxReqID uint // TODO like database model - BenefactorID uint // TODO need in business entity + KindBoxReqID uint + BenefactorID uint + Type KindBoxType TotalAmount uint - ReceiverID uint - SenderID uint SerialNumber string Status KindBoxStatus + SenderID uint + ReceiverID uint StatusChangedAt time.Time } diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index d1a2705..408e55c 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -5,8 +5,6 @@ import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxAddRequest struct { BenefactorID uint KindBoxReqID uint - SenderID uint - SerialNumber string } type KindBoxAddResponse struct { diff --git a/param/admin/kind_box_req/accept.go b/param/admin/kind_box_req/accept.go index 60573b9..01b42d1 100644 --- a/param/admin/kind_box_req/accept.go +++ b/param/admin/kind_box_req/accept.go @@ -1,9 +1,8 @@ package adminkindboxreqparam import ( - "time" - "git.gocasts.ir/ebhomengo/niki/entity" + "time" ) type KindBoxReqAcceptRequest struct { @@ -12,13 +11,16 @@ type KindBoxReqAcceptRequest struct { } 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"` + KindBoxReqID uint `json:"kind_box_req_id"` + KindBoxReqStatus entity.KindBoxReqStatus `json:"kind_box_req_status"` + CountRequested uint `json:"count_requested"` + CountAccepted uint `json:"count_accepted"` + ReferDate time.Time `json:"refer_date"` + AddressID uint `json:"address_id"` + + KindBoxID uint `json:"kind_box_id"` + KindBoxType entity.KindBoxType `json:"kind_box_type"` + BenefactorID uint `json:"benefactor_id"` + KindBoxStatus entity.KindBoxStatus `json:"kind_box_status"` + KindBoxSerialNumber string `json:"kind_box_serial_number"` } diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index 6dcf146..aae17ef 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -1 +1,10 @@ package mysqlkindbox + +import ( + "context" + "git.gocasts.ir/ebhomengo/niki/entity" +) + +func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) { + return entity.KindBox{}, nil +} diff --git a/repository/mysql/migration/1705992494_add_kind_boxes_table.sql b/repository/mysql/migration/1705992494_add_kind_boxes_table.sql new file mode 100644 index 0000000..8f7fd48 --- /dev/null +++ b/repository/mysql/migration/1705992494_add_kind_boxes_table.sql @@ -0,0 +1,24 @@ +-- +migrate Up +CREATE TABLE `kind_boxes` +( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `kind_box_req_id` INT NOT NULL, + `benefactor_id` INT NOT NULL, + `type` ENUM ('on-table','cylindrical','stand-up') NOT NULL, + `total_amount` INT UNSIGNED NULL NULL, + `serial_number` varchar(255), + `status` varchar(255), + `sender_id` INT NULL, + `receiver_id` INT NULL, + `status_changed_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`kind_box_req_id`) REFERENCES `kind_box_reqs` (`id`), + FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`), + FOREIGN KEY (`sender_id`) REFERENCES `admins` (`id`), + FOREIGN KEY (`receiver_id`) REFERENCES `admins` (`id`), + index `index_serial_number` (`serial_number`) + +); + +-- +migrate Down +DROP TABLE `kind_boxes`; \ No newline at end of file diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index bff284e..98390a2 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -14,8 +14,6 @@ func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.Ki kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ BenefactorID: req.BenefactorID, KindBoxReqID: req.KindBoxReqID, - SenderID: req.SenderID, - SerialNumber: req.SerialNumber, Status: entity.KindBoxPendingSendStatus, }) if err != nil { diff --git a/service/admin/kind_box/delete.go b/service/admin/kind_box/delete.go index b4f04ef..f71270a 100644 --- a/service/admin/kind_box/delete.go +++ b/service/admin/kind_box/delete.go @@ -1,20 +1,20 @@ package adminkindboxservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) { - // TODO: Does business domain need to delete an kindbox ? - const op = "adminkindboxservice.Delete" - - dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID) - if dErr != nil { - return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxDeleteResponse{}, nil -} +//import ( +// "context" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +//func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) { +// // TODO: Does business domain need to delete an kindbox ? +// const op = "adminkindboxservice.Delete" +// +// dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID) +// if dErr != nil { +// return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxDeleteResponse{}, nil +//} diff --git a/service/admin/kind_box/get.go b/service/admin/kind_box/get.go index c0a80f0..0bfca1d 100644 --- a/service/admin/kind_box/get.go +++ b/service/admin/kind_box/get.go @@ -1,19 +1,19 @@ package adminkindboxservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) { - const op = "adminkindboxservice.Get" - - kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID) - if err != nil { - return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxGetResponse{KindBox: kindBox}, nil -} +//import ( +// "context" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +//func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) { +// const op = "adminkindboxservice.Get" +// +// kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID) +// if err != nil { +// return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxGetResponse{KindBox: kindBox}, nil +//} diff --git a/service/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go index 4030d34..9832357 100644 --- a/service/admin/kind_box/get_all.go +++ b/service/admin/kind_box/get_all.go @@ -1,18 +1,18 @@ package adminkindboxservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { - const op = "adminkindboxservice.GetAll" - allKindBox, err := s.repo.GetAllKindBox(ctx) - if err != nil { - return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil -} +//import ( +// "context" +// +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +//func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { +// const op = "adminkindboxservice.GetAll" +// allKindBox, err := s.repo.GetAllKindBox(ctx) +// if err != nil { +// return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil +//} diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index 842a171..b49506d 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -8,10 +8,10 @@ import ( type Repository interface { AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) - UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) - DeleteKindBox(ctx context.Context, kindBoxID uint) error - GetAllKindBox(ctx context.Context) ([]entity.KindBox, error) - GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) + //UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) + //DeleteKindBox(ctx context.Context, kindBoxID uint) error + //GetAllKindBox(ctx context.Context) ([]entity.KindBox, error) + //GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) } type Service struct { diff --git a/service/admin/kind_box/update.go b/service/admin/kind_box/update.go index eba5087..0778752 100644 --- a/service/admin/kind_box/update.go +++ b/service/admin/kind_box/update.go @@ -1,29 +1,29 @@ package adminkindboxservice -import ( - "context" - - entity "git.gocasts.ir/ebhomengo/niki/entity" - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { - // TODO: can benefactor update its Request ? - // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? - // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected - const op = "adminkindboxservice.Update" - - kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{ - TotalAmount: req.TotalAmount, - ReceiverID: req.ReceiverID, - SenderID: req.SenderID, - SerialNumber: req.SerialNumber, - Status: req.Status, - }) - if uErr != nil { - return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) - } - - return param.KindBoxUpdateResponse{KindBox: kindBox}, nil -} +//import ( +// "context" +// +// entity "git.gocasts.ir/ebhomengo/niki/entity" +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" +// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +//) +// +//func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { +// // TODO: can benefactor update its Request ? +// // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? +// // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected +// const op = "adminkindboxservice.Update" +// +// kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{ +// TotalAmount: req.TotalAmount, +// ReceiverID: req.ReceiverID, +// SenderID: req.SenderID, +// SerialNumber: req.SerialNumber, +// Status: req.Status, +// }) +// if uErr != nil { +// return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) +// } +// +// return param.KindBoxUpdateResponse{KindBox: kindBox}, nil +//} diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index f25116e..e3cc2bc 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -2,7 +2,7 @@ package adminkindboxreqservice import ( "context" - + adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -15,22 +15,31 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) } // fire new event to create a kind-box. - // get kind box req kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { - return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) + return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr) + } + + res, kErr := s.kindBoxSvc.Add(ctx, adminkindboxparam.KindBoxAddRequest{ + BenefactorID: kindBoxReq.BenefactorID, + KindBoxReqID: kindBoxReq.ID, + }) + if kErr != nil { + return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr) } 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, + KindBoxReqID: kindBoxReq.ID, + KindBoxReqStatus: kindBoxReq.Status, + CountRequested: kindBoxReq.CountRequested, + CountAccepted: kindBoxReq.CountAccepted, + ReferDate: kindBoxReq.ReferDate, + AddressID: kindBoxReq.AddressID, + KindBoxID: res.KindBox.ID, + KindBoxType: kindBoxReq.KindBoxType, + BenefactorID: kindBoxReq.BenefactorID, + KindBoxStatus: res.KindBox.Status, + KindBoxSerialNumber: res.KindBox.SerialNumber, }, nil } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 80a415b..172feb5 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -2,6 +2,7 @@ package adminkindboxreqservice import ( "context" + adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" "git.gocasts.ir/ebhomengo/niki/entity" ) @@ -24,7 +25,8 @@ type Repository interface { // } type Service struct { - repo Repository + repo Repository + kindBoxSvc adminkindboxservice.Service // benefactorService BenefactorService } From befdad0c50d64c67a102128b4fa53459ed97836f Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Tue, 23 Jan 2024 13:51:56 +0330 Subject: [PATCH 06/17] create kind box after accepting request --- delivery/http_server/admin/kind_box/add.go | 52 +++--- delivery/http_server/admin/kind_box/get.go | 60 +++---- .../http_server/admin/kind_box/get_all.go | 47 ++--- delivery/http_server/admin/kind_box/route.go | 10 +- delivery/http_server/admin/kind_box/update.go | 60 +++---- go.mod | 1 + go.sum | 3 + main.go | 7 +- param/admin/kind_box/add.go | 8 +- param/admin/kind_box_req/accept.go | 9 +- repository/mysql/kind_box/kind_box.go | 38 +++- service/admin/kind_box/add.go | 27 ++- service/admin/kind_box/delete.go | 4 +- service/admin/kind_box/get.go | 4 +- service/admin/kind_box/get_all.go | 4 +- service/admin/kind_box/service.go | 10 +- service/admin/kind_box/update.go | 4 +- service/admin/kind_box_req/accept.go | 22 ++- service/admin/kind_box_req/service.go | 15 +- validator/admin/kind_box/add.go | 84 ++++----- validator/admin/kind_box/delete.go | 86 ++++----- validator/admin/kind_box/get.go | 84 ++++----- validator/admin/kind_box/update.go | 114 ++++++------ validator/admin/kind_box/validator.go | 163 +++++++++--------- 24 files changed, 471 insertions(+), 445 deletions(-) diff --git a/delivery/http_server/admin/kind_box/add.go b/delivery/http_server/admin/kind_box/add.go index 49331e0..fce2ac8 100644 --- a/delivery/http_server/admin/kind_box/add.go +++ b/delivery/http_server/admin/kind_box/add.go @@ -1,32 +1,24 @@ package adminkindboxhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" - echo "github.com/labstack/echo/v4" -) - -func (h Handler) Add(c echo.Context) error { - var req param.KindBoxAddRequest - if bErr := c.Bind(&req); bErr != nil { - return echo.NewHTTPError(http.StatusBadRequest) - } - if fieldErrors, err := h.adminKindBoxVld.ValidateAddRequest(req); err != nil { - msg, code := httpmsg.Error(err) - - return c.JSON(code, echo.Map{ - "message": msg, - "errors": fieldErrors, - }) - } - resp, sErr := h.adminKindBoxSvc.Add(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) Add(c echo.Context) error { +// var req param.KindBoxAddRequest +// if bErr := c.Bind(&req); bErr != nil { +// return echo.NewHTTPError(http.StatusBadRequest) +// } +// if fieldErrors, err := h.adminKindBoxVld.ValidateAddRequest(req); err != nil { +// msg, code := httpmsg.Error(err) +// +// return c.JSON(code, echo.Map{ +// "message": msg, +// "errors": fieldErrors, +// }) +// } +// resp, sErr := h.adminKindBoxSvc.Add(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/get.go b/delivery/http_server/admin/kind_box/get.go index c902488..518abfa 100644 --- a/delivery/http_server/admin/kind_box/get.go +++ b/delivery/http_server/admin/kind_box/get.go @@ -1,32 +1,32 @@ package adminkindboxhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - 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.KindBoxGetRequest - if bErr := c.Bind(&req); bErr != nil { - return echo.NewHTTPError(http.StatusBadRequest) - } - if fieldErrors, err := h.adminKindBoxVld.ValidateGetRequest(req); err != nil { - msg, code := httpmsg.Error(err) - - return c.JSON(code, echo.Map{ - "message": msg, - "errors": fieldErrors, - }) - } - resp, sErr := h.adminKindBoxSvc.Get(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" +// 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.KindBoxGetRequest +// if bErr := c.Bind(&req); bErr != nil { +// return echo.NewHTTPError(http.StatusBadRequest) +// } +// if fieldErrors, err := h.adminKindBoxVld.ValidateGetRequest(req); err != nil { +// msg, code := httpmsg.Error(err) +// +// return c.JSON(code, echo.Map{ +// "message": msg, +// "errors": fieldErrors, +// }) +// } +// resp, sErr := h.adminKindBoxSvc.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/get_all.go b/delivery/http_server/admin/kind_box/get_all.go index b6854aa..2e04c65 100644 --- a/delivery/http_server/admin/kind_box/get_all.go +++ b/delivery/http_server/admin/kind_box/get_all.go @@ -1,25 +1,26 @@ package adminkindboxhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - 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.KindBoxGetAllRequest - if bErr := c.Bind(&req); bErr != nil { - return echo.NewHTTPError(http.StatusBadRequest) - } - - resp, sErr := h.adminKindBoxSvc.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" +// 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.KindBoxGetAllRequest +// if bErr := c.Bind(&req); bErr != nil { +// return echo.NewHTTPError(http.StatusBadRequest) +// } +// +// resp, sErr := h.adminKindBoxSvc.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/route.go b/delivery/http_server/admin/kind_box/route.go index 9540c0f..4bad945 100644 --- a/delivery/http_server/admin/kind_box/route.go +++ b/delivery/http_server/admin/kind_box/route.go @@ -5,10 +5,10 @@ import ( ) func (h Handler) SetRoutes(e *echo.Echo) { - r := e.Group("/admin/kindboxes") - r.POST("/", h.Add).Name = "admin-addkindbox" - r.GET("/:id", h.Get).Name = "admin-getkindboxbyid" - r.GET("/", h.GetAll).Name = "admin-getallkindbox" - r.PATCH("/:id", h.Update).Name = "admin-updatekindbox" + + // r.POST("/", h.Add).Name = "admin-addkindbox" + // r.GET("/:id", h.Get).Name = "admin-getkindboxbyid" + // r.GET("/", h.GetAll).Name = "admin-getallkindbox" + // r.PATCH("/:id", h.Update).Name = "admin-updatekindbox" } diff --git a/delivery/http_server/admin/kind_box/update.go b/delivery/http_server/admin/kind_box/update.go index 176e44d..0f640c6 100644 --- a/delivery/http_server/admin/kind_box/update.go +++ b/delivery/http_server/admin/kind_box/update.go @@ -1,32 +1,32 @@ package adminkindboxhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" - echo "github.com/labstack/echo/v4" -) - -func (h Handler) Update(c echo.Context) error { - var req param.KindBoxUpdateRequest - if bErr := c.Bind(&req); bErr != nil { - return echo.NewHTTPError(http.StatusBadRequest) - } - if fieldErrors, err := h.adminKindBoxVld.ValidateUpdateRequest(req); err != nil { - msg, code := httpmsg.Error(err) - - return c.JSON(code, echo.Map{ - "message": msg, - "errors": fieldErrors, - }) - } - resp, sErr := h.adminKindBoxSvc.Update(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" +// httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" +// echo "github.com/labstack/echo/v4" +//) +// +//func (h Handler) Update(c echo.Context) error { +// var req param.KindBoxUpdateRequest +// if bErr := c.Bind(&req); bErr != nil { +// return echo.NewHTTPError(http.StatusBadRequest) +// } +// if fieldErrors, err := h.adminKindBoxVld.ValidateUpdateRequest(req); err != nil { +// msg, code := httpmsg.Error(err) +// +// return c.JSON(code, echo.Map{ +// "message": msg, +// "errors": fieldErrors, +// }) +// } +// resp, sErr := h.adminKindBoxSvc.Update(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/go.mod b/go.mod index 00f6c19..2422568 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/mitchellh/copystructure v1.2.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect + github.com/oklog/ulid/v2 v2.1.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/crypto v0.17.0 // indirect diff --git a/go.sum b/go.sum index 14df7a8..f3d0b6a 100644 --- a/go.sum +++ b/go.sum @@ -228,8 +228,11 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= +github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU= +github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= +github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= diff --git a/main.go b/main.go index c5c6ace..c9b35f8 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,11 @@ import ( mysqladdress "git.gocasts.ir/ebhomengo/niki/repository/mysql/address" mysqladmin "git.gocasts.ir/ebhomengo/niki/repository/mysql/admin" mysqlbenefactor "git.gocasts.ir/ebhomengo/niki/repository/mysql/benefactor" + mysqlkindbox "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box" 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" + adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" 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" @@ -66,8 +68,9 @@ func setupServices(cfg config.Config) ( benefactorKindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo) benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql) benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc) - - adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql) + mysqlkindboxRepo := mysqlkindbox.New(MysqlRepo) + adminkindboxsvc := adminkindboxservice.New(mysqlkindboxRepo) + adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql, adminkindboxsvc) adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql) adminAuthSvc = adminauthservice.New(cfg.AdminAuth) diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index 408e55c..4ba91ff 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -2,11 +2,13 @@ package adminkindboxparam import entity "git.gocasts.ir/ebhomengo/niki/entity" -type KindBoxAddRequest struct { +type KindBoxAddAfterAcceptingReqRequest struct { BenefactorID uint KindBoxReqID uint + Type entity.KindBoxType + Count uint } -type KindBoxAddResponse struct { - KindBox entity.KindBox +type KindBoxAddAfterAcceptingReqResponse struct { + KindBoxes []entity.KindBox } diff --git a/param/admin/kind_box_req/accept.go b/param/admin/kind_box_req/accept.go index 01b42d1..f3b8746 100644 --- a/param/admin/kind_box_req/accept.go +++ b/param/admin/kind_box_req/accept.go @@ -1,8 +1,9 @@ package adminkindboxreqparam import ( - "git.gocasts.ir/ebhomengo/niki/entity" "time" + + "git.gocasts.ir/ebhomengo/niki/entity" ) type KindBoxReqAcceptRequest struct { @@ -17,10 +18,4 @@ type KindBoxReqAcceptResponse struct { CountAccepted uint `json:"count_accepted"` ReferDate time.Time `json:"refer_date"` AddressID uint `json:"address_id"` - - KindBoxID uint `json:"kind_box_id"` - KindBoxType entity.KindBoxType `json:"kind_box_type"` - BenefactorID uint `json:"benefactor_id"` - KindBoxStatus entity.KindBoxStatus `json:"kind_box_status"` - KindBoxSerialNumber string `json:"kind_box_serial_number"` } diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index aae17ef..bb1de97 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,9 +2,43 @@ package mysqlkindbox 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" ) -func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) { - return entity.KindBox{}, nil +func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) { + const op = "mysqlkindbox.AddBatchKindBox" + tx, tErr := d.conn.Conn().Begin() + if tErr != nil { + return nil, richerror.New(op).WithErr(tErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + for _, kindBox := range kindBoxes { + res, err := tx. + ExecContext(ctx, + "insert into kind_boxes (kind_box_req_id , benefactor_id , type ,serial_number , status) values (? , ? , ? , ? ,?);", + kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.Type.String(), kindBox.SerialNumber, kindBox.Status.String(), + ) + if err != nil { + if rErr := tx.Rollback(); rErr != nil { + return nil, richerror.New(op).WithErr(rErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return nil, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + //nolint + // err is always nil + id, _ := res.LastInsertId() + kindBox.ID = uint(id) + } + if cErr := tx.Commit(); cErr != nil { + return nil, richerror.New(op).WithErr(cErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + return kindBoxes, nil } diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index 98390a2..9d59495 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -6,19 +6,28 @@ import ( entity "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" + "github.com/oklog/ulid/v2" ) -func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { - const op = "adminkindboxservice.Add" +func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param.KindBoxAddAfterAcceptingReqRequest) (param.KindBoxAddAfterAcceptingReqResponse, error) { + const op = "adminkindboxservice.AddKindBoxAfterAcceptingRequest" - kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ - BenefactorID: req.BenefactorID, - KindBoxReqID: req.KindBoxReqID, - Status: entity.KindBoxPendingSendStatus, - }) + var kindBoxes []entity.KindBox + for i := 0; i < int(req.Count); i++ { + kindBoxes = append(kindBoxes, entity.KindBox{ + KindBoxReqID: req.KindBoxReqID, + BenefactorID: req.BenefactorID, + Type: req.Type, + Status: entity.KindBoxPendingSendStatus, + SerialNumber: ulid.Make().String(), + }) + } + kindBoxes, err := s.repo.AddBatchKindBox(ctx, kindBoxes) if err != nil { - return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - return param.KindBoxAddResponse{KindBox: kindBox}, nil + return param.KindBoxAddAfterAcceptingReqResponse{ + KindBoxes: kindBoxes, + }, nil } diff --git a/service/admin/kind_box/delete.go b/service/admin/kind_box/delete.go index f71270a..316d7c2 100644 --- a/service/admin/kind_box/delete.go +++ b/service/admin/kind_box/delete.go @@ -1,13 +1,13 @@ package adminkindboxservice -//import ( +// import ( // "context" // // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" //) // -//func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) { +// func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) { // // TODO: Does business domain need to delete an kindbox ? // const op = "adminkindboxservice.Delete" // diff --git a/service/admin/kind_box/get.go b/service/admin/kind_box/get.go index 0bfca1d..b86a61a 100644 --- a/service/admin/kind_box/get.go +++ b/service/admin/kind_box/get.go @@ -1,13 +1,13 @@ package adminkindboxservice -//import ( +// import ( // "context" // // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" //) // -//func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) { +// func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) { // const op = "adminkindboxservice.Get" // // kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID) diff --git a/service/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go index 9832357..6f0cdc4 100644 --- a/service/admin/kind_box/get_all.go +++ b/service/admin/kind_box/get_all.go @@ -1,13 +1,13 @@ package adminkindboxservice -//import ( +// import ( // "context" // // param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" //) // -//func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { +// func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { // const op = "adminkindboxservice.GetAll" // allKindBox, err := s.repo.GetAllKindBox(ctx) // if err != nil { diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index b49506d..c11956a 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -7,11 +7,11 @@ import ( ) type Repository interface { - AddKindBox(ctx context.Context, kindBox entity.KindBox) (entity.KindBox, error) - //UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) - //DeleteKindBox(ctx context.Context, kindBoxID uint) error - //GetAllKindBox(ctx context.Context) ([]entity.KindBox, error) - //GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) + AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) + // UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) + // DeleteKindBox(ctx context.Context, kindBoxID uint) error + // GetAllKindBox(ctx context.Context) ([]entity.KindBox, error) + // GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) } type Service struct { diff --git a/service/admin/kind_box/update.go b/service/admin/kind_box/update.go index 0778752..1a21d1a 100644 --- a/service/admin/kind_box/update.go +++ b/service/admin/kind_box/update.go @@ -1,6 +1,6 @@ package adminkindboxservice -//import ( +// import ( // "context" // // entity "git.gocasts.ir/ebhomengo/niki/entity" @@ -8,7 +8,7 @@ package adminkindboxservice // richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" //) // -//func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { +// func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { // // TODO: can benefactor update its Request ? // // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? // // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index e3cc2bc..12c2c5e 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -2,6 +2,7 @@ package adminkindboxreqservice import ( "context" + adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -21,25 +22,22 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr) } - res, kErr := s.kindBoxSvc.Add(ctx, adminkindboxparam.KindBoxAddRequest{ + _, kErr := s.kindBoxClient.AddKindBoxAfterAcceptingRequest(ctx, adminkindboxparam.KindBoxAddAfterAcceptingReqRequest{ BenefactorID: kindBoxReq.BenefactorID, KindBoxReqID: kindBoxReq.ID, + Type: kindBoxReq.KindBoxType, + Count: kindBoxReq.CountAccepted, }) if kErr != nil { return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr) } return param.KindBoxReqAcceptResponse{ - KindBoxReqID: kindBoxReq.ID, - KindBoxReqStatus: kindBoxReq.Status, - CountRequested: kindBoxReq.CountRequested, - CountAccepted: kindBoxReq.CountAccepted, - ReferDate: kindBoxReq.ReferDate, - AddressID: kindBoxReq.AddressID, - KindBoxID: res.KindBox.ID, - KindBoxType: kindBoxReq.KindBoxType, - BenefactorID: kindBoxReq.BenefactorID, - KindBoxStatus: res.KindBox.Status, - KindBoxSerialNumber: res.KindBox.SerialNumber, + KindBoxReqID: kindBoxReq.ID, + KindBoxReqStatus: kindBoxReq.Status, + CountRequested: kindBoxReq.CountRequested, + CountAccepted: kindBoxReq.CountAccepted, + ReferDate: kindBoxReq.ReferDate, + AddressID: kindBoxReq.AddressID, }, nil } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 172feb5..dd1de71 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -2,9 +2,9 @@ package adminkindboxreqservice import ( "context" - adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" "git.gocasts.ir/ebhomengo/niki/entity" + adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" ) type Repository interface { @@ -19,19 +19,24 @@ type Repository interface { RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error } +type KindBoxClient interface { + AddKindBoxAfterAcceptingRequest(ctx context.Context, param adminkindboxparam.KindBoxAddAfterAcceptingReqRequest) (adminkindboxparam.KindBoxAddAfterAcceptingReqResponse, error) +} + // TODO: check validation. // type BenefactorService interface { // IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error) // } type Service struct { - repo Repository - kindBoxSvc adminkindboxservice.Service + repo Repository // benefactorService BenefactorService + kindBoxClient KindBoxClient } -func New(repository Repository) Service { +func New(repository Repository, kindBoxClient KindBoxClient) Service { return Service{ - repo: repository, + repo: repository, + kindBoxClient: kindBoxClient, } } diff --git a/validator/admin/kind_box/add.go b/validator/admin/kind_box/add.go index 6aaf628..0df010f 100644 --- a/validator/admin/kind_box/add.go +++ b/validator/admin/kind_box/add.go @@ -1,49 +1,39 @@ package adminkindboxvalidator -import ( - "errors" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - 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" -) - -func (v Validator) ValidateAddRequest(req param.KindBoxAddRequest) (map[string]string, error) { - const op = "adminkindboxvalidator.KindBoxAddRequest" - - if err := validation.ValidateStruct(&req, - validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric), - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.SenderID, - validation.Required, - validation.By(v.doesEmployeeExist)), - - validation.Field(&req.KindBoxReqID, - validation.Required, - validation.By(v.doesKindBoxRequestExist)), - ); 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 -} +// func (v Validator) ValidateAddRequest(req param.KindBoxAddRequest) (map[string]string, error) { +// const op = "adminkindboxvalidator.KindBoxAddRequest" +// +// if err := validation.ValidateStruct(&req, +// validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric), +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.SenderID, +// validation.Required, +// validation.By(v.doesEmployeeExist)), +// +// validation.Field(&req.KindBoxReqID, +// validation.Required, +// validation.By(v.doesKindBoxRequestExist)), +// ); 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/delete.go b/validator/admin/kind_box/delete.go index 32b0635..9c3bf37 100644 --- a/validator/admin/kind_box/delete.go +++ b/validator/admin/kind_box/delete.go @@ -1,45 +1,45 @@ package adminkindboxvalidator -import ( - "errors" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - 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.KindBoxDeleteRequest) (map[string]string, error) { - const op = "adminkindboxvalidator.ValidateDeleteRequest" - - if err := validation.ValidateStruct(&req, - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.KindBoxID, - validation.Required, - validation.By(v.hasPendingStatus), - validation.By(v.doesKindBoxExist), - 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" +// 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.KindBoxDeleteRequest) (map[string]string, error) { +// const op = "adminkindboxvalidator.ValidateDeleteRequest" +// +// if err := validation.ValidateStruct(&req, +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.KindBoxID, +// validation.Required, +// validation.By(v.hasPendingStatus), +// validation.By(v.doesKindBoxExist), +// 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/get.go b/validator/admin/kind_box/get.go index da65d86..523f026 100644 --- a/validator/admin/kind_box/get.go +++ b/validator/admin/kind_box/get.go @@ -1,44 +1,44 @@ package adminkindboxvalidator -import ( - "errors" - - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - 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.KindBoxGetRequest) (map[string]string, error) { - const op = "adminkindboxvalidator.ValidateGetRequest" - - if err := validation.ValidateStruct(&req, - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.KindBoxID, - validation.Required, - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)), - validation.By(v.doesKindBoxExist)), - ); 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" +// 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.KindBoxGetRequest) (map[string]string, error) { +// const op = "adminkindboxvalidator.ValidateGetRequest" +// +// if err := validation.ValidateStruct(&req, +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.KindBoxID, +// validation.Required, +// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)), +// validation.By(v.doesKindBoxExist)), +// ); 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/update.go b/validator/admin/kind_box/update.go index 220eef5..cbf5449 100644 --- a/validator/admin/kind_box/update.go +++ b/validator/admin/kind_box/update.go @@ -1,59 +1,59 @@ package adminkindboxvalidator -import ( - "errors" - - "git.gocasts.ir/ebhomengo/niki/entity" - param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" - 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" -) - -func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) { - const op = "adminkindboxvalidator.ValidateUpdateRequest" - - if err := validation.ValidateStruct(&req, - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - validation.Field(&req.KindBoxID, - validation.Required, - validation.By(v.doesKindBoxExist), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), - - validation.Field(&req.SerialNumber, is.Alphanumeric), - - validation.Field(&req.TotalAmount, validation.Min(0)), - - validation.Field(&req.SenderID, - validation.By(v.doesEmployeeExist)), - - validation.Field(&req.ReceiverID, - validation.By(v.doesEmployeeExist)), - - validation.Field(&req.Status, - validation.In(entity.AllKindBoxStatus())), - ); 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" +// +// "git.gocasts.ir/ebhomengo/niki/entity" +// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" +// 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" +//) +// +// func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) { +// const op = "adminkindboxvalidator.ValidateUpdateRequest" +// +// if err := validation.ValidateStruct(&req, +// validation.Field(&req.BenefactorID, +// validation.Required, +// validation.By(v.doesBenefactorExist)), +// +// validation.Field(&req.KindBoxID, +// validation.Required, +// validation.By(v.doesKindBoxExist), +// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), +// +// validation.Field(&req.SerialNumber, is.Alphanumeric), +// +// validation.Field(&req.TotalAmount, validation.Min(0)), +// +// validation.Field(&req.SenderID, +// validation.By(v.doesEmployeeExist)), +// +// validation.Field(&req.ReceiverID, +// validation.By(v.doesEmployeeExist)), +// +// validation.Field(&req.Status, +// validation.In(entity.AllKindBoxStatus())), +// ); 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/validator.go b/validator/admin/kind_box/validator.go index 1345a1d..66d837e 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -1,12 +1,5 @@ package adminkindboxvalidator -import ( - "fmt" - - errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" - validation "github.com/go-ozzo/ozzo-validation/v4" -) - type Repository interface { KindBoxRequestExist(id uint) (bool, error) EmployeeExist(id uint) (bool, error) @@ -25,72 +18,72 @@ func New(repo Repository) Validator { return Validator{repo: repo} } -func (v Validator) doesKindBoxRequestExist(value interface{}) error { - receiverID, ok := value.(uint) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - _, err := v.repo.KindBoxRequestExist(receiverID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} - -func (v Validator) doesEmployeeExist(value interface{}) error { - senderID, ok := value.(uint) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - _, err := v.repo.EmployeeExist(senderID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} - -func (v Validator) doesBenefactorExist(value interface{}) error { - benefactorID, ok := value.(uint) - 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) doesKindBoxExist(value interface{}) error { - kindboxID, ok := value.(uint) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - _, err := v.repo.KindBoxExist(kindboxID) - 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 { +// receiverID, ok := value.(uint) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.KindBoxRequestExist(receiverID) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// +// return nil +//} +// +// func (v Validator) doesEmployeeExist(value interface{}) error { +// senderID, ok := value.(uint) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.EmployeeExist(senderID) +// if err != nil { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// +// return nil +//} +// +// func (v Validator) doesBenefactorExist(value interface{}) error { +// benefactorID, ok := value.(uint) +// 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) doesKindBoxExist(value interface{}) error { +// kindboxID, ok := value.(uint) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgNotFound) +// } +// _, err := v.repo.KindBoxExist(kindboxID) +// 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 +// } +//} // TODO: this temperary to ignore linter error. (unused function) // func (v Validator) hasCorrectStatus(value interface{}) error { @@ -106,15 +99,15 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R // 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) 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 +//} From a18da64cf921019e4ce6bb773d07975a4ae9ea53 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Tue, 23 Jan 2024 14:45:36 +0330 Subject: [PATCH 07/17] Using goroutines to create multiple kind-box --- repository/mysql/kind_box/kind_box.go | 67 ++++++++++++++++++++------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index bb1de97..4ccfb87 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,6 +2,7 @@ package mysqlkindbox import ( "context" + "log" "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -9,36 +10,68 @@ import ( ) func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) { - const op = "mysqlkindbox.AddBatchKindBox" + const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback" + errCh := make(chan error, len(kindBoxes)) + resultCh := make(chan entity.KindBox, len(kindBoxes)) tx, tErr := d.conn.Conn().Begin() if tErr != nil { return nil, richerror.New(op).WithErr(tErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } + for _, kindBox := range kindBoxes { - res, err := tx. - ExecContext(ctx, - "insert into kind_boxes (kind_box_req_id , benefactor_id , type ,serial_number , status) values (? , ? , ? , ? ,?);", - kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.Type.String(), kindBox.SerialNumber, kindBox.Status.String(), + go func(kb entity.KindBox) { + res, err := tx.ExecContext(ctx, + "insert into kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) values (?, ?, ?, ?, ?);", + kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(), ) - if err != nil { - if rErr := tx.Rollback(); rErr != nil { - return nil, richerror.New(op).WithErr(rErr). + if err != nil { + errCh <- richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + + return } - return nil, richerror.New(op).WithErr(err). + //nolint + // err is always nil + id, _ := res.LastInsertId() + kb.ID = uint(id) + + resultCh <- kb + errCh <- nil + }(kindBox) + } + + var result []entity.KindBox + + for i := 0; i < len(kindBoxes); i++ { + select { + case err := <-errCh: + if err != nil { + if err := tx.Rollback(); err != nil { + log.Printf("Rollback error: %v", err) + } + + return nil, err + } + case res := <-resultCh: + result = append(result, res) + case <-ctx.Done(): + if err := tx.Rollback(); err != nil { + log.Printf("Rollback error: %v", err) + } + + return nil, richerror.New(op).WithErr(ctx.Err()). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - - //nolint - // err is always nil - id, _ := res.LastInsertId() - kindBox.ID = uint(id) } - if cErr := tx.Commit(); cErr != nil { - return nil, richerror.New(op).WithErr(cErr). + + if err := tx.Commit(); err != nil { + log.Printf("Commit error: %v", err) + + return nil, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - return kindBoxes, nil + + return result, nil } From e4a3c5464a571234db4f0c1fa3da541377fe0feb Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Thu, 25 Jan 2024 18:45:53 +0330 Subject: [PATCH 08/17] RollbackKindBoxRequestStatus --- repository/mysql/kind_box/kind_box.go | 9 ++++----- repository/mysql/kind_box_req/kind_box_req.go | 17 +++++++++++++++++ service/admin/kind_box_req/accept.go | 15 ++++++++++++--- service/admin/kind_box_req/service.go | 1 + 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index 4ccfb87..98a49f6 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,9 +2,8 @@ package mysqlkindbox import ( "context" - "log" - "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -49,7 +48,7 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] case err := <-errCh: if err != nil { if err := tx.Rollback(); err != nil { - log.Printf("Rollback error: %v", err) + logger.L().Error("Rollback error: ", err) } return nil, err @@ -58,7 +57,7 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] result = append(result, res) case <-ctx.Done(): if err := tx.Rollback(); err != nil { - log.Printf("Rollback error: %v", err) + logger.L().Error("Rollback error: ", err) } return nil, richerror.New(op).WithErr(ctx.Err()). @@ -67,7 +66,7 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] } if err := tx.Commit(); err != nil { - log.Printf("Commit error: %v", err) + logger.L().Error("Commit error: ", err) return nil, richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index b0f8e15..02aca25 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -122,3 +122,20 @@ func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description return nil } + +func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { + op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus") + statement, err := d.conn.Conn(). + Prepare(`update kind_box_reqs set status = ? where id = ?`) + defer statement.Close() + if err != nil { + return richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + _, eErr := statement.ExecContext(ctx, entity.KindBoxReqPendingStatus.String(), id) + if eErr != nil { + return richerror.New(op).WithErr(eErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + return nil +} diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index 12c2c5e..e284e02 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -2,12 +2,17 @@ package adminkindboxreqservice import ( "context" - + "git.gocasts.ir/ebhomengo/niki/logger" adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) +//@see +// When confirming a request, should the senderID field, which represents the person sending the kind-box to the beneficiary, +// be filled at the same time? Or is it acceptable to confirm the request first and fill in the senderID field later? +// + 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) @@ -15,8 +20,6 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err) } - // fire new event to create a kind-box. - kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr) @@ -29,6 +32,12 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) Count: kindBoxReq.CountAccepted, }) if kErr != nil { + // rollback kind box request status + rErr := s.repo.RollbackKindBoxRequestStatus(ctx, req.ID) + if rErr != nil { + // log error + logger.L().Error(rErr.Error()) + } return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr) } diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index dd1de71..10b1788 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -17,6 +17,7 @@ type Repository interface { AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error + RollbackKindBoxRequestStatus(ctx context.Context, id uint) error } type KindBoxClient interface { From 96eb93bb699cb8eac3b0cd96b3aa9c875734797a Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Thu, 25 Jan 2024 21:17:10 +0330 Subject: [PATCH 09/17] minor change validation Accept/Reject kind box request --- pkg/err_msg/message.go | 4 +-- repository/mysql/kind_box_req/kind_box_req.go | 31 ------------------- validator/admin/kind_box_req/validator.go | 15 ++++++--- 3 files changed, 13 insertions(+), 37 deletions(-) diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 5d24ce5..4c7c36c 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -14,6 +14,6 @@ const ( ErrorMsgOtpCodeIsNotValid = "verification code is not valid" ErrorMsgCantScanQueryResult = "can't scan query result" ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" - ErrorMsgReAcceptKindBoxReq = "accepted request cannot be re-accepted" - ErrorMsgReRejectKindBoxReq = "rejected request cannot be re-rejected" + ErrorMsgAcceptKindBoxReqStatus = "only pending requests will have the ability to be confirmed" + ErrorMsgRejectKindBoxReqStatus = "only pending requests will have the ability to be rejected" ) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 02aca25..0a86519 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -74,37 +74,6 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) { return true, nil } -func (d DB) CheckKindBoxReqStatusForAccepting(id uint) error { - op := richerror.Op("mysqlkindboxreq.CheckKindBoxReqStatusForAccepting") - - k, err := d.GetByID(context.Background(), id) - if err != nil { - return richerror.New(op).WithErr(err) - } - if k.Status == entity.KindBoxReqAcceptedStatus { - return richerror.New(op).WithMessage(errmsg.ErrorMsgReAcceptKindBoxReq).WithMeta(map[string]interface{}{ - "id": id, - }).WithKind(richerror.KindInvalid) - } - - return nil -} - -func (d DB) CheckKindBoxReqStatusForRejecting(id uint) error { - op := richerror.Op("mysqlkindboxreq.CheckKindBoxReqStatusForRejecting") - k, err := d.GetByID(context.Background(), id) - if err != nil { - return richerror.New(op).WithErr(err) - } - if k.Status == entity.KindBoxReqRejectedStatus { - return richerror.New(op).WithMessage(errmsg.ErrorMsgReRejectKindBoxReq).WithMeta(map[string]interface{}{ - "id": id, - }).WithKind(richerror.KindInvalid) - } - - return nil -} - func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error { op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") statement, err := d.conn.Conn(). diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 75d54db..c208c1d 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -1,8 +1,10 @@ package adminkindboxreqvalidator import ( + "context" "errors" "fmt" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" ) @@ -18,8 +20,7 @@ type Repository interface { // TypeExist(id int) (bool, error) // KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) // PendingStatus(id uint) (bool, error) - CheckKindBoxReqStatusForAccepting(id uint) error - CheckKindBoxReqStatusForRejecting(id uint) error + GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) } type Validator struct { @@ -79,10 +80,13 @@ func (v Validator) CheckKindBoxReqStatusForAccepting(value interface{}) error { if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - err := v.repo.CheckKindBoxReqStatusForAccepting(kindboxreqID) + kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID) if err != nil { return err } + if kindBox.Status != entity.KindBoxReqPendingStatus { + return fmt.Errorf(errmsg.ErrorMsgAcceptKindBoxReqStatus) + } return nil } @@ -92,10 +96,13 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error { if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - err := v.repo.CheckKindBoxReqStatusForRejecting(kindboxreqID) + kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID) if err != nil { return err } + if kindBox.Status != entity.KindBoxReqPendingStatus { + return fmt.Errorf(errmsg.ErrorMsgRejectKindBoxReqStatus) + } return nil } From 6853ddaf647bb26a9abbd05a6fa9ebae8b739610 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Thu, 25 Jan 2024 21:29:18 +0330 Subject: [PATCH 10/17] fix linter errors --- delivery/http_server/admin/kind_box/get.go | 2 +- delivery/http_server/admin/kind_box/get_all.go | 2 +- delivery/http_server/admin/kind_box/route.go | 5 ++--- delivery/http_server/admin/kind_box/update.go | 2 +- main.go | 1 - repository/mysql/kind_box/kind_box.go | 1 + repository/mysql/kind_box_req/kind_box_req.go | 18 +++++++++++++++--- service/admin/kind_box_req/accept.go | 4 +++- validator/admin/kind_box/validator.go | 4 ++-- validator/admin/kind_box_req/validator.go | 2 +- 10 files changed, 27 insertions(+), 14 deletions(-) diff --git a/delivery/http_server/admin/kind_box/get.go b/delivery/http_server/admin/kind_box/get.go index 518abfa..1ff0ff5 100644 --- a/delivery/http_server/admin/kind_box/get.go +++ b/delivery/http_server/admin/kind_box/get.go @@ -8,7 +8,7 @@ package adminkindboxhandler // echo "github.com/labstack/echo/v4" //) // -//func (h Handler) Get(c echo.Context) error { +// func (h Handler) Get(c echo.Context) error { // var req param.KindBoxGetRequest // if bErr := c.Bind(&req); bErr != nil { // return echo.NewHTTPError(http.StatusBadRequest) diff --git a/delivery/http_server/admin/kind_box/get_all.go b/delivery/http_server/admin/kind_box/get_all.go index 2e04c65..c39e2a2 100644 --- a/delivery/http_server/admin/kind_box/get_all.go +++ b/delivery/http_server/admin/kind_box/get_all.go @@ -9,7 +9,7 @@ package adminkindboxhandler // echo "github.com/labstack/echo/v4" //) // -//func (h Handler) GetAll(c echo.Context) error { +// func (h Handler) GetAll(c echo.Context) error { // var req param.KindBoxGetAllRequest // if bErr := c.Bind(&req); bErr != nil { // return echo.NewHTTPError(http.StatusBadRequest) diff --git a/delivery/http_server/admin/kind_box/route.go b/delivery/http_server/admin/kind_box/route.go index 4bad945..72d97ce 100644 --- a/delivery/http_server/admin/kind_box/route.go +++ b/delivery/http_server/admin/kind_box/route.go @@ -4,9 +4,8 @@ import ( echo "github.com/labstack/echo/v4" ) -func (h Handler) SetRoutes(e *echo.Echo) { - - +func (h Handler) SetRoutes(_ *echo.Echo) { + //nolint // r.POST("/", h.Add).Name = "admin-addkindbox" // r.GET("/:id", h.Get).Name = "admin-getkindboxbyid" // r.GET("/", h.GetAll).Name = "admin-getallkindbox" diff --git a/delivery/http_server/admin/kind_box/update.go b/delivery/http_server/admin/kind_box/update.go index 0f640c6..e52ad0a 100644 --- a/delivery/http_server/admin/kind_box/update.go +++ b/delivery/http_server/admin/kind_box/update.go @@ -8,7 +8,7 @@ package adminkindboxhandler // echo "github.com/labstack/echo/v4" //) // -//func (h Handler) Update(c echo.Context) error { +// func (h Handler) Update(c echo.Context) error { // var req param.KindBoxUpdateRequest // if bErr := c.Bind(&req); bErr != nil { // return echo.NewHTTPError(http.StatusBadRequest) diff --git a/main.go b/main.go index f7d65e0..ed797ce 100644 --- a/main.go +++ b/main.go @@ -63,7 +63,6 @@ func setupServices(cfg config.Config) ( adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc adminauthservice.Service, adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator, - ) { authSvc = authservice.New(cfg.Auth) diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index 98a49f6..c910f3f 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,6 +2,7 @@ package mysqlkindbox import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 0a86519..9889051 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -6,6 +6,7 @@ import ( "errors" "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -32,7 +33,11 @@ func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted ui op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`) - defer statement.Close() + defer func() { + dErr := statement.Close() + logger.L().Error(dErr.Error()) + }() + if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) @@ -78,7 +83,10 @@ func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`) - defer statement.Close() + defer func() { + dErr := statement.Close() + logger.L().Error(dErr.Error()) + }() if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) @@ -96,7 +104,10 @@ func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set status = ? where id = ?`) - defer statement.Close() + defer func() { + dErr := statement.Close() + logger.L().Error(dErr.Error()) + }() if err != nil { return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) @@ -106,5 +117,6 @@ func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { return richerror.New(op).WithErr(eErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } + return nil } diff --git a/service/admin/kind_box_req/accept.go b/service/admin/kind_box_req/accept.go index e284e02..c39e55f 100644 --- a/service/admin/kind_box_req/accept.go +++ b/service/admin/kind_box_req/accept.go @@ -2,13 +2,14 @@ package adminkindboxreqservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/logger" adminkindboxparam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -//@see +// @see // When confirming a request, should the senderID field, which represents the person sending the kind-box to the beneficiary, // be filled at the same time? Or is it acceptable to confirm the request first and fill in the senderID field later? // @@ -38,6 +39,7 @@ func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) // log error logger.L().Error(rErr.Error()) } + return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(kErr) } diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index 66d837e..7d23ca2 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -57,7 +57,7 @@ func New(repo Repository) Validator { // return nil //} // -//func (v Validator) doesKindBoxExist(value interface{}) error { +// func (v Validator) doesKindBoxExist(value interface{}) error { // kindboxID, ok := value.(uint) // if !ok { // return fmt.Errorf(errmsg.ErrorMsgNotFound) @@ -70,7 +70,7 @@ func New(repo Repository) Validator { // return nil //} // -//func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { +// func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { // return func(value interface{}) error { // kbID, ok := value.(uint) // if !ok { diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index c208c1d..50a2f0d 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -4,8 +4,8 @@ import ( "context" "errors" "fmt" - "git.gocasts.ir/ebhomengo/niki/entity" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" ) From 85b9a7254edc6b279fc3d40d13128e49e01a1770 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Fri, 26 Jan 2024 09:22:57 +0330 Subject: [PATCH 11/17] fix error handling defer close in repo layer --- repository/mysql/kind_box_req/kind_box_req.go | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 9889051..9dcc0b1 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -4,9 +4,7 @@ import ( "context" "database/sql" "errors" - "git.gocasts.ir/ebhomengo/niki/entity" - "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) @@ -29,13 +27,16 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en return kindBoxReq, nil } -func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error { +func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) (finalErr error) { op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`) + defer func() { - dErr := statement.Close() - logger.L().Error(dErr.Error()) + cErr := statement.Close() + if cErr != nil { + finalErr = cErr + } }() if err != nil { @@ -79,13 +80,15 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) { return true, nil } -func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error { +func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) (finalErr error) { op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`) defer func() { - dErr := statement.Close() - logger.L().Error(dErr.Error()) + cErr := statement.Close() + if cErr != nil { + finalErr = cErr + } }() if err != nil { return richerror.New(op).WithErr(err). @@ -100,13 +103,15 @@ func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description return nil } -func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error { +func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) (finalErr error) { op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus") statement, err := d.conn.Conn(). Prepare(`update kind_box_reqs set status = ? where id = ?`) defer func() { - dErr := statement.Close() - logger.L().Error(dErr.Error()) + cErr := statement.Close() + if cErr != nil { + finalErr = cErr + } }() if err != nil { return richerror.New(op).WithErr(err). From ed5c1650407bd76acd449f6ece124de8abd413b7 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Fri, 26 Jan 2024 09:32:25 +0330 Subject: [PATCH 12/17] Deleting the commented codes --- delivery/http_server/admin/kind_box/add.go | 23 ----- delivery/http_server/admin/kind_box/get.go | 31 ------ .../http_server/admin/kind_box/get_all.go | 25 ----- delivery/http_server/admin/kind_box/route.go | 5 - delivery/http_server/admin/kind_box/update.go | 31 ------ .../http_server/admin/kind_box_req/add.go | 31 ------ .../http_server/admin/kind_box_req/get.go | 23 ----- .../http_server/admin/kind_box_req/get_all.go | 24 ----- .../http_server/admin/kind_box_req/route.go | 6 -- .../http_server/admin/kind_box_req/update.go | 31 ------ .../benefactor/kind_box_req/get.go | 23 ----- .../benefactor/kind_box_req/get_all.go | 16 ---- .../benefactor/kind_box_req/route.go | 4 - .../benefactor/kind_box_req/update.go | 23 ----- repository/mysql/kind_box_req/kind_box_req.go | 1 + service/admin/kind_box/delete.go | 19 ---- service/admin/kind_box/get.go | 18 ---- service/admin/kind_box/get_all.go | 17 ---- service/admin/kind_box/service.go | 9 -- service/admin/kind_box/update.go | 28 ------ service/admin/kind_box_req/add.go | 16 ---- service/admin/kind_box_req/delete.go | 20 ---- service/admin/kind_box_req/get.go | 20 ---- service/admin/kind_box_req/get_all.go | 20 ---- service/admin/kind_box_req/service.go | 6 -- service/admin/kind_box_req/update.go | 28 ------ service/benefactor/kind_box_req/delete.go | 12 --- service/benefactor/kind_box_req/get.go | 11 --- service/benefactor/kind_box_req/get_all.go | 12 --- service/benefactor/kind_box_req/service.go | 5 - service/benefactor/kind_box_req/update.go | 17 ---- validator/admin/kind_box/add.go | 38 -------- validator/admin/kind_box/delete.go | 44 --------- validator/admin/kind_box/get.go | 43 --------- validator/admin/kind_box/update.go | 58 ------------ validator/admin/kind_box/validator.go | 94 ------------------- validator/admin/kind_box_req/add.go | 45 --------- validator/admin/kind_box_req/delete.go | 44 --------- validator/admin/kind_box_req/get.go | 43 --------- validator/admin/kind_box_req/update.go | 49 ---------- validator/admin/kind_box_req/validator.go | 58 ------------ 41 files changed, 1 insertion(+), 1070 deletions(-) diff --git a/delivery/http_server/admin/kind_box/add.go b/delivery/http_server/admin/kind_box/add.go index fce2ac8..3cc696d 100644 --- a/delivery/http_server/admin/kind_box/add.go +++ b/delivery/http_server/admin/kind_box/add.go @@ -1,24 +1 @@ package adminkindboxhandler - -// func (h Handler) Add(c echo.Context) error { -// var req param.KindBoxAddRequest -// if bErr := c.Bind(&req); bErr != nil { -// return echo.NewHTTPError(http.StatusBadRequest) -// } -// if fieldErrors, err := h.adminKindBoxVld.ValidateAddRequest(req); err != nil { -// msg, code := httpmsg.Error(err) -// -// return c.JSON(code, echo.Map{ -// "message": msg, -// "errors": fieldErrors, -// }) -// } -// resp, sErr := h.adminKindBoxSvc.Add(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/get.go b/delivery/http_server/admin/kind_box/get.go index 1ff0ff5..3cc696d 100644 --- a/delivery/http_server/admin/kind_box/get.go +++ b/delivery/http_server/admin/kind_box/get.go @@ -1,32 +1 @@ package adminkindboxhandler - -// import ( -// "net/http" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// 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.KindBoxGetRequest -// if bErr := c.Bind(&req); bErr != nil { -// return echo.NewHTTPError(http.StatusBadRequest) -// } -// if fieldErrors, err := h.adminKindBoxVld.ValidateGetRequest(req); err != nil { -// msg, code := httpmsg.Error(err) -// -// return c.JSON(code, echo.Map{ -// "message": msg, -// "errors": fieldErrors, -// }) -// } -// resp, sErr := h.adminKindBoxSvc.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/get_all.go b/delivery/http_server/admin/kind_box/get_all.go index c39e2a2..3cc696d 100644 --- a/delivery/http_server/admin/kind_box/get_all.go +++ b/delivery/http_server/admin/kind_box/get_all.go @@ -1,26 +1 @@ package adminkindboxhandler - -// -// import ( -// "net/http" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// 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.KindBoxGetAllRequest -// if bErr := c.Bind(&req); bErr != nil { -// return echo.NewHTTPError(http.StatusBadRequest) -// } -// -// resp, sErr := h.adminKindBoxSvc.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/route.go b/delivery/http_server/admin/kind_box/route.go index 72d97ce..ef9bd24 100644 --- a/delivery/http_server/admin/kind_box/route.go +++ b/delivery/http_server/admin/kind_box/route.go @@ -5,9 +5,4 @@ import ( ) func (h Handler) SetRoutes(_ *echo.Echo) { - //nolint - // r.POST("/", h.Add).Name = "admin-addkindbox" - // r.GET("/:id", h.Get).Name = "admin-getkindboxbyid" - // r.GET("/", h.GetAll).Name = "admin-getallkindbox" - // r.PATCH("/:id", h.Update).Name = "admin-updatekindbox" } diff --git a/delivery/http_server/admin/kind_box/update.go b/delivery/http_server/admin/kind_box/update.go index e52ad0a..3cc696d 100644 --- a/delivery/http_server/admin/kind_box/update.go +++ b/delivery/http_server/admin/kind_box/update.go @@ -1,32 +1 @@ package adminkindboxhandler - -// import ( -// "net/http" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" -// echo "github.com/labstack/echo/v4" -//) -// -// func (h Handler) Update(c echo.Context) error { -// var req param.KindBoxUpdateRequest -// if bErr := c.Bind(&req); bErr != nil { -// return echo.NewHTTPError(http.StatusBadRequest) -// } -// if fieldErrors, err := h.adminKindBoxVld.ValidateUpdateRequest(req); err != nil { -// msg, code := httpmsg.Error(err) -// -// return c.JSON(code, echo.Map{ -// "message": msg, -// "errors": fieldErrors, -// }) -// } -// resp, sErr := h.adminKindBoxSvc.Update(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/add.go b/delivery/http_server/admin/kind_box_req/add.go index faacba5..b5635e7 100644 --- a/delivery/http_server/admin/kind_box_req/add.go +++ b/delivery/http_server/admin/kind_box_req/add.go @@ -1,32 +1 @@ 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) Add(c echo.Context) error { -// var req param.KindBoxReqAddRequest -// if bErr := c.Bind(&req); bErr != nil { -// return echo.NewHTTPError(http.StatusBadRequest) -// } -// if fieldErrors, err := h.adminKindBoxReqVld.ValidateAddRequest(req); err != nil { -// msg, code := httpmsg.Error(err) -// -// return c.JSON(code, echo.Map{ -// "message": msg, -// "errors": fieldErrors, -// }) -// } -// resp, sErr := h.adminKindBoxReqSvc.Add(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 fa66c9a..b5635e7 100644 --- a/delivery/http_server/admin/kind_box_req/get.go +++ b/delivery/http_server/admin/kind_box_req/get.go @@ -1,24 +1 @@ package adminkindboxreqhandler - -// 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 241e186..b5635e7 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 @@ 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) -//} diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index 578dd01..a751fea 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -7,12 +7,6 @@ import ( func (h Handler) SetRoutes(e *echo.Echo) { r := e.Group("/admin/kindboxreqs") - //nolint:gocritic - //r.POST("/", h.Add).Name = "admin-addkindboxreq" - //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" // todo - add acl r.PATCH("/accept-kind-box-req/:id", h.Accept) r.PATCH("/reject-kind-box-req/:id", h.Reject) diff --git a/delivery/http_server/admin/kind_box_req/update.go b/delivery/http_server/admin/kind_box_req/update.go index 5e6b2c6..b5635e7 100644 --- a/delivery/http_server/admin/kind_box_req/update.go +++ b/delivery/http_server/admin/kind_box_req/update.go @@ -1,32 +1 @@ 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) Update(c echo.Context) error { -// var req param.KindBoxReqUpdateRequest -// if bErr := c.Bind(&req); bErr != nil { -// return echo.NewHTTPError(http.StatusBadRequest) -// } -// if fieldErrors, err := h.adminKindBoxReqVld.ValidateUpdateRequest(req); err != nil { -// msg, code := httpmsg.Error(err) -// -// return c.JSON(code, echo.Map{ -// "message": msg, -// "errors": fieldErrors, -// }) -// } -// resp, sErr := h.adminKindBoxReqSvc.Update(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/benefactor/kind_box_req/get.go b/delivery/http_server/benefactor/kind_box_req/get.go index 113e576..9ea2099 100644 --- a/delivery/http_server/benefactor/kind_box_req/get.go +++ b/delivery/http_server/benefactor/kind_box_req/get.go @@ -1,24 +1 @@ package benefactorkindboxreqhandler - -// 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.benefactorKindBoxReqVld.ValidateGetRequest(req); err != nil { -// msg, code := httpmsg.Error(err) -// -// return c.JSON(code, echo.Map{ -// "message": msg, -// "errors": fieldErrors, -// }) -// } -// resp, sErr := h.benefactorKindBoxReqSvc.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/benefactor/kind_box_req/get_all.go b/delivery/http_server/benefactor/kind_box_req/get_all.go index b06f9d8..9ea2099 100644 --- a/delivery/http_server/benefactor/kind_box_req/get_all.go +++ b/delivery/http_server/benefactor/kind_box_req/get_all.go @@ -1,17 +1 @@ package benefactorkindboxreqhandler - -// 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.benefactorKindBoxReqSvc.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/benefactor/kind_box_req/route.go b/delivery/http_server/benefactor/kind_box_req/route.go index a5b9b08..a4d2b0e 100644 --- a/delivery/http_server/benefactor/kind_box_req/route.go +++ b/delivery/http_server/benefactor/kind_box_req/route.go @@ -11,8 +11,4 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.POST("/", h.Add, middleware.Auth(h.authSvc, h.authConfig), middleware.BenefactorAuthorization(entity.UserBenefactorRole)) - //nolint:gocritic - // r.GET("/:id", h.Get) - // r.GET("/", h.GetAll) - // r.PATCH("/:id", h.Update) } diff --git a/delivery/http_server/benefactor/kind_box_req/update.go b/delivery/http_server/benefactor/kind_box_req/update.go index 0ae0f40..9ea2099 100644 --- a/delivery/http_server/benefactor/kind_box_req/update.go +++ b/delivery/http_server/benefactor/kind_box_req/update.go @@ -1,24 +1 @@ package benefactorkindboxreqhandler - -// func (h Handler) Update(c echo.Context) error { -// var req param.KindBoxReqUpdateRequest -// if bErr := c.Bind(&req); bErr != nil { -// return echo.NewHTTPError(http.StatusBadRequest) -// } -// if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateUpdateRequest(req); err != nil { -// msg, code := httpmsg.Error(err) -// -// return c.JSON(code, echo.Map{ -// "message": msg, -// "errors": fieldErrors, -// }) -// } -// resp, sErr := h.benefactorKindBoxReqSvc.Update(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/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 9dcc0b1..7605809 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "errors" + "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/service/admin/kind_box/delete.go b/service/admin/kind_box/delete.go index 316d7c2..9b0bb5d 100644 --- a/service/admin/kind_box/delete.go +++ b/service/admin/kind_box/delete.go @@ -1,20 +1 @@ package adminkindboxservice - -// import ( -// "context" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -//) -// -// func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (param.KindBoxDeleteResponse, error) { -// // TODO: Does business domain need to delete an kindbox ? -// const op = "adminkindboxservice.Delete" -// -// dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID) -// if dErr != nil { -// return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxDeleteResponse{}, nil -//} diff --git a/service/admin/kind_box/get.go b/service/admin/kind_box/get.go index b86a61a..9b0bb5d 100644 --- a/service/admin/kind_box/get.go +++ b/service/admin/kind_box/get.go @@ -1,19 +1 @@ package adminkindboxservice - -// import ( -// "context" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -//) -// -// func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.KindBoxGetResponse, error) { -// const op = "adminkindboxservice.Get" -// -// kindBox, err := s.repo.GetKindBox(ctx, req.KindBoxID) -// if err != nil { -// return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxGetResponse{KindBox: kindBox}, nil -//} diff --git a/service/admin/kind_box/get_all.go b/service/admin/kind_box/get_all.go index 6f0cdc4..9b0bb5d 100644 --- a/service/admin/kind_box/get_all.go +++ b/service/admin/kind_box/get_all.go @@ -1,18 +1 @@ package adminkindboxservice - -// import ( -// "context" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -//) -// -// func (s Service) GetAll(ctx context.Context, _ param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) { -// const op = "adminkindboxservice.GetAll" -// allKindBox, err := s.repo.GetAllKindBox(ctx) -// if err != nil { -// return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil -//} diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index c11956a..0b072e4 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -8,21 +8,12 @@ import ( type Repository interface { AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) - // UpdateKindBox(ctx context.Context, kindBoxID uint, kindBoxInput entity.KindBox) (entity.KindBox, error) - // DeleteKindBox(ctx context.Context, kindBoxID uint) error - // GetAllKindBox(ctx context.Context) ([]entity.KindBox, error) - // GetKindBox(ctx context.Context, kindBox uint) (entity.KindBox, error) } type Service struct { repo Repository } -// TODO: check validation. -// type BenefactorService interface { -// IsBenefactorExist(ctx context.Context, benefactorID uint) (bool, error) -// } - func New(repository Repository) Service { return Service{ repo: repository, diff --git a/service/admin/kind_box/update.go b/service/admin/kind_box/update.go index 1a21d1a..9b0bb5d 100644 --- a/service/admin/kind_box/update.go +++ b/service/admin/kind_box/update.go @@ -1,29 +1 @@ package adminkindboxservice - -// import ( -// "context" -// -// entity "git.gocasts.ir/ebhomengo/niki/entity" -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -//) -// -// func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (param.KindBoxUpdateResponse, error) { -// // TODO: can benefactor update its Request ? -// // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? -// // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected -// const op = "adminkindboxservice.Update" -// -// kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{ -// TotalAmount: req.TotalAmount, -// ReceiverID: req.ReceiverID, -// SenderID: req.SenderID, -// SerialNumber: req.SerialNumber, -// Status: req.Status, -// }) -// if uErr != nil { -// return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxUpdateResponse{KindBox: kindBox}, nil -//} diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go index f50fce1..475ca43 100644 --- a/service/admin/kind_box_req/add.go +++ b/service/admin/kind_box_req/add.go @@ -1,17 +1 @@ package adminkindboxreqservice - -// func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { -// const op = "adminkindboxreqservice.Add" -// -// kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ -// BenefactorID: req.BenefactorID, -// KindBoxType: req.TypeID, -// CountRequested: req.CountRequested, -// Status: entity.KindBoxReqPendingStatus, -// }) -// if err != nil { -// return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxReqAddResponse{KindBoxReq: kindBoxReq}, nil -//} diff --git a/service/admin/kind_box_req/delete.go b/service/admin/kind_box_req/delete.go index bf7481e..475ca43 100644 --- a/service/admin/kind_box_req/delete.go +++ b/service/admin/kind_box_req/delete.go @@ -1,21 +1 @@ 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 -//} diff --git a/service/admin/kind_box_req/get.go b/service/admin/kind_box_req/get.go index f79f458..475ca43 100644 --- a/service/admin/kind_box_req/get.go +++ b/service/admin/kind_box_req/get.go @@ -1,21 +1 @@ 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 -//} diff --git a/service/admin/kind_box_req/get_all.go b/service/admin/kind_box_req/get_all.go index f16af1b..475ca43 100644 --- a/service/admin/kind_box_req/get_all.go +++ b/service/admin/kind_box_req/get_all.go @@ -1,21 +1 @@ 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 -//} diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 10b1788..d419e49 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -8,12 +8,6 @@ import ( ) 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) - // 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) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go index 72f6998..475ca43 100644 --- a/service/admin/kind_box_req/update.go +++ b/service/admin/kind_box_req/update.go @@ -1,29 +1 @@ package adminkindboxreqservice - -// -// import ( -// "context" -// -// entity "git.gocasts.ir/ebhomengo/niki/entity" -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" -// richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -//) -// -// func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { -// // TODO: can benefactor update its request ? -// // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? -// // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected -// -// const op = "adminkindboxreqservice.Update" -// -// kindBoxReq, uErr := s.repo.UpdateKindBoxReq(ctx, req.KindBoxReqID, entity.KindBoxReq{ -// BenefactorID: req.BenefactorID, -// TypeID: req.TypeID, -// CountRequested: req.CountRequested, -// }) -// if uErr != nil { -// return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxReqUpdateResponse{KindBoxReq: kindBoxReq}, nil -//} diff --git a/service/benefactor/kind_box_req/delete.go b/service/benefactor/kind_box_req/delete.go index 506f8ad..cf248cf 100644 --- a/service/benefactor/kind_box_req/delete.go +++ b/service/benefactor/kind_box_req/delete.go @@ -1,13 +1 @@ package benefactorkindboxreqservice - -// func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest) (param.KindBoxReqDeleteResponse, error) { -// // TODO: Does business domain need to delete an kindboxreq ? -// const op = "userkindboxreqservice.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/benefactor/kind_box_req/get.go b/service/benefactor/kind_box_req/get.go index ce50932..cf248cf 100644 --- a/service/benefactor/kind_box_req/get.go +++ b/service/benefactor/kind_box_req/get.go @@ -1,12 +1 @@ package benefactorkindboxreqservice - -// func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param.KindBoxReqGetResponse, error) { -// const op = "userkindboxreqservice.Get" -// -// 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/benefactor/kind_box_req/get_all.go b/service/benefactor/kind_box_req/get_all.go index fbaf2ba..cf248cf 100644 --- a/service/benefactor/kind_box_req/get_all.go +++ b/service/benefactor/kind_box_req/get_all.go @@ -1,13 +1 @@ package benefactorkindboxreqservice - -//// TODO: Pagination, Filters, Sort. -// func (s Service) GetAll(ctx context.Context, req param.KindBoxReqGetAllRequest) (param.KindBoxReqGetAllResponse, error) { -// const op = "userkindboxreqservice.GetAll" -// -// allKindBoxReq, err := s.repo.GetAllKindBoxReq(ctx, req.BenefactorID) -// if err != nil { -// return param.KindBoxReqGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxReqGetAllResponse{AllKindBoxReq: allKindBoxReq}, nil -//} diff --git a/service/benefactor/kind_box_req/service.go b/service/benefactor/kind_box_req/service.go index 1bd63de..1b336f2 100644 --- a/service/benefactor/kind_box_req/service.go +++ b/service/benefactor/kind_box_req/service.go @@ -8,11 +8,6 @@ import ( 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) - // TODO: can benefactor cancel its request ? - // DeleteKindBoxReq(ctx context.Context, kindBoxReqID uint) error - // GetAllKindBoxReq(ctx context.Context, benefactorID uint) ([]entity.KindBoxReq, error) - // GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) } type Service struct { diff --git a/service/benefactor/kind_box_req/update.go b/service/benefactor/kind_box_req/update.go index ba84bc3..cf248cf 100644 --- a/service/benefactor/kind_box_req/update.go +++ b/service/benefactor/kind_box_req/update.go @@ -1,18 +1 @@ package benefactorkindboxreqservice - -// func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { -// // TODO: can benefactor update its request ? -// // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? -// const op = "userkindboxreqservice.Update" -// -// kindBoxReq, uErr := s.repo.UpdateKindBoxReq(ctx, req.KindBoxReqID, entity.KindBoxReq{ -// BenefactorID: req.BenefactorID, -// KindBoxType: req.TypeID, -// CountRequested: req.CountRequested, -// }) -// if uErr != nil { -// return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(uErr).WithKind(richerror.KindUnexpected) -// } -// -// return param.KindBoxReqUpdateResponse{KindBoxReq: kindBoxReq}, nil -//} diff --git a/validator/admin/kind_box/add.go b/validator/admin/kind_box/add.go index 0df010f..7ba0d92 100644 --- a/validator/admin/kind_box/add.go +++ b/validator/admin/kind_box/add.go @@ -1,39 +1 @@ package adminkindboxvalidator - -// func (v Validator) ValidateAddRequest(req param.KindBoxAddRequest) (map[string]string, error) { -// const op = "adminkindboxvalidator.KindBoxAddRequest" -// -// if err := validation.ValidateStruct(&req, -// validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric), -// validation.Field(&req.BenefactorID, -// validation.Required, -// validation.By(v.doesBenefactorExist)), -// -// validation.Field(&req.SenderID, -// validation.Required, -// validation.By(v.doesEmployeeExist)), -// -// validation.Field(&req.KindBoxReqID, -// validation.Required, -// validation.By(v.doesKindBoxRequestExist)), -// ); 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/delete.go b/validator/admin/kind_box/delete.go index 9c3bf37..7ba0d92 100644 --- a/validator/admin/kind_box/delete.go +++ b/validator/admin/kind_box/delete.go @@ -1,45 +1 @@ package adminkindboxvalidator - -// import ( -// "errors" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// 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.KindBoxDeleteRequest) (map[string]string, error) { -// const op = "adminkindboxvalidator.ValidateDeleteRequest" -// -// if err := validation.ValidateStruct(&req, -// validation.Field(&req.BenefactorID, -// validation.Required, -// validation.By(v.doesBenefactorExist)), -// -// validation.Field(&req.KindBoxID, -// validation.Required, -// validation.By(v.hasPendingStatus), -// validation.By(v.doesKindBoxExist), -// 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/get.go b/validator/admin/kind_box/get.go index 523f026..7ba0d92 100644 --- a/validator/admin/kind_box/get.go +++ b/validator/admin/kind_box/get.go @@ -1,44 +1 @@ package adminkindboxvalidator - -// import ( -// "errors" -// -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// 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.KindBoxGetRequest) (map[string]string, error) { -// const op = "adminkindboxvalidator.ValidateGetRequest" -// -// if err := validation.ValidateStruct(&req, -// validation.Field(&req.BenefactorID, -// validation.Required, -// validation.By(v.doesBenefactorExist)), -// -// validation.Field(&req.KindBoxID, -// validation.Required, -// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)), -// validation.By(v.doesKindBoxExist)), -// ); 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/update.go b/validator/admin/kind_box/update.go index cbf5449..7ba0d92 100644 --- a/validator/admin/kind_box/update.go +++ b/validator/admin/kind_box/update.go @@ -1,59 +1 @@ package adminkindboxvalidator - -// import ( -// "errors" -// -// "git.gocasts.ir/ebhomengo/niki/entity" -// param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" -// 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" -//) -// -// func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[string]string, error) { -// const op = "adminkindboxvalidator.ValidateUpdateRequest" -// -// if err := validation.ValidateStruct(&req, -// validation.Field(&req.BenefactorID, -// validation.Required, -// validation.By(v.doesBenefactorExist)), -// -// validation.Field(&req.KindBoxID, -// validation.Required, -// validation.By(v.doesKindBoxExist), -// validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), -// -// validation.Field(&req.SerialNumber, is.Alphanumeric), -// -// validation.Field(&req.TotalAmount, validation.Min(0)), -// -// validation.Field(&req.SenderID, -// validation.By(v.doesEmployeeExist)), -// -// validation.Field(&req.ReceiverID, -// validation.By(v.doesEmployeeExist)), -// -// validation.Field(&req.Status, -// validation.In(entity.AllKindBoxStatus())), -// ); 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/validator.go b/validator/admin/kind_box/validator.go index 7d23ca2..b180884 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -17,97 +17,3 @@ type Validator struct { func New(repo Repository) Validator { return Validator{repo: repo} } - -// func (v Validator) doesKindBoxRequestExist(value interface{}) error { -// receiverID, ok := value.(uint) -// if !ok { -// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) -// } -// _, err := v.repo.KindBoxRequestExist(receiverID) -// if err != nil { -// return fmt.Errorf(errmsg.ErrorMsgNotFound) -// } -// -// return nil -//} -// -// func (v Validator) doesEmployeeExist(value interface{}) error { -// senderID, ok := value.(uint) -// if !ok { -// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) -// } -// _, err := v.repo.EmployeeExist(senderID) -// if err != nil { -// return fmt.Errorf(errmsg.ErrorMsgNotFound) -// } -// -// return nil -//} -// -// func (v Validator) doesBenefactorExist(value interface{}) error { -// benefactorID, ok := value.(uint) -// 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) doesKindBoxExist(value interface{}) error { -// kindboxID, ok := value.(uint) -// if !ok { -// return fmt.Errorf(errmsg.ErrorMsgNotFound) -// } -// _, err := v.repo.KindBoxExist(kindboxID) -// 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 -// } -//} - -// TODO: this temperary to ignore linter error. (unused function) -// func (v Validator) hasCorrectStatus(value interface{}) error { -// status, ok := value.(string) -// if !ok { -// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) -// } -// _, err := v.repo.CheckStatus(status) -// 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 -//} diff --git a/validator/admin/kind_box_req/add.go b/validator/admin/kind_box_req/add.go index f44c259..7e69606 100644 --- a/validator/admin/kind_box_req/add.go +++ b/validator/admin/kind_box_req/add.go @@ -1,46 +1 @@ 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 -//} diff --git a/validator/admin/kind_box_req/delete.go b/validator/admin/kind_box_req/delete.go index 72dea66..7e69606 100644 --- a/validator/admin/kind_box_req/delete.go +++ b/validator/admin/kind_box_req/delete.go @@ -1,45 +1 @@ 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 -//} diff --git a/validator/admin/kind_box_req/get.go b/validator/admin/kind_box_req/get.go index 1ffd8a6..7e69606 100644 --- a/validator/admin/kind_box_req/get.go +++ b/validator/admin/kind_box_req/get.go @@ -1,44 +1 @@ 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 -//} diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go index 90ba01f..7e69606 100644 --- a/validator/admin/kind_box_req/update.go +++ b/validator/admin/kind_box_req/update.go @@ -1,50 +1 @@ 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 -//} diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 50a2f0d..d9cf678 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -15,11 +15,7 @@ const ( ) type Repository interface { - // BenefactorExist(id int) (bool, error) KindBoxRequestExist(id uint) (bool, error) - // TypeExist(id int) (bool, error) - // KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) - // PendingStatus(id uint) (bool, error) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) } @@ -31,33 +27,6 @@ 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.(uint) if !ok { @@ -106,30 +75,3 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error { 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 -//} From 2e9bc7c241a6bcd53acfa60e9ad6174c2c31f467 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 10:35:56 +0330 Subject: [PATCH 13/17] remove CountAccepted from param/admin/kind_box_req/reject.go --- param/admin/kind_box_req/reject.go | 1 - service/admin/kind_box_req/reject.go | 1 - 2 files changed, 2 deletions(-) diff --git a/param/admin/kind_box_req/reject.go b/param/admin/kind_box_req/reject.go index 56119b1..a000e42 100644 --- a/param/admin/kind_box_req/reject.go +++ b/param/admin/kind_box_req/reject.go @@ -15,7 +15,6 @@ type KindBoxReqRejectResponse 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"` diff --git a/service/admin/kind_box_req/reject.go b/service/admin/kind_box_req/reject.go index 1734bca..0c8e66e 100644 --- a/service/admin/kind_box_req/reject.go +++ b/service/admin/kind_box_req/reject.go @@ -26,7 +26,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) ID: kindBoxReq.ID, KindBoxType: kindBoxReq.KindBoxType, CountRequested: kindBoxReq.CountRequested, - CountAccepted: kindBoxReq.CountAccepted, BenefactorID: kindBoxReq.BenefactorID, Status: kindBoxReq.Status, Description: kindBoxReq.Description, From 52f9a4aac29f2309d41e767318a99352433d1a3d Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 11:57:16 +0330 Subject: [PATCH 14/17] Removing from the output of the method. --- param/admin/kind_box/add.go | 4 +--- repository/mysql/kind_box/kind_box.go | 26 +++++++++----------------- service/admin/kind_box/add.go | 6 ++---- service/admin/kind_box/service.go | 2 +- 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index 4ba91ff..0227fb9 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -9,6 +9,4 @@ type KindBoxAddAfterAcceptingReqRequest struct { Count uint } -type KindBoxAddAfterAcceptingReqResponse struct { - KindBoxes []entity.KindBox -} +type KindBoxAddAfterAcceptingReqResponse struct{} diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index c910f3f..0672d18 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,26 +2,25 @@ package mysqlkindbox import ( "context" - "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) { +func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error { const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback" errCh := make(chan error, len(kindBoxes)) resultCh := make(chan entity.KindBox, len(kindBoxes)) tx, tErr := d.conn.Conn().Begin() if tErr != nil { - return nil, richerror.New(op).WithErr(tErr). + return richerror.New(op).WithErr(tErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } for _, kindBox := range kindBoxes { go func(kb entity.KindBox) { - res, err := tx.ExecContext(ctx, + _, err := tx.ExecContext(ctx, "insert into kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) values (?, ?, ?, ?, ?);", kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(), ) @@ -32,18 +31,11 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] return } - //nolint - // err is always nil - id, _ := res.LastInsertId() - kb.ID = uint(id) - resultCh <- kb errCh <- nil }(kindBox) } - var result []entity.KindBox - for i := 0; i < len(kindBoxes); i++ { select { case err := <-errCh: @@ -52,16 +44,16 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] logger.L().Error("Rollback error: ", err) } - return nil, err + return err } - case res := <-resultCh: - result = append(result, res) + case <-resultCh: + case <-ctx.Done(): if err := tx.Rollback(); err != nil { logger.L().Error("Rollback error: ", err) } - return nil, richerror.New(op).WithErr(ctx.Err()). + return richerror.New(op).WithErr(ctx.Err()). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } } @@ -69,9 +61,9 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] if err := tx.Commit(); err != nil { logger.L().Error("Commit error: ", err) - return nil, richerror.New(op).WithErr(err). + return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - return result, nil + return nil } diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index 9d59495..2cac8c5 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -22,12 +22,10 @@ func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param. SerialNumber: ulid.Make().String(), }) } - kindBoxes, err := s.repo.AddBatchKindBox(ctx, kindBoxes) + err := s.repo.AddBatchKindBox(ctx, kindBoxes) if err != nil { return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - return param.KindBoxAddAfterAcceptingReqResponse{ - KindBoxes: kindBoxes, - }, nil + return param.KindBoxAddAfterAcceptingReqResponse{}, nil } diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index 0b072e4..0910923 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -7,7 +7,7 @@ import ( ) type Repository interface { - AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) + AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error } type Service struct { From d09d7f068a19f8413dd79b04292f92bdd094f441 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 13:18:28 +0330 Subject: [PATCH 15/17] fix linter error --- delivery/http_server/middleware/benefactor_authorization.go | 1 + 1 file changed, 1 insertion(+) diff --git a/delivery/http_server/middleware/benefactor_authorization.go b/delivery/http_server/middleware/benefactor_authorization.go index 0371cc0..e442cda 100644 --- a/delivery/http_server/middleware/benefactor_authorization.go +++ b/delivery/http_server/middleware/benefactor_authorization.go @@ -9,6 +9,7 @@ import ( "github.com/labstack/echo/v4" ) +//nolint func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { From 154f6e98570bd6029f38742c221c3d27583239a7 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 15:32:03 +0330 Subject: [PATCH 16/17] remove comment --- service/admin/kind_box_req/reject.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/service/admin/kind_box_req/reject.go b/service/admin/kind_box_req/reject.go index 0c8e66e..aac41ec 100644 --- a/service/admin/kind_box_req/reject.go +++ b/service/admin/kind_box_req/reject.go @@ -14,9 +14,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err) } - // fire new event to create a kind-box. - // get kind box req - kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err) From 8f658433cc9f1c3199fd89c6353b31b53d473f4a Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 10:35:56 +0330 Subject: [PATCH 17/17] fix issue --- .../middleware/benefactor_authorization.go | 1 + param/admin/kind_box/add.go | 4 +-- param/admin/kind_box_req/reject.go | 1 - repository/mysql/kind_box/kind_box.go | 26 +++++++------------ service/admin/kind_box/add.go | 6 ++--- service/admin/kind_box/service.go | 2 +- service/admin/kind_box_req/reject.go | 4 --- 7 files changed, 14 insertions(+), 30 deletions(-) diff --git a/delivery/http_server/middleware/benefactor_authorization.go b/delivery/http_server/middleware/benefactor_authorization.go index 0371cc0..e442cda 100644 --- a/delivery/http_server/middleware/benefactor_authorization.go +++ b/delivery/http_server/middleware/benefactor_authorization.go @@ -9,6 +9,7 @@ import ( "github.com/labstack/echo/v4" ) +//nolint func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index 4ba91ff..0227fb9 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -9,6 +9,4 @@ type KindBoxAddAfterAcceptingReqRequest struct { Count uint } -type KindBoxAddAfterAcceptingReqResponse struct { - KindBoxes []entity.KindBox -} +type KindBoxAddAfterAcceptingReqResponse struct{} diff --git a/param/admin/kind_box_req/reject.go b/param/admin/kind_box_req/reject.go index 56119b1..a000e42 100644 --- a/param/admin/kind_box_req/reject.go +++ b/param/admin/kind_box_req/reject.go @@ -15,7 +15,6 @@ type KindBoxReqRejectResponse 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"` diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index c910f3f..0672d18 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,26 +2,25 @@ package mysqlkindbox import ( "context" - "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) { +func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error { const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback" errCh := make(chan error, len(kindBoxes)) resultCh := make(chan entity.KindBox, len(kindBoxes)) tx, tErr := d.conn.Conn().Begin() if tErr != nil { - return nil, richerror.New(op).WithErr(tErr). + return richerror.New(op).WithErr(tErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } for _, kindBox := range kindBoxes { go func(kb entity.KindBox) { - res, err := tx.ExecContext(ctx, + _, err := tx.ExecContext(ctx, "insert into kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) values (?, ?, ?, ?, ?);", kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(), ) @@ -32,18 +31,11 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] return } - //nolint - // err is always nil - id, _ := res.LastInsertId() - kb.ID = uint(id) - resultCh <- kb errCh <- nil }(kindBox) } - var result []entity.KindBox - for i := 0; i < len(kindBoxes); i++ { select { case err := <-errCh: @@ -52,16 +44,16 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] logger.L().Error("Rollback error: ", err) } - return nil, err + return err } - case res := <-resultCh: - result = append(result, res) + case <-resultCh: + case <-ctx.Done(): if err := tx.Rollback(); err != nil { logger.L().Error("Rollback error: ", err) } - return nil, richerror.New(op).WithErr(ctx.Err()). + return richerror.New(op).WithErr(ctx.Err()). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } } @@ -69,9 +61,9 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] if err := tx.Commit(); err != nil { logger.L().Error("Commit error: ", err) - return nil, richerror.New(op).WithErr(err). + return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - return result, nil + return nil } diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index 9d59495..2cac8c5 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -22,12 +22,10 @@ func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param. SerialNumber: ulid.Make().String(), }) } - kindBoxes, err := s.repo.AddBatchKindBox(ctx, kindBoxes) + err := s.repo.AddBatchKindBox(ctx, kindBoxes) if err != nil { return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - return param.KindBoxAddAfterAcceptingReqResponse{ - KindBoxes: kindBoxes, - }, nil + return param.KindBoxAddAfterAcceptingReqResponse{}, nil } diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index 0b072e4..0910923 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -7,7 +7,7 @@ import ( ) type Repository interface { - AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) + AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error } type Service struct { diff --git a/service/admin/kind_box_req/reject.go b/service/admin/kind_box_req/reject.go index 1734bca..aac41ec 100644 --- a/service/admin/kind_box_req/reject.go +++ b/service/admin/kind_box_req/reject.go @@ -14,9 +14,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err) } - // fire new event to create a kind-box. - // get kind box req - kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err) @@ -26,7 +23,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) ID: kindBoxReq.ID, KindBoxType: kindBoxReq.KindBoxType, CountRequested: kindBoxReq.CountRequested, - CountAccepted: kindBoxReq.CountAccepted, BenefactorID: kindBoxReq.BenefactorID, Status: kindBoxReq.Status, Description: kindBoxReq.Description,