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

58 lines
1.7 KiB
Go

package mysqlkindboxreq
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) KindBoxRequestExist(ctx context.Context, id uint) (bool, error) {
const op = "mysqlkindboxreq.KindBoxRequestExist"
query := `select * from kind_box_reqs where id = ? and deleted_at is null`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqGetByID, query)
if err != nil {
return false, richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
row := stmt.QueryRow(id)
_, sErr := scanKindBoxReq(row)
if sErr != nil {
if errors.Is(sErr, sql.ErrNoRows) {
return false, nil
}
return false, richerror.New(op).WithErr(sErr).WithMessage(errmsg.ErrorMsgCantScanQueryResult).
WithKind(richerror.KindUnexpected).WithMeta(map[string]any{"id": id})
}
return true, nil
}
func (d *DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
const op = "mysqlkindboxreq.RollbackKindBoxRequestStatus"
query := `update kind_box_reqs set status = ? where id = ?`
//nolint
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqRollbackToPendingStatus, query)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
}
_, err = stmt.ExecContext(ctx, entity.KindBoxReqPendingStatus.String(), id)
if err != nil {
return richerror.New(op).WithErr(err).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return nil
}