forked from ebhomengo/niki
fix issue
This commit is contained in:
parent
06acc01e64
commit
8f658433cc
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//nolint
|
||||||
func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc {
|
func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
|
|
|
@ -9,6 +9,4 @@ type KindBoxAddAfterAcceptingReqRequest struct {
|
||||||
Count uint
|
Count uint
|
||||||
}
|
}
|
||||||
|
|
||||||
type KindBoxAddAfterAcceptingReqResponse struct {
|
type KindBoxAddAfterAcceptingReqResponse struct{}
|
||||||
KindBoxes []entity.KindBox
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ type KindBoxReqRejectResponse struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
KindBoxType entity.KindBoxType `json:"kind_box_type"`
|
KindBoxType entity.KindBoxType `json:"kind_box_type"`
|
||||||
CountRequested uint `json:"count_requested"`
|
CountRequested uint `json:"count_requested"`
|
||||||
CountAccepted uint `json:"count_accepted"`
|
|
||||||
BenefactorID uint `json:"benefactor_id"`
|
BenefactorID uint `json:"benefactor_id"`
|
||||||
Status entity.KindBoxReqStatus `json:"status"`
|
Status entity.KindBoxReqStatus `json:"status"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
|
|
@ -2,26 +2,25 @@ package mysqlkindbox
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
"git.gocasts.ir/ebhomengo/niki/logger"
|
"git.gocasts.ir/ebhomengo/niki/logger"
|
||||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
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"
|
const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback"
|
||||||
errCh := make(chan error, len(kindBoxes))
|
errCh := make(chan error, len(kindBoxes))
|
||||||
resultCh := make(chan entity.KindBox, len(kindBoxes))
|
resultCh := make(chan entity.KindBox, len(kindBoxes))
|
||||||
tx, tErr := d.conn.Conn().Begin()
|
tx, tErr := d.conn.Conn().Begin()
|
||||||
if tErr != nil {
|
if tErr != nil {
|
||||||
return nil, richerror.New(op).WithErr(tErr).
|
return richerror.New(op).WithErr(tErr).
|
||||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, kindBox := range kindBoxes {
|
for _, kindBox := range kindBoxes {
|
||||||
go func(kb entity.KindBox) {
|
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 (?, ?, ?, ?, ?);",
|
"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(),
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint
|
|
||||||
// err is always nil
|
|
||||||
id, _ := res.LastInsertId()
|
|
||||||
kb.ID = uint(id)
|
|
||||||
|
|
||||||
resultCh <- kb
|
resultCh <- kb
|
||||||
errCh <- nil
|
errCh <- nil
|
||||||
}(kindBox)
|
}(kindBox)
|
||||||
}
|
}
|
||||||
|
|
||||||
var result []entity.KindBox
|
|
||||||
|
|
||||||
for i := 0; i < len(kindBoxes); i++ {
|
for i := 0; i < len(kindBoxes); i++ {
|
||||||
select {
|
select {
|
||||||
case err := <-errCh:
|
case err := <-errCh:
|
||||||
|
@ -52,16 +44,16 @@ func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]
|
||||||
logger.L().Error("Rollback error: ", err)
|
logger.L().Error("Rollback error: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
case res := <-resultCh:
|
case <-resultCh:
|
||||||
result = append(result, res)
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
if err := tx.Rollback(); err != nil {
|
if err := tx.Rollback(); err != nil {
|
||||||
logger.L().Error("Rollback error: ", err)
|
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)
|
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 {
|
if err := tx.Commit(); err != nil {
|
||||||
logger.L().Error("Commit error: ", err)
|
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)
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,12 +22,10 @@ func (s Service) AddKindBoxAfterAcceptingRequest(ctx context.Context, req param.
|
||||||
SerialNumber: ulid.Make().String(),
|
SerialNumber: ulid.Make().String(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
kindBoxes, err := s.repo.AddBatchKindBox(ctx, kindBoxes)
|
err := s.repo.AddBatchKindBox(ctx, kindBoxes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
return param.KindBoxAddAfterAcceptingReqResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
|
||||||
}
|
}
|
||||||
|
|
||||||
return param.KindBoxAddAfterAcceptingReqResponse{
|
return param.KindBoxAddAfterAcceptingReqResponse{}, nil
|
||||||
KindBoxes: kindBoxes,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]entity.KindBox, error)
|
AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
|
|
@ -14,9 +14,6 @@ func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest)
|
||||||
return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err)
|
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)
|
kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
|
||||||
if gErr != nil {
|
if gErr != nil {
|
||||||
return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err)
|
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,
|
ID: kindBoxReq.ID,
|
||||||
KindBoxType: kindBoxReq.KindBoxType,
|
KindBoxType: kindBoxReq.KindBoxType,
|
||||||
CountRequested: kindBoxReq.CountRequested,
|
CountRequested: kindBoxReq.CountRequested,
|
||||||
CountAccepted: kindBoxReq.CountAccepted,
|
|
||||||
BenefactorID: kindBoxReq.BenefactorID,
|
BenefactorID: kindBoxReq.BenefactorID,
|
||||||
Status: kindBoxReq.Status,
|
Status: kindBoxReq.Status,
|
||||||
Description: kindBoxReq.Description,
|
Description: kindBoxReq.Description,
|
||||||
|
|
Loading…
Reference in New Issue