fix issue

This commit is contained in:
Abolfazl Nourzad 2024-01-27 10:35:56 +03:30
parent 06acc01e64
commit 8f658433cc
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
7 changed files with 14 additions and 30 deletions

View File

@ -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 {

View File

@ -9,6 +9,4 @@ type KindBoxAddAfterAcceptingReqRequest struct {
Count uint
}
type KindBoxAddAfterAcceptingReqResponse struct {
KindBoxes []entity.KindBox
}
type KindBoxAddAfterAcceptingReqResponse struct{}

View File

@ -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"`

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -14,9 +14,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest)
return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err)
}
// fire new event to create a kind-box.
// get kind box req
kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
if gErr != nil {
return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err)
@ -26,7 +23,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest)
ID: kindBoxReq.ID,
KindBoxType: kindBoxReq.KindBoxType,
CountRequested: kindBoxReq.CountRequested,
CountAccepted: kindBoxReq.CountAccepted,
BenefactorID: kindBoxReq.BenefactorID,
Status: kindBoxReq.Status,
Description: kindBoxReq.Description,