forked from ebhomengo/niki
1
0
Fork 0

Removing from the output of the method.

This commit is contained in:
Abolfazl Nourzad 2024-01-27 11:57:16 +03:30
parent 2e9bc7c241
commit 52f9a4aac2
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
4 changed files with 13 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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