forked from ebhomengo/niki
Removing from the output of the method.
This commit is contained in:
parent
2e9bc7c241
commit
52f9a4aac2
|
@ -9,6 +9,4 @@ type KindBoxAddAfterAcceptingReqRequest struct {
|
|||
Count uint
|
||||
}
|
||||
|
||||
type KindBoxAddAfterAcceptingReqResponse struct {
|
||||
KindBoxes []entity.KindBox
|
||||
}
|
||||
type KindBoxAddAfterAcceptingReqResponse struct{}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue