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

45 lines
1.6 KiB
Go

package mysqlkindbox
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d DB) AddKindBox(ctx context.Context, kindBox entity.KindBox) error {
const op = "mysqlkindbox.AddKindBox"
_, err := d.conn.Conn().ExecContext(ctx, `insert into kind_boxes(kind_box_req_id,benefactor_id,type,status,sender_agent_id) values (?,?,?,?,?)`,
kindBox.KindBoxReqID, kindBox.BenefactorID, kindBox.Type, entity.KindBoxDeliveredStatus.String(), kindBox.SenderAgentID)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
}
return nil
}
func (d DB) AddBatchKindBox(ctx context.Context, kindBoxes []entity.KindBox) error {
const op = "mysqlkindbox.AddBatchKindBoxConcurrentlyRollback"
queryStr := "INSERT INTO kind_boxes (kind_box_req_id, benefactor_id, type, status ,deliver_refer_date,deliver_address_id,sender_agent_id,delivered_at) VALUES "
values := []any{}
for _, kb := range kindBoxes {
queryStr += "(?, ?, ?, ?, ?, ?, ?, ?),"
values = append(values, kb.KindBoxReqID, kb.BenefactorID, kb.Type, kb.Status.String(), kb.DeliverReferDate, kb.DeliverAddressID, kb.SenderAgentID, kb.DeliveredAt)
}
// 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)
}
return nil
}