2024-01-14 15:53:37 +00:00
|
|
|
package mysqlbenefactor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-01-15 10:33:24 +00:00
|
|
|
"errors"
|
2024-06-14 08:41:36 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-14 15:53:37 +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"
|
|
|
|
)
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
func (d *DB) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) {
|
2024-01-14 15:53:37 +00:00
|
|
|
const op = "mysqlbenefactor.IsExistBenefactorByPhoneNumber"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `select * from benefactors where phone_number = ?`
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorIsExistByPhoneNumber, query)
|
|
|
|
if err != nil {
|
|
|
|
return false, entity.Benefactor{}, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-14 15:53:37 +00:00
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, phoneNumber)
|
2024-01-14 15:53:37 +00:00
|
|
|
Benefactor, err := scanBenefactor(row)
|
|
|
|
if err != nil {
|
2024-01-15 10:33:24 +00:00
|
|
|
sErr := sql.ErrNoRows
|
|
|
|
//TODO-errorsas: second argument to errors.As should not be *error
|
|
|
|
//nolint
|
|
|
|
if errors.As(err, &sErr) {
|
|
|
|
return false, entity.Benefactor{}, nil
|
2024-01-14 15:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-01-16 16:13:06 +00:00
|
|
|
func (d *DB) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) {
|
|
|
|
const op = "mysqlbenefactor.IsExistBenefactorByID"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `select * from benefactors where id = ?`
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorIsExistByID, query)
|
|
|
|
if err != nil {
|
|
|
|
return false, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-16 16:13:06 +00:00
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, id)
|
|
|
|
_, err = scanBenefactor(row)
|
2024-01-16 16:13:06 +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-01-14 15:53:37 +00:00
|
|
|
func scanBenefactor(scanner mysql.Scanner) (entity.Benefactor, error) {
|
2024-05-20 18:03:36 +00:00
|
|
|
var createdAt, updatedAt time.Time
|
2024-01-14 15:53:37 +00:00
|
|
|
var benefactor entity.Benefactor
|
2024-01-15 10:33:24 +00:00
|
|
|
// TODO - use db model and mapper between entity and db model OR use this approach
|
|
|
|
|
|
|
|
var benefactorNullableFields nullableFields
|
2024-01-14 15:53:37 +00:00
|
|
|
|
2024-01-15 10:33:24 +00:00
|
|
|
err := scanner.Scan(&benefactor.ID, &benefactorNullableFields.firstName,
|
2024-05-20 18:03:36 +00:00
|
|
|
&benefactorNullableFields.lastName, &benefactor.PhoneNumber, &benefactorNullableFields.description,
|
|
|
|
&benefactorNullableFields.email, &benefactorNullableFields.genderStr,
|
|
|
|
&benefactorNullableFields.birthdate, &createdAt, &updatedAt)
|
2024-01-14 15:53:37 +00:00
|
|
|
|
2024-01-15 10:33:24 +00:00
|
|
|
mapNotNullToBenefactor(benefactorNullableFields, &benefactor)
|
2024-01-14 15:53:37 +00:00
|
|
|
|
|
|
|
return benefactor, err
|
|
|
|
}
|
2024-01-15 10:33:24 +00:00
|
|
|
|
|
|
|
type nullableFields struct {
|
|
|
|
firstName sql.NullString
|
|
|
|
lastName sql.NullString
|
|
|
|
description sql.NullString
|
|
|
|
email sql.NullString
|
|
|
|
genderStr sql.NullString
|
|
|
|
birthdate sql.NullTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO - find the other solution.
|
|
|
|
func mapNotNullToBenefactor(data nullableFields, benefactor *entity.Benefactor) {
|
|
|
|
if data.firstName.Valid {
|
|
|
|
benefactor.FirstName = data.firstName.String
|
|
|
|
}
|
|
|
|
if data.lastName.Valid {
|
|
|
|
benefactor.LastName = data.lastName.String
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
|
2024-01-15 10:33:24 +00:00
|
|
|
if data.description.Valid {
|
|
|
|
benefactor.Description = data.description.String
|
|
|
|
}
|
|
|
|
if data.email.Valid {
|
|
|
|
benefactor.Email = data.email.String
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
|
2024-01-15 10:33:24 +00:00
|
|
|
if data.genderStr.Valid {
|
|
|
|
benefactor.Gender = entity.MapToGender(data.genderStr.String)
|
|
|
|
}
|
|
|
|
if data.birthdate.Valid {
|
2024-03-14 13:38:42 +00:00
|
|
|
benefactor.BirthDate = data.birthdate.Time
|
2024-01-15 10:33:24 +00:00
|
|
|
}
|
|
|
|
}
|