2024-05-31 14:49:04 +00:00
package mysqlkindboxreq
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"
2024-07-30 11:05:41 +00:00
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
2024-05-31 14:49:04 +00:00
)
2024-07-30 11:05:41 +00:00
func ( d * DB ) AddKindBoxReq ( ctx context . Context , kindBoxReq entity . KindBoxReq ) ( entity . KindBoxReq , error ) {
2024-05-31 14:49:04 +00:00
const op = "mysqlkindboxreq.AddKindBoxReq"
2024-07-30 11:05:41 +00:00
query := ` insert into kind_box_reqs(benefactor_id,kind_box_type,deliver_address_id,count_requested,deliver_refer_date,deliver_refer_time_id,status) values (?,?,?,?,?,?,?) `
//nolint
stmt , err := d . conn . PrepareStatement ( ctx , mysql . StatementKeyKindBoxReqAdd , query )
if err != nil {
return entity . KindBoxReq { } , richerror . New ( op ) . WithErr ( err ) .
WithMessage ( errmsg . ErrorMsgCantPrepareStatement ) . WithKind ( richerror . KindUnexpected )
}
res , err := stmt . ExecContext ( ctx ,
2024-08-01 10:20:18 +00:00
kindBoxReq . BenefactorID , kindBoxReq . KindBoxType ,
2024-07-30 11:05:41 +00:00
kindBoxReq . DeliverAddressID , kindBoxReq . CountRequested , kindBoxReq . DeliverReferDate ,
2024-08-01 10:20:18 +00:00
kindBoxReq . DeliverReferTimeID , kindBoxReq . Status )
2024-05-31 14:49:04 +00:00
if err != nil {
return entity . KindBoxReq { } , richerror . New ( op ) . WithErr ( err ) .
2024-09-15 20:17:26 +00:00
WithKind ( richerror . KindUnexpected )
2024-05-31 14:49:04 +00:00
}
//nolint
// err is always nil
id , _ := res . LastInsertId ( )
kindBoxReq . ID = uint ( id )
return kindBoxReq , nil
}