forked from ebhomengo/niki
🔥 fix(repository) multiple insert data & remove prepare statement (#28)
Reviewed-on: ebhomengo/niki#28 Co-authored-by: Abolfazl Nourzad <abolfazlnorzad78@gmail.com> Co-committed-by: Abolfazl Nourzad <abolfazlnorzad78@gmail.com>
This commit is contained in:
parent
3b3815ccec
commit
4943e90d77
|
@ -4,64 +4,25 @@ 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) 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 richerror.New(op).WithErr(tErr).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
|
||||
queryStr := "INSERT INTO kind_boxes (kind_box_req_id, benefactor_id, type, serial_number, status) VALUES "
|
||||
values := []any{}
|
||||
|
||||
for _, kb := range kindBoxes {
|
||||
queryStr += "(?, ?, ?, ?, ?),"
|
||||
values = append(values, kb.KindBoxReqID, kb.BenefactorID, kb.Type, kb.SerialNumber, kb.Status.String())
|
||||
}
|
||||
|
||||
for _, kindBox := range kindBoxes {
|
||||
go func(kb entity.KindBox) {
|
||||
_, 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(),
|
||||
)
|
||||
if err != nil {
|
||||
errCh <- richerror.New(op).WithErr(err).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
resultCh <- kb
|
||||
errCh <- nil
|
||||
}(kindBox)
|
||||
}
|
||||
|
||||
for i := 0; i < len(kindBoxes); i++ {
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil {
|
||||
if err := tx.Rollback(); err != nil {
|
||||
logger.L().Error("Rollback error: ", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
case <-resultCh:
|
||||
|
||||
case <-ctx.Done():
|
||||
if err := tx.Rollback(); err != nil {
|
||||
logger.L().Error("Rollback error: ", err)
|
||||
}
|
||||
|
||||
return richerror.New(op).WithErr(ctx.Err()).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
logger.L().Error("Commit error: ", err)
|
||||
// trim the last ,
|
||||
queryStr = queryStr[0 : len(queryStr)-1]
|
||||
|
||||
if _, err := d.conn.Conn().ExecContext(ctx, queryStr, values...); err != nil {
|
||||
return richerror.New(op).WithErr(err).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
|
|
@ -28,27 +28,14 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en
|
|||
return kindBoxReq, nil
|
||||
}
|
||||
|
||||
func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) (finalErr error) {
|
||||
func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error {
|
||||
op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
|
||||
statement, err := d.conn.Conn().
|
||||
Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`)
|
||||
|
||||
defer func() {
|
||||
cErr := statement.Close()
|
||||
if cErr != nil {
|
||||
finalErr = cErr
|
||||
}
|
||||
}()
|
||||
|
||||
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set count_accepted = ? , status = ? where id = ?`,
|
||||
countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
|
||||
if err != nil {
|
||||
return richerror.New(op).WithErr(err).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
_, eErr := statement.ExecContext(ctx, countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
|
||||
if eErr != nil {
|
||||
return richerror.New(op).WithErr(eErr).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -81,48 +68,25 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) (finalErr error) {
|
||||
func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error {
|
||||
op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq")
|
||||
statement, err := d.conn.Conn().
|
||||
Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`)
|
||||
defer func() {
|
||||
cErr := statement.Close()
|
||||
if cErr != nil {
|
||||
finalErr = cErr
|
||||
}
|
||||
}()
|
||||
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set description = ? , status = ? where id = ?`,
|
||||
description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
|
||||
if err != nil {
|
||||
return richerror.New(op).WithErr(err).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
_, eErr := statement.ExecContext(ctx, description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
|
||||
if eErr != nil {
|
||||
return richerror.New(op).WithErr(eErr).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) (finalErr error) {
|
||||
func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
|
||||
op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus")
|
||||
statement, err := d.conn.Conn().
|
||||
Prepare(`update kind_box_reqs set status = ? where id = ?`)
|
||||
defer func() {
|
||||
cErr := statement.Close()
|
||||
if cErr != nil {
|
||||
finalErr = cErr
|
||||
}
|
||||
}()
|
||||
_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set status = ? where id = ?`, entity.KindBoxReqPendingStatus.String(), id)
|
||||
if err != nil {
|
||||
return richerror.New(op).WithErr(err).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
_, eErr := statement.ExecContext(ctx, entity.KindBoxReqPendingStatus.String(), id)
|
||||
if eErr != nil {
|
||||
return richerror.New(op).WithErr(eErr).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue