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 +//}