forked from ebhomengo/niki
64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
package mysqlbenefactor
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"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) GetByID(ctx context.Context, id uint) (entity.Benefactor, error) {
|
|
const op = "mysqlbenefactor.GetByID"
|
|
|
|
query := `select * from benefactors where id = ?`
|
|
//nolint
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorGetByID, query)
|
|
if err != nil {
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
row := stmt.QueryRowContext(ctx, id)
|
|
bnf, err := scanBenefactor(row)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return entity.Benefactor{}, richerror.New(op).WithKind(richerror.KindNotFound).
|
|
WithMessage(errmsg.ErrorMsgNotFound)
|
|
}
|
|
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
return bnf, nil
|
|
}
|
|
|
|
func (d *DB) GetByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Benefactor, error) {
|
|
const op = "mysqlbenefactor.GetByPhoneNumber"
|
|
|
|
query := `select * from benefactors where phone_number = ?`
|
|
//nolint
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorGetByPhoneNumber, query)
|
|
if err != nil {
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
row := stmt.QueryRowContext(ctx, phoneNumber)
|
|
bnf, err := scanBenefactor(row)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return entity.Benefactor{}, richerror.New(op).WithKind(richerror.KindNotFound).
|
|
WithMessage(errmsg.ErrorMsgNotFound)
|
|
}
|
|
|
|
return entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
|
|
}
|
|
|
|
return bnf, nil
|
|
}
|