From 11a74df75f29b518277e49dfd5ad3dc71fefce07 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Wed, 12 Jun 2024 13:49:21 +0330 Subject: [PATCH] fix(repository): replace "assigned-sender-agent" with entity.KindBoxReqAssignedSenderAgentStatus.String() --- .../benefactor/kind_box_req/route.go | 2 + .../benefactor/kind_box_req/update.go | 58 ++++++++++++ docs/docs.go | 90 +++++++++++++++++++ docs/swagger.json | 90 +++++++++++++++++++ docs/swagger.yaml | 58 ++++++++++++ param/benefactor/kind_box_req/update.go | 19 ++-- .../kind_box_req/get_delivery_awaiting.go | 4 +- repository/mysql/kind_box_req/update.go | 43 +++++++++ service/benefactor/kind_box_req/service.go | 1 + service/benefactor/kind_box_req/update.go | 37 ++++++++ validator/benefactor/kind_box_req/add.go | 2 +- validator/benefactor/kind_box_req/update.go | 22 ++--- .../benefactor/kind_box_req/validator.go | 2 +- 13 files changed, 403 insertions(+), 25 deletions(-) create mode 100644 delivery/http_server/benefactor/kind_box_req/update.go create mode 100644 repository/mysql/kind_box_req/update.go create mode 100644 service/benefactor/kind_box_req/update.go diff --git a/delivery/http_server/benefactor/kind_box_req/route.go b/delivery/http_server/benefactor/kind_box_req/route.go index 4ed79ff..48f0a80 100644 --- a/delivery/http_server/benefactor/kind_box_req/route.go +++ b/delivery/http_server/benefactor/kind_box_req/route.go @@ -17,4 +17,6 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.POST("/", h.Add) r.GET("/:id", h.Get) r.DELETE("/:id", h.Delete) + 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 new file mode 100644 index 0000000..e8291e8 --- /dev/null +++ b/delivery/http_server/benefactor/kind_box_req/update.go @@ -0,0 +1,58 @@ +package benefactorkindboxreqhandler + +import ( + "net/http" + + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + claim "git.gocasts.ir/ebhomengo/niki/pkg/claim" + echo "github.com/labstack/echo/v4" +) + +// UpdateKindBoxReq godoc +// @Summary update kindBoxReq +// @Tags KindBoxReq +// @Accept json +// @Produce json +// @Param id path int true "KindBoxReq ID" +// @Param Request body param.KindBoxReqUpdateRequest true "Update kind box req fields" +// @Success 204 {object} param.KindBoxReqUpdateResponse +// @Failure 400 {string} "Bad request" +// @Security AuthBearerBenefactor +// @Router /benefactor/kindboxreqs/{id} [patch] +func (h Handler) Update(c echo.Context) error { + + var req param.KindBoxReqUpdateRequest + + if err := c.Bind(&req); err != nil { + return c.JSON(http.StatusBadRequest, echo.Map{ + "message": errmsg.ErrBadRequest, + }) + } + + if bErr := echo.PathParamsBinder(c).Uint("id", &req.KindBoxReqID).BindError(); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + + claims := claim.GetClaimsFromEchoContext(c) + req.BenefactorID = claims.UserID + + 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, err := h.benefactorKindBoxReqSvc.UpdateKindBoxReq(c.Request().Context(), req) + if err != nil { + msg, code := httpmsg.Error(err) + return echo.NewHTTPError(code, msg) + } + + return c.JSON(http.StatusOK, resp) +} diff --git a/docs/docs.go b/docs/docs.go index dbc927c..6432659 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -868,6 +868,55 @@ const docTemplate = `{ } } } + }, + "patch": { + "security": [ + { + "AuthBearerBenefactor": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBoxReq" + ], + "summary": "update kindBoxReq", + "parameters": [ + { + "type": "integer", + "description": "KindBoxReq ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Update kind box req fields", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateRequest" + } + } + ], + "responses": { + "204": { + "description": "No Content", + "schema": { + "$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } } }, "/benefactor/login-register": { @@ -1574,6 +1623,47 @@ const docTemplate = `{ } } }, + "benefactorkindboxreqparam.KindBoxReqUpdateRequest": { + "type": "object", + "properties": { + "count_requested": { + "type": "integer", + "example": 2 + }, + "deliver_address_id": { + "type": "integer", + "example": 2 + }, + "deliver_refer_date": { + "type": "string", + "example": "2025-01-02 15:04:05" + }, + "deliver_refer_time_id": { + "type": "integer", + "example": 1 + }, + "description": { + "type": "string", + "example": "this is a description for..." + }, + "kind_box_type": { + "allOf": [ + { + "$ref": "#/definitions/entity.KindBoxType" + } + ], + "example": 1 + } + } + }, + "benefactorkindboxreqparam.KindBoxReqUpdateResponse": { + "type": "object", + "properties": { + "kind_box_req": { + "$ref": "#/definitions/entity.KindBoxReq" + } + } + }, "entity.Address": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 567dc95..9602453 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -857,6 +857,55 @@ } } } + }, + "patch": { + "security": [ + { + "AuthBearerBenefactor": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBoxReq" + ], + "summary": "update kindBoxReq", + "parameters": [ + { + "type": "integer", + "description": "KindBoxReq ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Update kind box req fields", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateRequest" + } + } + ], + "responses": { + "204": { + "description": "No Content", + "schema": { + "$ref": "#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } } }, "/benefactor/login-register": { @@ -1563,6 +1612,47 @@ } } }, + "benefactorkindboxreqparam.KindBoxReqUpdateRequest": { + "type": "object", + "properties": { + "count_requested": { + "type": "integer", + "example": 2 + }, + "deliver_address_id": { + "type": "integer", + "example": 2 + }, + "deliver_refer_date": { + "type": "string", + "example": "2025-01-02 15:04:05" + }, + "deliver_refer_time_id": { + "type": "integer", + "example": 1 + }, + "description": { + "type": "string", + "example": "this is a description for..." + }, + "kind_box_type": { + "allOf": [ + { + "$ref": "#/definitions/entity.KindBoxType" + } + ], + "example": 1 + } + } + }, + "benefactorkindboxreqparam.KindBoxReqUpdateResponse": { + "type": "object", + "properties": { + "kind_box_req": { + "$ref": "#/definitions/entity.KindBoxReq" + } + } + }, "entity.Address": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 758d76e..aec84fc 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -408,6 +408,33 @@ definitions: kind_box_req: $ref: '#/definitions/entity.KindBoxReq' type: object + benefactorkindboxreqparam.KindBoxReqUpdateRequest: + properties: + count_requested: + example: 2 + type: integer + deliver_address_id: + example: 2 + type: integer + deliver_refer_date: + example: "2025-01-02 15:04:05" + type: string + deliver_refer_time_id: + example: 1 + type: integer + description: + example: this is a description for... + type: string + kind_box_type: + allOf: + - $ref: '#/definitions/entity.KindBoxType' + example: 1 + type: object + benefactorkindboxreqparam.KindBoxReqUpdateResponse: + properties: + kind_box_req: + $ref: '#/definitions/entity.KindBoxReq' + type: object entity.Address: properties: address: @@ -1110,6 +1137,37 @@ paths: summary: Get a kind box request for a benefactor tags: - KindBoxReq + patch: + consumes: + - application/json + parameters: + - description: KindBoxReq ID + in: path + name: id + required: true + type: integer + - description: Update kind box req fields + in: body + name: Request + required: true + schema: + $ref: '#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateRequest' + produces: + - application/json + responses: + "204": + description: No Content + schema: + $ref: '#/definitions/benefactorkindboxreqparam.KindBoxReqUpdateResponse' + "400": + description: Bad request + schema: + type: string + security: + - AuthBearerBenefactor: [] + summary: update kindBoxReq + tags: + - KindBoxReq /benefactor/login-register: post: consumes: diff --git a/param/benefactor/kind_box_req/update.go b/param/benefactor/kind_box_req/update.go index 10d8f7e..c443e3c 100644 --- a/param/benefactor/kind_box_req/update.go +++ b/param/benefactor/kind_box_req/update.go @@ -1,14 +1,21 @@ package benefactorkindboxreqparam -import entity "git.gocasts.ir/ebhomengo/niki/entity" +import ( + + entity "git.gocasts.ir/ebhomengo/niki/entity" +) type KindBoxReqUpdateRequest struct { - BenefactorID uint - KindBoxReqID uint - TypeID entity.KindBoxType - CountRequested uint + KindBoxReqID uint `json:"-"` + BenefactorID uint `json:"-"` + KindBoxType entity.KindBoxType `json:"kind_box_type" example:"1"` + CountRequested uint `json:"count_requested" example:"2"` + Description string `json:"description" example:"this is a description for..."` + DeliverReferDate string `json:"deliver_refer_date" example:"2025-01-02 15:04:05"` + DeliverReferTimeID uint `json:"deliver_refer_time_id" example:"1"` + DeliverAddressID uint `json:"deliver_address_id" example:"2"` } type KindBoxReqUpdateResponse struct { - KindBoxReq entity.KindBoxReq + KindBoxReq entity.KindBoxReq `json:"kind_box_req"` } diff --git a/repository/mysql/kind_box_req/get_delivery_awaiting.go b/repository/mysql/kind_box_req/get_delivery_awaiting.go index cf7fb33..66d647a 100644 --- a/repository/mysql/kind_box_req/get_delivery_awaiting.go +++ b/repository/mysql/kind_box_req/get_delivery_awaiting.go @@ -12,8 +12,8 @@ import ( func (d DB) GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error) { const op = "mysqlkindboxreq.GetAwaitingDeliveryByAgent" - query := `SELECT * FROM kind_box_reqs WHERE id = ? AND sender_agent_id = ? AND status = 'assigned-sender-agent' AND deleted_at IS NULL ` - row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxReqID, agentID) + query := `SELECT * FROM kind_box_reqs WHERE id = ? AND sender_agent_id = ? AND status = ? AND deleted_at IS NULL ` + row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxReqID, agentID, entity.KindBoxReqAssignedSenderAgentStatus.String()) k, err := scanKindBoxReq(row) if err != nil { if err == sql.ErrNoRows { diff --git a/repository/mysql/kind_box_req/update.go b/repository/mysql/kind_box_req/update.go new file mode 100644 index 0000000..1dc2559 --- /dev/null +++ b/repository/mysql/kind_box_req/update.go @@ -0,0 +1,43 @@ + +package mysqlkindboxreq + +import ( + "context" + + entity "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) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) { + + const op = "mysqlkindboxreq.UpdateKindBoxReq" + + + query := `UPDATE kind_box_reqs SET + kind_box_type = ?, count_requested = ?, description = ?, deliver_refer_time_id = ?, + deliver_refer_date = ?, deliver_address_id = ? + WHERE id = ? AND benefactor_id = ? AND deleted_at IS NULL` + + + _, uErr := d.conn.Conn().ExecContext(ctx, query, + kindBoxReq.KindBoxType, kindBoxReq.CountRequested, + kindBoxReq.Description, kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate, + kindBoxReq.DeliverAddressID, kindBoxReq.ID, kindBoxReq.BenefactorID) + + + if uErr != nil { + return entity.KindBoxReq{}, richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord). + WithKind(richerror.KindUnexpected) + + } + + updatedKindBoxReq, gErr := d.GetKindBoxReqByID(ctx, kindBoxReq.ID) + if gErr != nil { + return entity.KindBoxReq{}, richerror.New(op).WithErr(gErr).WithMessage(errmsg.ErrorMsgNotFound). + WithKind(richerror.KindUnexpected) + } + + return updatedKindBoxReq, nil +} \ No newline at end of file diff --git a/service/benefactor/kind_box_req/service.go b/service/benefactor/kind_box_req/service.go index 5573272..0d1123f 100644 --- a/service/benefactor/kind_box_req/service.go +++ b/service/benefactor/kind_box_req/service.go @@ -10,6 +10,7 @@ type Repository interface { AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) DeleteKindBoxReqByID(ctx context.Context, kindBoxReqID uint) error + UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) } type Service struct { diff --git a/service/benefactor/kind_box_req/update.go b/service/benefactor/kind_box_req/update.go new file mode 100644 index 0000000..ed4dcdd --- /dev/null +++ b/service/benefactor/kind_box_req/update.go @@ -0,0 +1,37 @@ +package benefactorkindboxreqservice + +import ( + "context" + "time" + + param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" + entity "git.gocasts.ir/ebhomengo/niki/entity" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) UpdateKindBoxReq(ctx context.Context, req param.KindBoxReqUpdateRequest) (param.KindBoxReqUpdateResponse, error) { + const op = "benefactorkindboxreqservice.UpdateKindBoxReq" + + + t, tErr := time.Parse(time.DateTime, req.DeliverReferDate) + if tErr != nil { + return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(tErr).WithKind(richerror.KindInvalid) + } + + updatedKindBoxReq, err := s.repo.UpdateKindBoxReq(ctx, entity.KindBoxReq{ + ID: req.KindBoxReqID, + BenefactorID: req.BenefactorID, + KindBoxType: req.KindBoxType, + CountRequested: req.CountRequested, + Description: req.Description, + DeliverReferDate: t, + DeliverReferTimeID: req.DeliverReferTimeID, + DeliverAddressID: req.DeliverAddressID, + }) + + if err != nil { + return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return param.KindBoxReqUpdateResponse{KindBoxReq: updatedKindBoxReq}, nil +} diff --git a/validator/benefactor/kind_box_req/add.go b/validator/benefactor/kind_box_req/add.go index f37c402..6dc80bc 100644 --- a/validator/benefactor/kind_box_req/add.go +++ b/validator/benefactor/kind_box_req/add.go @@ -21,7 +21,7 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin validation.Field(&req.TypeID, validation.Required, - validation.By(v.doesTypeExist)), + validation.By(v.doesKindBoxTypeExist)), validation.Field(&req.DeliverAddressID, validation.Required, diff --git a/validator/benefactor/kind_box_req/update.go b/validator/benefactor/kind_box_req/update.go index 5c8addd..060f3b3 100644 --- a/validator/benefactor/kind_box_req/update.go +++ b/validator/benefactor/kind_box_req/update.go @@ -10,24 +10,16 @@ import ( ) func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) { - const op = "userkindboxreqvalidator.ValidateUpdateRequest" + const op = "benefactorkindboxreqvalidator.ValidateUpdateRequest" if err := validation.ValidateStruct(&req, + validation.Field(&req.KindBoxReqID, validation.Required, validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID)), validation.By(v.doesKindBoxRequestHavePendingStatus)), + validation.Field(&req.KindBoxType, validation.Required, validation.By(v.doesTypeExist)), validation.Field(&req.CountRequested, validation.Required, validation.Min(MinKindBoxReq), validation.Max(MaxKindBoxReq)), - - validation.Field(&req.BenefactorID, - validation.Required, - validation.By(v.doesBenefactorExist)), - - // validation.Field(&req.KindBoxReqID, - // validation.Required, - // validation.By(v.doesKindBoxRequestExist), - // validation.By(v.hasPendingStatus), - // validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID))), - - validation.Field(&req.TypeID, - validation.Required, - validation.By(v.doesTypeExist)), + validation.Field(&req.DeliverReferDate, validation.Required, validation.By(v.isDateValid)), + validation.Field(&req.DeliverReferTimeID, validation.Required, validation.By(v.isReferTimeIDValid)), + validation.Field(&req.DeliverAddressID, validation.Required, validation.By(v.doesAddressExist(req.BenefactorID))), + ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/benefactor/kind_box_req/validator.go b/validator/benefactor/kind_box_req/validator.go index feb1609..6168775 100644 --- a/validator/benefactor/kind_box_req/validator.go +++ b/validator/benefactor/kind_box_req/validator.go @@ -72,7 +72,7 @@ func (v Validator) doesBenefactorExist(value interface{}) error { return nil } -func (v Validator) doesTypeExist(value interface{}) error { +func (v Validator) doesKindBoxTypeExist(value interface{}) error { typeID, ok := value.(entity.KindBoxType) if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)