From 2e9bc7c241a6bcd53acfa60e9ad6174c2c31f467 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 10:35:56 +0330 Subject: [PATCH 1/4] remove CountAccepted from param/admin/kind_box_req/reject.go --- param/admin/kind_box_req/reject.go | 1 - service/admin/kind_box_req/reject.go | 1 - 2 files changed, 2 deletions(-) diff --git a/param/admin/kind_box_req/reject.go b/param/admin/kind_box_req/reject.go index 56119b1..a000e42 100644 --- a/param/admin/kind_box_req/reject.go +++ b/param/admin/kind_box_req/reject.go @@ -15,7 +15,6 @@ type KindBoxReqRejectResponse struct { ID uint `json:"id"` KindBoxType entity.KindBoxType `json:"kind_box_type"` CountRequested uint `json:"count_requested"` - CountAccepted uint `json:"count_accepted"` BenefactorID uint `json:"benefactor_id"` Status entity.KindBoxReqStatus `json:"status"` Description string `json:"description"` diff --git a/service/admin/kind_box_req/reject.go b/service/admin/kind_box_req/reject.go index 1734bca..0c8e66e 100644 --- a/service/admin/kind_box_req/reject.go +++ b/service/admin/kind_box_req/reject.go @@ -26,7 +26,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) ID: kindBoxReq.ID, KindBoxType: kindBoxReq.KindBoxType, CountRequested: kindBoxReq.CountRequested, - CountAccepted: kindBoxReq.CountAccepted, BenefactorID: kindBoxReq.BenefactorID, Status: kindBoxReq.Status, Description: kindBoxReq.Description, From 52f9a4aac29f2309d41e767318a99352433d1a3d Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 11:57:16 +0330 Subject: [PATCH 2/4] Removing from the output of the method. --- param/admin/kind_box/add.go | 4 +--- repository/mysql/kind_box/kind_box.go | 26 +++++++++----------------- service/admin/kind_box/add.go | 6 ++---- service/admin/kind_box/service.go | 2 +- 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/param/admin/kind_box/add.go b/param/admin/kind_box/add.go index 4ba91ff..0227fb9 100644 --- a/param/admin/kind_box/add.go +++ b/param/admin/kind_box/add.go @@ -9,6 +9,4 @@ type KindBoxAddAfterAcceptingReqRequest struct { Count uint } -type KindBoxAddAfterAcceptingReqResponse struct { - KindBoxes []entity.KindBox -} +type KindBoxAddAfterAcceptingReqResponse struct{} diff --git a/repository/mysql/kind_box/kind_box.go b/repository/mysql/kind_box/kind_box.go index c910f3f..0672d18 100644 --- a/repository/mysql/kind_box/kind_box.go +++ b/repository/mysql/kind_box/kind_box.go @@ -2,26 +2,25 @@ package mysqlkindbox import ( "context" - "git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/logger" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" ) -func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) { +func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error { const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback" errCh := make(chan error, len(kindBoxes)) resultCh := make(chan entity.KindBox, len(kindBoxes)) tx, tErr := d.conn.Conn().Begin() if tErr != nil { - return nil, richerror.New(op).WithErr(tErr). + return richerror.New(op).WithErr(tErr). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } for _, kindBox := range kindBoxes { go func(kb entity.KindBox) { - res, err := tx.ExecContext(ctx, + _, err := tx.ExecContext(ctx, "insert into kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) values (?, ?, ?, ?, ?);", kb.KindBoxReqID, kb.BenefactorID, kb.Type.String(), kb.SerialNumber, kb.Status.String(), ) @@ -32,18 +31,11 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] return } - //nolint - // err is always nil - id, _ := res.LastInsertId() - kb.ID = uint(id) - resultCh <- kb errCh <- nil }(kindBox) } - var result []entity.KindBox - for i := 0; i < len(kindBoxes); i++ { select { case err := <-errCh: @@ -52,16 +44,16 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] logger.L().Error("Rollback error: ", err) } - return nil, err + return err } - case res := <-resultCh: - result = append(result, res) + case <-resultCh: + case <-ctx.Done(): if err := tx.Rollback(); err != nil { logger.L().Error("Rollback error: ", err) } - return nil, richerror.New(op).WithErr(ctx.Err()). + return richerror.New(op).WithErr(ctx.Err()). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } } @@ -69,9 +61,9 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([] if err := tx.Commit(); err != nil { logger.L().Error("Commit error: ", err) - return nil, richerror.New(op).WithErr(err). + return richerror.New(op).WithErr(err). WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) } - return result, nil + return nil } diff --git a/service/admin/kind_box/add.go b/service/admin/kind_box/add.go index 9d59495..2cac8c5 100644 --- a/service/admin/kind_box/add.go +++ b/service/admin/kind_box/add.go @@ -22,12 +22,10 @@ func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param. SerialNumber: ulid.Make().String(), }) } - kindBoxes, err := s.repo.AddBatchKindBox(ctx, kindBoxes) + err := s.repo.AddBatchKindBox(ctx, kindBoxes) if err != nil { return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) } - return param.KindBoxAddAfterAcceptingReqResponse{ - KindBoxes: kindBoxes, - }, nil + return param.KindBoxAddAfterAcceptingReqResponse{}, nil } diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index 0b072e4..0910923 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -7,7 +7,7 @@ import ( ) type Repository interface { - AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error) + AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error } type Service struct { From d09d7f068a19f8413dd79b04292f92bdd094f441 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 13:18:28 +0330 Subject: [PATCH 3/4] fix linter error --- delivery/http_server/middleware/benefactor_authorization.go | 1 + 1 file changed, 1 insertion(+) diff --git a/delivery/http_server/middleware/benefactor_authorization.go b/delivery/http_server/middleware/benefactor_authorization.go index 0371cc0..e442cda 100644 --- a/delivery/http_server/middleware/benefactor_authorization.go +++ b/delivery/http_server/middleware/benefactor_authorization.go @@ -9,6 +9,7 @@ import ( "github.com/labstack/echo/v4" ) +//nolint func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { From 154f6e98570bd6029f38742c221c3d27583239a7 Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Sat, 27 Jan 2024 15:32:03 +0330 Subject: [PATCH 4/4] remove comment --- service/admin/kind_box_req/reject.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/service/admin/kind_box_req/reject.go b/service/admin/kind_box_req/reject.go index 0c8e66e..aac41ec 100644 --- a/service/admin/kind_box_req/reject.go +++ b/service/admin/kind_box_req/reject.go @@ -14,9 +14,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err) } - // fire new event to create a kind-box. - // get kind box req - kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID) if gErr != nil { return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err)