2024-01-16 16:13:06 +00:00
|
|
|
package mysqlkindboxreq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-22 14:07:51 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2024-06-14 08:41:36 +00:00
|
|
|
|
2024-01-16 16:13:06 +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"
|
2024-07-30 11:05:41 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
2024-01-16 16:13:06 +00:00
|
|
|
)
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
func (d *DB) KindBoxRequestExist(ctx context.Context, id uint) (bool, error) {
|
2024-07-21 14:16:01 +00:00
|
|
|
const op = "mysqlkindboxreq.KindBoxRequestExist"
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
query := `select * from kind_box_reqs where id = ? and deleted_at is null`
|
2024-07-24 23:45:04 +00:00
|
|
|
//nolint
|
2024-07-30 11:05:41 +00:00
|
|
|
stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyKindBoxReqGetByID, query)
|
2024-07-21 14:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
row := stmt.QueryRow(id)
|
2024-01-22 14:07:51 +00:00
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
|
2024-07-30 11:05:41 +00:00
|
|
|
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)
|
2024-01-25 15:15:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).WithErr(err).
|
|
|
|
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
2024-01-25 17:59:18 +00:00
|
|
|
|
2024-01-25 15:15:53 +00:00
|
|
|
return nil
|
|
|
|
}
|