2024-01-19 16:56:11 +00:00
|
|
|
package mysqladmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2024-01-22 14:41:55 +00:00
|
|
|
|
2024-02-20 20:34:51 +00:00
|
|
|
entity "git.gocasts.ir/ebhomengo/niki/entity"
|
2024-01-19 16:56:11 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d DB) AdminExistByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) {
|
|
|
|
const op = "mysqlbenefactor.IsExistBenefactorByID"
|
|
|
|
|
|
|
|
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where phone_number = ?`, phoneNumber)
|
|
|
|
|
|
|
|
_, 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 false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - log unexpected error for better observability
|
|
|
|
return false, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DB) AdminExistByEmail(ctx context.Context, email string) (bool, error) {
|
|
|
|
const op = "mysqlbenefactor.IsExistBenefactorByID"
|
|
|
|
|
|
|
|
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where email = ?`, email)
|
|
|
|
|
|
|
|
_, 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 false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - log unexpected error for better observability
|
|
|
|
return false, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
|
|
|
|
func (d DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) {
|
|
|
|
const op = "mysqladmin.GetAdminByID"
|
|
|
|
|
|
|
|
row := d.conn.Conn().QueryRowContext(ctx, `select * from admins where id = ?`, adminID)
|
|
|
|
|
|
|
|
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{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - log unexpected error for better observability
|
|
|
|
return entity.Admin{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return admin, nil
|
|
|
|
}
|