2024-01-19 16:56:11 +00:00
|
|
|
package mysqladmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2024-01-22 14:41:55 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d DB) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) {
|
|
|
|
const op = "mysqlbenefactor.IsExistBenefactorByID"
|
|
|
|
|
|
|
|
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where phone_number = ?`, phoneNumber)
|
|
|
|
|
|
|
|
admin, err := scanAdmin(row)
|
|
|
|
if err != nil {
|
|
|
|
sErr := sql.ErrNoRows
|
|
|
|
//TODO-errorsas: second argument to errors.As should not be *error
|
|
|
|
//nolint
|
|
|
|
if errors.As(err, &sErr) {
|
|
|
|
return entity.Admin{}, richerror.New(op).WithErr(sErr).
|
|
|
|
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - log unexpected error for better observability
|
|
|
|
return entity.Admin{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return admin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanAdmin(scanner mysql.Scanner) (entity.Admin, error) {
|
2024-05-17 20:16:28 +00:00
|
|
|
var createdAt, updatedAt time.Time
|
2024-01-19 16:56:11 +00:00
|
|
|
var admin entity.Admin
|
|
|
|
var roleStr, statusStr, password string
|
|
|
|
// TODO - use db model and mapper between entity and db model OR use this approach
|
|
|
|
|
|
|
|
var adminNullableFields nullableFields
|
|
|
|
|
|
|
|
err := scanner.Scan(&admin.ID, &adminNullableFields.firstName,
|
|
|
|
&adminNullableFields.lastName, &password, &admin.PhoneNumber,
|
|
|
|
&roleStr, &adminNullableFields.description,
|
|
|
|
&adminNullableFields.email, &adminNullableFields.genderStr,
|
2024-05-17 20:16:28 +00:00
|
|
|
&statusStr, &createdAt, &updatedAt)
|
2024-01-19 16:56:11 +00:00
|
|
|
|
|
|
|
admin.Role = entity.MapToAdminRole(roleStr)
|
|
|
|
admin.Status = entity.MapToAdminStatus(statusStr)
|
2024-03-14 13:38:42 +00:00
|
|
|
admin.Password = password
|
2024-01-19 16:56:11 +00:00
|
|
|
mapNotNullToAdmin(adminNullableFields, &admin)
|
|
|
|
|
|
|
|
return admin, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type nullableFields struct {
|
|
|
|
firstName sql.NullString
|
|
|
|
lastName sql.NullString
|
|
|
|
description sql.NullString
|
|
|
|
email sql.NullString
|
|
|
|
genderStr sql.NullString
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - find the other solution.
|
|
|
|
func mapNotNullToAdmin(data nullableFields, admin *entity.Admin) {
|
|
|
|
if data.firstName.Valid {
|
|
|
|
admin.FirstName = data.firstName.String
|
|
|
|
}
|
|
|
|
if data.lastName.Valid {
|
|
|
|
admin.LastName = data.lastName.String
|
|
|
|
}
|
|
|
|
if data.description.Valid {
|
|
|
|
admin.Description = data.description.String
|
|
|
|
}
|
|
|
|
if data.email.Valid {
|
|
|
|
admin.Email = data.email.String
|
|
|
|
}
|
|
|
|
if data.genderStr.Valid {
|
|
|
|
admin.Gender = entity.MapToGender(data.genderStr.String)
|
|
|
|
}
|
|
|
|
}
|