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"
|
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) AdminExistByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) {
|
2024-01-19 16:56:11 +00:00
|
|
|
const op = "mysqlbenefactor.IsExistBenefactorByID"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `select * from admins where phone_number = ?`
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminExistByPhoneNumber, query)
|
|
|
|
if err != nil {
|
|
|
|
return false, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-19 16:56:11 +00:00
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, phoneNumber)
|
|
|
|
_, err = scanAdmin(row)
|
2024-01-19 16:56:11 +00:00
|
|
|
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-07-30 11:05:41 +00:00
|
|
|
func (d *DB) AdminExistByEmail(ctx context.Context, email string) (bool, error) {
|
2024-01-19 16:56:11 +00:00
|
|
|
const op = "mysqlbenefactor.IsExistBenefactorByID"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `select * from admins where email = ?`
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminExistByEmail, query)
|
|
|
|
if err != nil {
|
|
|
|
return false, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-19 16:56:11 +00:00
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, email)
|
|
|
|
_, err = scanAdmin(row)
|
2024-01-19 16:56:11 +00:00
|
|
|
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
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
func (d *DB) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) {
|
2024-02-20 20:34:51 +00:00
|
|
|
const op = "mysqladmin.GetAdminByID"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `select * from admins where id = ?`
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAdminGetByID, query)
|
|
|
|
if err != nil {
|
|
|
|
return entity.Admin{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, adminID)
|
2024-02-20 20:34:51 +00:00
|
|
|
admin, err := scanAdmin(row)
|
|
|
|
if err != nil {
|
2024-09-09 08:17:30 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return entity.Admin{}, richerror.New(op).WithKind(richerror.KindNotFound).
|
|
|
|
WithMessage(errmsg.ErrorMsgNotFound)
|
2024-02-20 20:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return entity.Admin{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
return admin, nil
|
|
|
|
}
|