forked from ebhomengo/niki
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
package mysqladmin
|
|
|
|
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) AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) {
|
|
const op = "mysqladmin.AddAdmin"
|
|
|
|
query := `insert into admins(first_name,last_name,password,phone_number,
|
|
role,description,email,gender,status) values (?,?,?,?,?,?,?,?,?)`
|
|
//nolint
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminAdd, query)
|
|
if err != nil {
|
|
return entity.Admin{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
res, err := stmt.ExecContext(ctx,
|
|
admin.FirstName, admin.LastName, admin.Password,
|
|
admin.PhoneNumber, admin.Role.String(), admin.Description,
|
|
admin.Email, admin.Gender.String(), admin.Status.String())
|
|
if err != nil {
|
|
return entity.Admin{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
//nolint
|
|
// err is always nil
|
|
id, _ := res.LastInsertId()
|
|
admin.ID = uint(id)
|
|
|
|
return admin, nil
|
|
}
|