forked from ebhomengo/niki
36 lines
1.0 KiB
Go
36 lines
1.0 KiB
Go
package mysqlbenefactor
|
|
|
|
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"
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
|
)
|
|
|
|
func (d *DB) CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error) {
|
|
const op = "mysqlbenefactor.CreateBenefactor"
|
|
|
|
query := `insert into benefactors(phone_number) values(?)`
|
|
//nolint
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorCreate, query)
|
|
if err != nil {
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
res, err := stmt.ExecContext(ctx, benefactor.PhoneNumber)
|
|
if err != nil {
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
//nolint
|
|
// error is always nil
|
|
id, _ := res.LastInsertId()
|
|
benefactor.ID = uint(id)
|
|
|
|
return benefactor, nil
|
|
}
|