niki/repository/mysql/benefactor/exist_benefactor.go

51 lines
1.6 KiB
Go

package mysqlbenefactor
import (
"context"
"database/sql"
"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"
"time"
)
func (d DB) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) {
const op = "mysqlbenefactor.IsExistBenefactorByPhoneNumber"
row := d.conn.Conn().QueryRowContext(ctx, `select * from benefactors where phone_number = ?`, phoneNumber)
Benefactor, err := scanBenefactor(row)
if err != nil {
if err == sql.ErrNoRows {
return false, entity.Benefactor{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
}
// TODO - log unexpected error for better observability
return false, entity.Benefactor{}, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
return true, Benefactor, nil
}
func scanBenefactor(scanner mysql.Scanner) (entity.Benefactor, error) {
var createdAt time.Time
var benefactor entity.Benefactor
var roleStr, genderStr, statusStr string
err := scanner.Scan(&benefactor.ID, &benefactor.FirstName,
&benefactor.LastName, &benefactor.PhoneNumber,
&benefactor.Address, &benefactor.Description,
&benefactor.Email, &benefactor.City, &genderStr,
&statusStr, &benefactor.Birthdate, &roleStr,
&createdAt)
benefactor.Role = entity.MapToUserRole(roleStr)
benefactor.Gender = entity.MapToGender(genderStr)
benefactor.Status = entity.MapToBenefactorStatus(statusStr)
return benefactor, err
}