forked from ebhomengo/niki
1
0
Fork 0
niki/repository/mysql/kind_box/kind_box.go

77 lines
2.1 KiB
Go
Raw Normal View History

2023-12-20 15:39:25 +00:00
package mysqlkindbox
2024-01-23 07:39:58 +00:00
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
2024-01-25 15:15:53 +00:00
"git.gocasts.ir/ebhomengo/niki/logger"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
2024-01-23 07:39:58 +00:00
)
func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) ([]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).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
for _, kindBox := range kindBoxes {
go func(kb entity.KindBox) {
res, 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
}
//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:
if err != nil {
if err := tx.Rollback(); err != nil {
2024-01-25 15:15:53 +00:00
logger.L().Error("Rollback error: ", err)
}
return nil, err
}
case res := <-resultCh:
result = append(result, res)
case <-ctx.Done():
if err := tx.Rollback(); err != nil {
2024-01-25 15:15:53 +00:00
logger.L().Error("Rollback error: ", err)
}
return nil, richerror.New(op).WithErr(ctx.Err()).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
}
if err := tx.Commit(); err != nil {
2024-01-25 15:15:53 +00:00
logger.L().Error("Commit error: ", err)
return nil, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return result, nil
2024-01-23 07:39:58 +00:00
}