2024-01-19 16:56:11 +00:00
|
|
|
package mysqladmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-14 08:41:36 +00:00
|
|
|
|
2024-01-19 16:56:11 +00:00
|
|
|
"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-01-19 16:56:11 +00:00
|
|
|
)
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
func (d *DB) AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) {
|
2024-01-19 16:56:11 +00:00
|
|
|
const op = "mysqladmin.AddAdmin"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
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,
|
2024-08-01 10:20:18 +00:00
|
|
|
admin.Email, admin.Gender, admin.Status)
|
2024-01-19 16:56:11 +00:00
|
|
|
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
|
|
|
|
}
|