2024-06-08 14:25:24 +00:00
|
|
|
package mysqladdress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
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-06-08 14:25:24 +00:00
|
|
|
)
|
|
|
|
|
2024-07-24 23:45:04 +00:00
|
|
|
func (d *DB) IsExistAddressByID(ctx context.Context, addressID, benefactorID uint) (bool, error) {
|
2024-06-08 14:25:24 +00:00
|
|
|
const op = "mysqladdress.IsExistAddressByID"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `select * from addresses where id = ? and benefactor_id = ? and deleted_at is null `
|
|
|
|
//nolint
|
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressIsExistByID, query)
|
|
|
|
if err != nil {
|
|
|
|
return false, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-06-08 14:25:24 +00:00
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
row := stmt.QueryRowContext(ctx, addressID, benefactorID)
|
|
|
|
_, err = scanAddress(row)
|
2024-06-08 14:25:24 +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
|
|
|
|
}
|