forked from ebhomengo/niki
1
0
Fork 0
niki/repository/mysql/address/exist_city.go

41 lines
1.1 KiB
Go

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"
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)
func (d *DB) IsExistCityByID(ctx context.Context, id uint) (bool, error) {
const op = "mysqladdress.IsExistCityByID"
query := `select * from cities where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyCityIsExistByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRowContext(ctx, id)
_, err = scanCity(row)
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
}