From 91409f027561ff7c943f5586ff724f3bbf7f46db Mon Sep 17 00:00:00 2001 From: mohammad mahdi rezaei Date: Tue, 16 Jan 2024 19:43:06 +0330 Subject: [PATCH] :sparkles:feat(niki): add kindboxreq benefactor --- .../http_server/admin/kind_box_req/add.go | 60 ++++----- .../http_server/admin/kind_box_req/route.go | 6 +- .../http_server/admin/kind_box_req/update.go | 60 ++++----- .../benefactor/kind_box_req/add.go | 5 +- .../benefactor/kind_box_req/get.go | 52 ++++---- .../benefactor/kind_box_req/get_all.go | 38 +++--- .../benefactor/kind_box_req/route.go | 10 +- .../benefactor/kind_box_req/update.go | 52 ++++---- delivery/http_server/middleware/auth.go | 25 ++++ delivery/http_server/server.go | 28 +++-- entity/Province.go | 6 + entity/address.go | 12 ++ entity/city.go | 7 ++ entity/kind_box_req.go | 17 +-- entity/kind_box_type.go | 25 ++-- go.mod | 2 + go.sum | 4 + main.go | 26 +++- param/benefactor/address/add.go | 17 +++ param/benefactor/address/get.go | 10 ++ param/benefactor/benefactore/get.go | 8 ++ param/benefactor/kind_box_req/add.go | 14 ++- param/benefactor/kind_box_req/update.go | 2 +- pkg/claim/echo.go | 12 ++ repository/mysql/address/create.go | 27 ++++ repository/mysql/address/db.go | 13 ++ repository/mysql/address/get.go | 47 +++++++ .../mysql/benefactor/exist_benefactor.go | 22 ++++ .../mysql/{mysql_kind_box => kind_box}/db.go | 0 .../{mysql_kind_box => kind_box}/kind_box.go | 0 repository/mysql/kind_box_req/db.go | 13 ++ repository/mysql/kind_box_req/kind_box_req.go | 27 ++++ .../1705398864_add_provinces_table.sql | 9 ++ .../migration/1705399195_add_cities_table.sql | 11 ++ .../1705399370_add_addresses_table.sql | 19 +++ .../1705400111_add_kind_box_reqs_table.sql | 19 +++ rtx.log | 0 service/admin/kind_box_req/add.go | 38 +++--- service/admin/kind_box_req/update.go | 53 ++++---- service/benefactor/address/service.go | 52 ++++++++ service/benefactor/benefactor/get.go | 19 +++ service/benefactor/benefactor/service.go | 1 + service/benefactor/kind_box_req/add.go | 10 +- service/benefactor/kind_box_req/delete.go | 29 ++--- service/benefactor/kind_box_req/get.go | 27 ++-- service/benefactor/kind_box_req/get_all.go | 29 ++--- service/benefactor/kind_box_req/service.go | 8 +- service/benefactor/kind_box_req/update.go | 40 +++--- validator/benefactor/kind_box_req/add.go | 11 ++ validator/benefactor/kind_box_req/delete.go | 10 +- validator/benefactor/kind_box_req/get.go | 8 +- validator/benefactor/kind_box_req/update.go | 10 +- .../benefactor/kind_box_req/validator.go | 118 +++++++++++------- 53 files changed, 791 insertions(+), 377 deletions(-) create mode 100644 delivery/http_server/middleware/auth.go create mode 100644 entity/Province.go create mode 100644 entity/address.go create mode 100644 entity/city.go create mode 100644 param/benefactor/address/add.go create mode 100644 param/benefactor/address/get.go create mode 100644 param/benefactor/benefactore/get.go create mode 100644 pkg/claim/echo.go create mode 100644 repository/mysql/address/create.go create mode 100644 repository/mysql/address/db.go create mode 100644 repository/mysql/address/get.go rename repository/mysql/{mysql_kind_box => kind_box}/db.go (100%) rename repository/mysql/{mysql_kind_box => kind_box}/kind_box.go (100%) create mode 100644 repository/mysql/kind_box_req/db.go create mode 100644 repository/mysql/kind_box_req/kind_box_req.go create mode 100644 repository/mysql/migration/1705398864_add_provinces_table.sql create mode 100644 repository/mysql/migration/1705399195_add_cities_table.sql create mode 100644 repository/mysql/migration/1705399370_add_addresses_table.sql create mode 100644 repository/mysql/migration/1705400111_add_kind_box_reqs_table.sql create mode 100644 rtx.log create mode 100644 service/benefactor/address/service.go create mode 100644 service/benefactor/benefactor/get.go diff --git a/delivery/http_server/admin/kind_box_req/add.go b/delivery/http_server/admin/kind_box_req/add.go index 6166d97..faacba5 100644 --- a/delivery/http_server/admin/kind_box_req/add.go +++ b/delivery/http_server/admin/kind_box_req/add.go @@ -1,32 +1,32 @@ 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) -} +// 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/route.go b/delivery/http_server/admin/kind_box_req/route.go index 49011ed..ef9c179 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -7,8 +7,10 @@ import ( func (h Handler) SetRoutes(e *echo.Echo) { r := e.Group("/admin/kindboxreqs") - r.POST("/", h.Add).Name = "admin-addkindboxreq" + //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.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" + //nolint:gocritic + //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" } diff --git a/delivery/http_server/admin/kind_box_req/update.go b/delivery/http_server/admin/kind_box_req/update.go index 079d54e..5e6b2c6 100644 --- a/delivery/http_server/admin/kind_box_req/update.go +++ b/delivery/http_server/admin/kind_box_req/update.go @@ -1,32 +1,32 @@ 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) -} +// 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/add.go b/delivery/http_server/benefactor/kind_box_req/add.go index 373e55b..19f1314 100644 --- a/delivery/http_server/benefactor/kind_box_req/add.go +++ b/delivery/http_server/benefactor/kind_box_req/add.go @@ -4,15 +4,18 @@ import ( "net/http" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + "git.gocasts.ir/ebhomengo/niki/pkg/claim" 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 + claims := claim.GetClaimsFromEchoContext(c) + req := param.KindBoxReqAddRequest{BenefactorID: claims.UserID} if bErr := c.Bind(&req); bErr != nil { return echo.NewHTTPError(http.StatusBadRequest) } + if fieldErrors, err := h.benefactorKindBoxReqVld.ValidateAddRequest(req); err != nil { msg, code := httpmsg.Error(err) diff --git a/delivery/http_server/benefactor/kind_box_req/get.go b/delivery/http_server/benefactor/kind_box_req/get.go index 1d38b6a..113e576 100644 --- a/delivery/http_server/benefactor/kind_box_req/get.go +++ b/delivery/http_server/benefactor/kind_box_req/get.go @@ -1,32 +1,24 @@ package benefactorkindboxreqhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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.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) -} +// 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 de6a35f..b06f9d8 100644 --- a/delivery/http_server/benefactor/kind_box_req/get_all.go +++ b/delivery/http_server/benefactor/kind_box_req/get_all.go @@ -1,25 +1,17 @@ package benefactorkindboxreqhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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.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) -} +// 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 13d4d37..eb3c550 100644 --- a/delivery/http_server/benefactor/kind_box_req/route.go +++ b/delivery/http_server/benefactor/kind_box_req/route.go @@ -1,14 +1,16 @@ package benefactorkindboxreqhandler import ( + "git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware" echo "github.com/labstack/echo/v4" ) func (h Handler) SetRoutes(e *echo.Echo) { r := e.Group("/benefactor/kindboxreqs") - r.POST("/", h.Add).Name = "benefactor-addkindboxreq" - r.GET("/:id", h.Get).Name = "benefactor-get-kindboxreqbyid" - r.GET("/", h.GetAll).Name = "benefactor-getallkindboxreq" - r.PATCH("/:id", h.Update).Name = "benefactor-updatekindboxreq" + r.POST("/", h.Add, middleware.Auth(h.authSvc, h.authConfig)) + //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 54bf60b..0ae0f40 100644 --- a/delivery/http_server/benefactor/kind_box_req/update.go +++ b/delivery/http_server/benefactor/kind_box_req/update.go @@ -1,32 +1,24 @@ package benefactorkindboxreqhandler -import ( - "net/http" - - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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.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) -} +// 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/delivery/http_server/middleware/auth.go b/delivery/http_server/middleware/auth.go new file mode 100644 index 0000000..a721ccf --- /dev/null +++ b/delivery/http_server/middleware/auth.go @@ -0,0 +1,25 @@ +package middleware + +import ( + "git.gocasts.ir/ebhomengo/niki/config" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + mw "github.com/labstack/echo-jwt/v4" + "github.com/labstack/echo/v4" +) + +func Auth(service authservice.Service, cfg authservice.Config) echo.MiddlewareFunc { + return mw.WithConfig(mw.Config{ + ContextKey: config.AuthMiddlewareContextKey, + SigningKey: []byte(cfg.SignKey), + // TODO - as sign method string to config + SigningMethod: "HS256", + ParseTokenFunc: func(c echo.Context, auth string) (interface{}, error) { + claims, err := service.ParseToken(auth) + if err != nil { + return nil, err + } + + return claims, nil + }, + }) +} diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index facfbf4..9b78b28 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -5,23 +5,36 @@ import ( config "git.gocasts.ir/ebhomengo/niki/config" benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor" + benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" + benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/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" middleware "github.com/labstack/echo/v4/middleware" ) type Server struct { - config config.Config - Router *echo.Echo - benefactorHandler benefactorhandler.Handler + config config.Config + Router *echo.Echo + benefactorHandler benefactorhandler.Handler + benefactorkindboxreqhandler benefactorkindboxreqhandler.Handler } -func New(cfg config.Config, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator) Server { +func New( + cfg config.Config, + benefactorSvc benefactorservice.Service, + benefactorVld benefactorvalidator.Validator, + authSvc authservice.Service, + benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, + benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, +) Server { return Server{ - Router: echo.New(), - config: cfg, - benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorSvc, benefactorVld), + Router: echo.New(), + config: cfg, + benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorSvc, benefactorVld), + benefactorkindboxreqhandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld), } } @@ -33,6 +46,7 @@ func (s Server) Serve() { // Routes s.Router.GET("/health-check", s.healthCheck) s.benefactorHandler.SetRoutes(s.Router) + s.benefactorkindboxreqhandler.SetRoutes(s.Router) // Start server address := fmt.Sprintf(":%d", s.config.HTTPServer.Port) diff --git a/entity/Province.go b/entity/Province.go new file mode 100644 index 0000000..3d01755 --- /dev/null +++ b/entity/Province.go @@ -0,0 +1,6 @@ +package entity + +type Province struct { + ID uint + Name string +} diff --git a/entity/address.go b/entity/address.go new file mode 100644 index 0000000..ebfbb50 --- /dev/null +++ b/entity/address.go @@ -0,0 +1,12 @@ +package entity + +type Address struct { + ID uint + PostalCode string + Address string + Lat float32 + Lon float32 + CityID uint + ProvinceID uint + BenefactorID uint +} diff --git a/entity/city.go b/entity/city.go new file mode 100644 index 0000000..8c777e5 --- /dev/null +++ b/entity/city.go @@ -0,0 +1,7 @@ +package entity + +type City struct { + ID uint + Name string + ProvinceID uint +} diff --git a/entity/kind_box_req.go b/entity/kind_box_req.go index 6336109..cbb0384 100644 --- a/entity/kind_box_req.go +++ b/entity/kind_box_req.go @@ -3,12 +3,13 @@ package entity import "time" type KindBoxReq struct { - ID uint - TypeID uint - CountRequested uint - CountAccepted uint - BenefactorID uint - Status KindBoxReqStatus - Description string - StatusChangedAt time.Time + ID uint + KindBoxType KindBoxType + CountRequested uint + CountAccepted uint + BenefactorID uint + Status KindBoxReqStatus + Description string + ReferDate time.Time + AddressID uint } diff --git a/entity/kind_box_type.go b/entity/kind_box_type.go index 9f7e358..49d12f1 100644 --- a/entity/kind_box_type.go +++ b/entity/kind_box_type.go @@ -1,27 +1,28 @@ package entity +// TODO - use config file instead of const. type KindBoxType uint -// const ( -// KindBoxPendingSendStatus KindBoxType = iota + 1 -// KindBoxSentStatus -// KindBoxPendingReceivedStatus -// KindBoxReceivedStatus -// KindBoxEnumeratedStatus -// ) +const ( + KindBoxOnTable KindBoxType = iota + 1 + KindBoxCylindrical + KindBoxStandUp +) var KindBoxTypeStrings = map[KindBoxType]string{ - // KindBoxPendingSendStatus: "pending-send", - // KindBoxSentStatus: "sent", - // KindBoxPendingReceivedStatus: "pending-received", - // KindBoxReceivedStatus: "received", - // KindBoxEnumeratedStatus: "enumerated", + KindBoxOnTable: "on-table", + KindBoxCylindrical: "cylindrical", + KindBoxStandUp: "stand-up", } func (s KindBoxType) String() string { return KindBoxTypeStrings[s] } +func (s KindBoxType) IsValid() bool { + return s > 0 && int(s) <= len(KindBoxTypeStrings) +} + // AllKindBoxType returns a slice containing all string values of KindBoxType. func AllKindBoxType() []string { statusStrings := make([]string, len(KindBoxTypeStrings)) diff --git a/go.mod b/go.mod index 64a20d8..9b5c792 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-sql-driver/mysql v1.6.0 github.com/golang-jwt/jwt/v4 v4.5.0 github.com/knadh/koanf v1.5.0 + github.com/labstack/echo-jwt/v4 v4.2.0 github.com/labstack/echo/v4 v4.11.4 github.com/redis/go-redis/v9 v9.4.0 github.com/rubenv/sql-migrate v1.6.0 @@ -22,6 +23,7 @@ require ( github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/go-gorp/gorp/v3 v3.1.0 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect + github.com/golang-jwt/jwt/v5 v5.0.0 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index cacce26..205d41d 100644 --- a/go.sum +++ b/go.sum @@ -82,6 +82,8 @@ github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keL github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE= +github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -174,6 +176,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/labstack/echo-jwt/v4 v4.2.0 h1:odSISV9JgcSCuhgQSV/6Io3i7nUmfM/QkBeR5GVJj5c= +github.com/labstack/echo-jwt/v4 v4.2.0/go.mod h1:MA2RqdXdEn4/uEglx0HcUOgQSyBaTh5JcaHIan3biwU= github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8= github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= diff --git a/main.go b/main.go index e1c0314..e517b59 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,18 @@ import ( smsprovider "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider" "git.gocasts.ir/ebhomengo/niki/config" httpserver "git.gocasts.ir/ebhomengo/niki/delivery/http_server" + "git.gocasts.ir/ebhomengo/niki/repository/migrator" "git.gocasts.ir/ebhomengo/niki/repository/mysql" + mysqladdress "git.gocasts.ir/ebhomengo/niki/repository/mysql/address" mysqlbenefactor "git.gocasts.ir/ebhomengo/niki/repository/mysql/benefactor" + mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req" redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp" 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" 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" ) @@ -18,16 +24,18 @@ func main() { cfg := config.C() // TODO - add command for migrations - // mgr := migrator.New(cfg.Mysql) - // mgr.Up() + mgr := migrator.New(cfg.Mysql) + mgr.Up() - _, benefactorSvc, benefactorVld := setupServices(cfg) - server := httpserver.New(cfg, benefactorSvc, benefactorVld) + authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld := setupServices(cfg) + server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld) server.Serve() } +//nolint:nakedret // we are sure of this func setupServices(cfg config.Config) ( authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, + benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, ) { authSvc = authservice.New(cfg.Auth) @@ -43,5 +51,13 @@ func setupServices(cfg config.Config) ( benefactorVld = benefactorvalidator.New() - return authSvc, benefactorSvc, benefactorVld + benefactorkindBoxReqMysql := mysqlkindboxreq.New(MysqlRepo) + benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorkindBoxReqMysql) + + benefactorAddressMysql := mysqladdress.New(MysqlRepo) + benefactorAddressSvc := benefactoraddressservice.New(benefactorAddressMysql) + + benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorkindBoxReqMysql, benefactorSvc, benefactorAddressSvc) + + return } diff --git a/param/benefactor/address/add.go b/param/benefactor/address/add.go new file mode 100644 index 0000000..2579e7c --- /dev/null +++ b/param/benefactor/address/add.go @@ -0,0 +1,17 @@ +package addressparam + +import "git.gocasts.ir/ebhomengo/niki/entity" + +type BenefactorAddAddressRequest struct { + PostalCode string `json:"postal_code"` + Address string `json:"address"` + Lat float32 `json:"lat"` + Lon float32 `json:"lon"` + CityID uint `json:"city_id"` + ProvinceID uint `json:"province_id"` + BenefactorID uint `json:"benefactor_id"` +} + +type BenefactorAddAddressResponse struct { + Address entity.Address `json:"address"` +} diff --git a/param/benefactor/address/get.go b/param/benefactor/address/get.go new file mode 100644 index 0000000..de98560 --- /dev/null +++ b/param/benefactor/address/get.go @@ -0,0 +1,10 @@ +package addressparam + +import "git.gocasts.ir/ebhomengo/niki/entity" + +type GetAddressByIDRequest struct { + ID uint +} +type GetAddressByIDResponse struct { + Address *entity.Address +} diff --git a/param/benefactor/benefactore/get.go b/param/benefactor/benefactore/get.go new file mode 100644 index 0000000..48fade8 --- /dev/null +++ b/param/benefactor/benefactore/get.go @@ -0,0 +1,8 @@ +package benefactoreparam + +type BenefactorExistByIDRequest struct { + ID uint +} +type BenefactorExistByIDResponse struct { + Existed bool +} diff --git a/param/benefactor/kind_box_req/add.go b/param/benefactor/kind_box_req/add.go index fef7326..0bea866 100644 --- a/param/benefactor/kind_box_req/add.go +++ b/param/benefactor/kind_box_req/add.go @@ -1,13 +1,17 @@ package benefactorkindboxreqparam -import entity "git.gocasts.ir/ebhomengo/niki/entity" +import ( + entity "git.gocasts.ir/ebhomengo/niki/entity" +) type KindBoxReqAddRequest struct { - BenefactorID uint - TypeID uint - CountRequested uint + BenefactorID uint `json:"benefactor_id"` + TypeID entity.KindBoxType `json:"type_id"` + AddressID uint `json:"address_id"` + ReferDate string `json:"refer_date"` + CountRequested uint `json:"count_requested"` } type KindBoxReqAddResponse struct { - KindBoxReq entity.KindBoxReq + KindBoxReq entity.KindBoxReq `json:"kind_box_req"` } diff --git a/param/benefactor/kind_box_req/update.go b/param/benefactor/kind_box_req/update.go index e3d0df4..10d8f7e 100644 --- a/param/benefactor/kind_box_req/update.go +++ b/param/benefactor/kind_box_req/update.go @@ -5,7 +5,7 @@ import entity "git.gocasts.ir/ebhomengo/niki/entity" type KindBoxReqUpdateRequest struct { BenefactorID uint KindBoxReqID uint - TypeID uint + TypeID entity.KindBoxType CountRequested uint } diff --git a/pkg/claim/echo.go b/pkg/claim/echo.go new file mode 100644 index 0000000..e3db441 --- /dev/null +++ b/pkg/claim/echo.go @@ -0,0 +1,12 @@ +package claim + +import ( + "git.gocasts.ir/ebhomengo/niki/config" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + "github.com/labstack/echo/v4" +) + +func GetClaimsFromEchoContext(c echo.Context) *authservice.Claims { + //nolint:forcetypeassert //defensive programming vs let it crash - log-metric-recover ,... + return c.Get(config.AuthMiddlewareContextKey).(*authservice.Claims) +} diff --git a/repository/mysql/address/create.go b/repository/mysql/address/create.go new file mode 100644 index 0000000..8623217 --- /dev/null +++ b/repository/mysql/address/create.go @@ -0,0 +1,27 @@ +package mysqladdress + +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) CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) { + const op = "mysqlbenefactor.createBenefactorAddress" + + res, err := d.conn.Conn().ExecContext(ctx, `insert into addresses(postal_code, address, lat, lon,province_id,city_id,benefactor_id) values(?, ?, ?,?,?,?,?)`, + address.PostalCode, address.Address, address.Lat, address.Lon, address.ProvinceID, address.CityID, address.BenefactorID) + if err != nil { + return entity.Address{}, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) + } + + //nolint + // error is always nil + id, _ := res.LastInsertId() + address.ID = uint(id) + + return address, nil +} diff --git a/repository/mysql/address/db.go b/repository/mysql/address/db.go new file mode 100644 index 0000000..8aa9e69 --- /dev/null +++ b/repository/mysql/address/db.go @@ -0,0 +1,13 @@ +package mysqladdress + +import "git.gocasts.ir/ebhomengo/niki/repository/mysql" + +type DB struct { + conn *mysql.DB +} + +func New(conn *mysql.DB) *DB { + return &DB{ + conn: conn, + } +} diff --git a/repository/mysql/address/get.go b/repository/mysql/address/get.go new file mode 100644 index 0000000..e390f3b --- /dev/null +++ b/repository/mysql/address/get.go @@ -0,0 +1,47 @@ +package mysqladdress + +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" +) + +// TODO - use this approach or return (bool,entity.Address,error). +func (d DB) GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) { + const op = "mysqladdress.IsExistAddressByID" + + row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ?`, id) + + address, err := scanAddress(row) + if err != nil { + sErr := sql.ErrNoRows + //TODO-errorsas: second argument to errors.As should not be *error + //nolint + if errors.As(err, &sErr) { + return nil, nil + } + + // TODO - log unexpected error for better observability + return nil, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + + return &address, nil +} + +func scanAddress(scanner mysql.Scanner) (entity.Address, error) { + var createdAt time.Time + var address entity.Address + + err := scanner.Scan(&address.ID, &address.PostalCode, &address.Address, &address.Lat, &address.Lon, + &address.ProvinceID, &address.CityID, &address.BenefactorID, + &createdAt) + + return address, err +} diff --git a/repository/mysql/benefactor/exist_benefactor.go b/repository/mysql/benefactor/exist_benefactor.go index 6b538db..3b2e46c 100644 --- a/repository/mysql/benefactor/exist_benefactor.go +++ b/repository/mysql/benefactor/exist_benefactor.go @@ -34,6 +34,28 @@ func (d DB) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber stri return true, Benefactor, nil } +func (d *DB) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) { + const op = "mysqlbenefactor.IsExistBenefactorByID" + + row := d.conn.Conn().QueryRowContext(ctx, `select * from benefactors where id = ?`, id) + + _, err := scanBenefactor(row) + if err != nil { + sErr := sql.ErrNoRows + //TODO-errorsas: second argument to errors.As should not be *error + //nolint + if errors.As(err, &sErr) { + return false, nil + } + + // TODO - log unexpected error for better observability + return false, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + + return true, nil +} + func scanBenefactor(scanner mysql.Scanner) (entity.Benefactor, error) { var createdAt time.Time var benefactor entity.Benefactor diff --git a/repository/mysql/mysql_kind_box/db.go b/repository/mysql/kind_box/db.go similarity index 100% rename from repository/mysql/mysql_kind_box/db.go rename to repository/mysql/kind_box/db.go diff --git a/repository/mysql/mysql_kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go similarity index 100% rename from repository/mysql/mysql_kind_box/kind_box.go rename to repository/mysql/kind_box/kind_box.go diff --git a/repository/mysql/kind_box_req/db.go b/repository/mysql/kind_box_req/db.go new file mode 100644 index 0000000..e90d065 --- /dev/null +++ b/repository/mysql/kind_box_req/db.go @@ -0,0 +1,13 @@ +package mysqlkindboxreq + +import "git.gocasts.ir/ebhomengo/niki/repository/mysql" + +type DB struct { + conn *mysql.DB +} + +func New(conn *mysql.DB) *DB { + return &DB{ + conn: conn, + } +} diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go new file mode 100644 index 0000000..d9e2fc8 --- /dev/null +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -0,0 +1,27 @@ +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" +) + +func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) { + const op = "mysqlkindboxreq.AddKindBoxReq" + + res, err := d.conn.Conn().ExecContext(ctx, `insert into kind_box_reqs(benefactor_id,kind_box_type,address_id,count_requested,refer_date,status) values (?,?,?,?,?,?)`, + kindBoxReq.BenefactorID, kindBoxReq.KindBoxType.String(), kindBoxReq.AddressID, kindBoxReq.CountRequested, kindBoxReq.ReferDate, kindBoxReq.Status.String()) + if err != nil { + return entity.KindBoxReq{}, richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected) + } + + //nolint + // err is always nil + id, _ := res.LastInsertId() + kindBoxReq.ID = uint(id) + + return kindBoxReq, nil +} diff --git a/repository/mysql/migration/1705398864_add_provinces_table.sql b/repository/mysql/migration/1705398864_add_provinces_table.sql new file mode 100644 index 0000000..f55b3c9 --- /dev/null +++ b/repository/mysql/migration/1705398864_add_provinces_table.sql @@ -0,0 +1,9 @@ +-- +migrate Up +CREATE TABLE `provinces` ( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `name` VARCHAR(191) NOT NULL , + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +-- +migrate Down +DROP TABLE `provinces`; \ No newline at end of file diff --git a/repository/mysql/migration/1705399195_add_cities_table.sql b/repository/mysql/migration/1705399195_add_cities_table.sql new file mode 100644 index 0000000..d2a66dd --- /dev/null +++ b/repository/mysql/migration/1705399195_add_cities_table.sql @@ -0,0 +1,11 @@ +-- +migrate Up +CREATE TABLE `cities` ( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `name` VARCHAR(191) NOT NULL, + `province_id` INT NOT NULL, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`province_id`) REFERENCES `provinces`(`id`) +); + +-- +migrate Down +DROP TABLE `cities`; \ No newline at end of file diff --git a/repository/mysql/migration/1705399370_add_addresses_table.sql b/repository/mysql/migration/1705399370_add_addresses_table.sql new file mode 100644 index 0000000..c5f0107 --- /dev/null +++ b/repository/mysql/migration/1705399370_add_addresses_table.sql @@ -0,0 +1,19 @@ +-- +migrate Up +CREATE TABLE `addresses` +( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `postal_code` VARCHAR(191) NOT NULL, + `address` TEXT NOT NULL, + `lat` FLOAT NOT NULL, + `lon` FLOAT NOT NULL, + `province_id` INT NOT NULL, + `city_id` INT NOT NULL, + `benefactor_id` INT NOT NULL, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`province_id`) REFERENCES `provinces` (`id`), + FOREIGN KEY (`city_id`) REFERENCES `cities` (`id`), + FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`) +); + +-- +migrate Down +DROP TABLE `addresses`; \ No newline at end of file diff --git a/repository/mysql/migration/1705400111_add_kind_box_reqs_table.sql b/repository/mysql/migration/1705400111_add_kind_box_reqs_table.sql new file mode 100644 index 0000000..5798e1f --- /dev/null +++ b/repository/mysql/migration/1705400111_add_kind_box_reqs_table.sql @@ -0,0 +1,19 @@ +-- +migrate Up +CREATE TABLE `kind_box_reqs` +( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `benefactor_id` INT NOT NULL, + `kind_box_type` ENUM('on-table','cylindrical','stand-up') NOT NULL, + `address_id` INT NOT NULL, + `count_requested` INT UNSIGNED NOT NULL, + `count_accepted` INT, + `description` TEXT, + `refer_date` DATETIME NOT NULL, + `status` varchar(191), + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`address_id`) REFERENCES `addresses` (`id`), + FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors` (`id`) +); + +-- +migrate Down +DROP TABLE `kind_box_reqs`; \ No newline at end of file diff --git a/rtx.log b/rtx.log new file mode 100644 index 0000000..e69de29 diff --git a/service/admin/kind_box_req/add.go b/service/admin/kind_box_req/add.go index bc44260..f50fce1 100644 --- a/service/admin/kind_box_req/add.go +++ b/service/admin/kind_box_req/add.go @@ -1,25 +1,17 @@ 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) 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, - TypeID: 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 -} +// 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/update.go b/service/admin/kind_box_req/update.go index 63ee211..72f6998 100644 --- a/service/admin/kind_box_req/update.go +++ b/service/admin/kind_box_req/update.go @@ -1,28 +1,29 @@ 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 -} +// +// 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/address/service.go b/service/benefactor/address/service.go new file mode 100644 index 0000000..bed51da --- /dev/null +++ b/service/benefactor/address/service.go @@ -0,0 +1,52 @@ +package benefactoraddressservice + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +type Repository interface { + CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) + GetAddressByID(ctx context.Context, id uint) (*entity.Address, error) +} + +type Service struct { + repo Repository +} + +func New(repo Repository) Service { + return Service{repo: repo} +} + +func (s Service) Add(ctx context.Context, req param.BenefactorAddAddressRequest) (param.BenefactorAddAddressResponse, error) { + const op = "benefactoraddressservice.Add" + + address, err := s.repo.CreateBenefactorAddress(ctx, entity.Address{ + PostalCode: req.PostalCode, + Address: req.Address, + Lat: req.Lat, + Lon: req.Lon, + CityID: req.CityID, + ProvinceID: req.ProvinceID, + BenefactorID: req.BenefactorID, + }) + if err != nil { + return param.BenefactorAddAddressResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.BenefactorAddAddressResponse{Address: address}, nil +} + +func (s Service) AddressExistByID(ctx context.Context, req param.GetAddressByIDRequest) (param.GetAddressByIDResponse, error) { + const op = "benefactorservice.BenefactorExistByID" + + address, err := s.repo.GetAddressByID(ctx, req.ID) + if err != nil { + return param.GetAddressByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.GetAddressByIDResponse{Address: address}, nil +} diff --git a/service/benefactor/benefactor/get.go b/service/benefactor/benefactor/get.go new file mode 100644 index 0000000..33df195 --- /dev/null +++ b/service/benefactor/benefactor/get.go @@ -0,0 +1,19 @@ +package benefactorservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) BenefactorExistByID(ctx context.Context, req param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) { + const op = "benefactorservice.BenefactorExistByID" + + isExisted, err := s.repo.IsExistBenefactorByID(ctx, req.ID) + if err != nil { + return param.BenefactorExistByIDResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.BenefactorExistByIDResponse{Existed: isExisted}, nil +} diff --git a/service/benefactor/benefactor/service.go b/service/benefactor/benefactor/service.go index 219c2a4..44abd23 100644 --- a/service/benefactor/benefactor/service.go +++ b/service/benefactor/benefactor/service.go @@ -16,6 +16,7 @@ type Config struct { type Repository interface { IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error) + IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) } type AuthGenerator interface { diff --git a/service/benefactor/kind_box_req/add.go b/service/benefactor/kind_box_req/add.go index 3bd2c26..ff225ed 100644 --- a/service/benefactor/kind_box_req/add.go +++ b/service/benefactor/kind_box_req/add.go @@ -2,6 +2,7 @@ package benefactorkindboxreqservice import ( "context" + "time" entity "git.gocasts.ir/ebhomengo/niki/entity" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" @@ -10,10 +11,15 @@ import ( func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { const op = "userkindboxreqservice.Add" - + t, tErr := time.Parse(time.DateTime, req.ReferDate) + if tErr != nil { + return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(tErr).WithKind(richerror.KindInvalid) + } kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ BenefactorID: req.BenefactorID, - TypeID: req.TypeID, + KindBoxType: req.TypeID, + AddressID: req.AddressID, + ReferDate: t, CountRequested: req.CountRequested, Status: entity.KindBoxReqPendingStatus, }) diff --git a/service/benefactor/kind_box_req/delete.go b/service/benefactor/kind_box_req/delete.go index 20f9b30..506f8ad 100644 --- a/service/benefactor/kind_box_req/delete.go +++ b/service/benefactor/kind_box_req/delete.go @@ -1,20 +1,13 @@ package benefactorkindboxreqservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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 = "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 -} +// 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 0891acc..ce50932 100644 --- a/service/benefactor/kind_box_req/get.go +++ b/service/benefactor/kind_box_req/get.go @@ -1,19 +1,12 @@ package benefactorkindboxreqservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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 = "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 -} +// 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 6a40a01..fbaf2ba 100644 --- a/service/benefactor/kind_box_req/get_all.go +++ b/service/benefactor/kind_box_req/get_all.go @@ -1,20 +1,13 @@ package benefactorkindboxreqservice -import ( - "context" - - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -// 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 -} +//// 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 dfa81c1..1bd63de 100644 --- a/service/benefactor/kind_box_req/service.go +++ b/service/benefactor/kind_box_req/service.go @@ -8,11 +8,11 @@ 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) + // 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) + // 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 a8117d9..ba84bc3 100644 --- a/service/benefactor/kind_box_req/update.go +++ b/service/benefactor/kind_box_req/update.go @@ -1,26 +1,18 @@ package benefactorkindboxreqservice -import ( - "context" - - entity "git.gocasts.ir/ebhomengo/niki/entity" - param "git.gocasts.ir/ebhomengo/niki/param/benefactor/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 ? - const op = "userkindboxreqservice.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 -} +// 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/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index 9cbe8cf..6ea00b3 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -2,6 +2,7 @@ package benefactorkindboxreqvalidator import ( "errors" + "time" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" @@ -23,7 +24,17 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin validation.Field(&req.TypeID, validation.Required, validation.By(v.doesTypeExist)), + + validation.Field(&req.AddressID, + validation.Required, + validation.By(v.doesAddressExist(req.BenefactorID))), + + validation.Field(&req.ReferDate, + validation.Required, + validation.Date(time.DateTime), + ), ); err != nil { + fieldErrors := make(map[string]string) var errV validation.Errors diff --git a/validator/benefactor/kind_box_req/delete.go b/validator/benefactor/kind_box_req/delete.go index b96e4d1..42c973a 100644 --- a/validator/benefactor/kind_box_req/delete.go +++ b/validator/benefactor/kind_box_req/delete.go @@ -17,11 +17,11 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map 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.KindBoxReqID, + // validation.Required, + // validation.By(v.doesKindBoxRequestExist), + // validation.By(v.hasPendingStatus), + // validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/get.go b/validator/benefactor/kind_box_req/get.go index 10322dc..075f299 100644 --- a/validator/benefactor/kind_box_req/get.go +++ b/validator/benefactor/kind_box_req/get.go @@ -17,10 +17,10 @@ func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[strin validation.Required, validation.By(v.doesBenefactorExist)), - validation.Field(&req.KindBoxReqID, - validation.Required, - validation.By(v.doesKindBoxRequestExist), - validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), + // validation.Field(&req.KindBoxReqID, + // validation.Required, + // validation.By(v.doesKindBoxRequestExist), + // validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/update.go b/validator/benefactor/kind_box_req/update.go index 7112f7a..5c8addd 100644 --- a/validator/benefactor/kind_box_req/update.go +++ b/validator/benefactor/kind_box_req/update.go @@ -19,11 +19,11 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map 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.KindBoxReqID, + // validation.Required, + // validation.By(v.doesKindBoxRequestExist), + // validation.By(v.hasPendingStatus), + // validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), validation.Field(&req.TypeID, validation.Required, diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index d303035..9b17b0d 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -1,31 +1,43 @@ package benefactorkindboxreqvalidator import ( + "context" "fmt" + "git.gocasts.ir/ebhomengo/niki/entity" + addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address" + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" validation "github.com/go-ozzo/ozzo-validation/v4" ) const ( - MinKindBoxReq = 1 - MaxKindBoxReq = 100 + MinKindBoxReq uint = 1 + MaxKindBoxReq uint = 100 ) type Repository interface { - BenefactorExist(id uint) (bool, error) - KindBoxReqExist(id uint) (bool, error) - TypeExist(id uint) (bool, error) - KindBoxBelongToBenefactor(bID uint, kbID uint) (bool, error) - PendingStatus(id uint) (bool, error) + // KindBoxReqExist(id uint) (bool, error) + // KindBoxBelongToBenefactor(bID uint, kbID uint) (bool, error) + // PendingStatus(id uint) (bool, error) +} + +type BenefactorSvc interface { + BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error) +} + +type AddressSvc interface { + AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error) } type Validator struct { - repo Repository + repo Repository + benefactorSvc BenefactorSvc + addressSvc AddressSvc } -func New(repo Repository) Validator { - return Validator{repo: repo} +func New(repo Repository, benefactorSvc BenefactorSvc, addressSvc AddressSvc) Validator { + return Validator{repo: repo, benefactorSvc: benefactorSvc, addressSvc: addressSvc} } func (v Validator) doesBenefactorExist(value interface{}) error { @@ -33,7 +45,7 @@ func (v Validator) doesBenefactorExist(value interface{}) error { if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - _, err := v.repo.BenefactorExist(benefactorID) + _, err := v.benefactorSvc.BenefactorExistByID(context.Background(), param.BenefactorExistByIDRequest{ID: benefactorID}) if err != nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -42,39 +54,31 @@ func (v Validator) doesBenefactorExist(value interface{}) error { } func (v Validator) doesTypeExist(value interface{}) error { - typeID, ok := value.(uint) + typeID, ok := value.(entity.KindBoxType) if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - _, err := v.repo.TypeExist(typeID) - if err != nil { + if !typeID.IsValid() { return fmt.Errorf(errmsg.ErrorMsgNotFound) } return nil } -func (v Validator) doesKindBoxRequestExist(value interface{}) error { - kindBoxReqID, ok := value.(uint) - if !ok { - return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) - } - _, err := v.repo.KindBoxReqExist(kindBoxReqID) - if err != nil { - return fmt.Errorf(errmsg.ErrorMsgNotFound) - } - - return nil -} - -func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc { +func (v Validator) doesAddressExist(benefactorID uint) validation.RuleFunc { return func(value interface{}) error { - kbID, ok := value.(uint) + addressID, ok := value.(uint) if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + address, err := v.addressSvc.AddressExistByID(context.Background(), addressparam.GetAddressByIDRequest{ID: addressID}) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if address.Address == nil { return fmt.Errorf(errmsg.ErrorMsgNotFound) } - _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID) - if err != nil { + if address.Address.BenefactorID != benefactorID { return fmt.Errorf(errmsg.ErrorMsgNotFound) } @@ -82,15 +86,43 @@ func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.R } } -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) doesKindBoxRequestExist(value interface{}) error { +// kindBoxReqID, ok := value.(uint) +// if !ok { +// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) +// } +// _, err := v.repo.KindBoxReqExist(kindBoxReqID) +// 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) 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 +//}