2024-01-16 16:13:06 +00:00
package mysqlkindboxreq
import (
"context"
2024-01-22 14:07:51 +00:00
"database/sql"
"errors"
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"
)
func ( d DB ) AddKindBoxReq ( ctx context . Context , kindBoxReq entity . KindBoxReq ) ( entity . KindBoxReq , error ) {
const op = "mysqlkindboxreq.AddKindBoxReq"
res , err := d . conn . Conn ( ) . ExecContext ( ctx , ` insert into kind_box_reqs(benefactor_id,kind_box_type,address_id,count_requested,refer_date,status) values (?,?,?,?,?,?) ` ,
kindBoxReq . BenefactorID , kindBoxReq . KindBoxType . String ( ) , kindBoxReq . AddressID , kindBoxReq . CountRequested , kindBoxReq . ReferDate , kindBoxReq . Status . String ( ) )
if err != nil {
return entity . KindBoxReq { } , richerror . New ( op ) . WithErr ( err ) .
WithMessage ( errmsg . ErrorMsgNotFound ) . WithKind ( richerror . KindUnexpected )
}
//nolint
// err is always nil
id , _ := res . LastInsertId ( )
kindBoxReq . ID = uint ( id )
return kindBoxReq , nil
}
2024-01-22 08:14:57 +00:00
2024-01-26 05:52:57 +00:00
func ( d DB ) AcceptKindBoxReq ( ctx context . Context , kindBoxReqID , countAccepted uint ) ( finalErr error ) {
2024-01-22 08:14:57 +00:00
op := richerror . Op ( "mysqlkindboxreq.AcceptKindBoxReq" )
statement , err := d . conn . Conn ( ) .
2024-01-22 10:43:16 +00:00
Prepare ( ` update kind_box_reqs set count_accepted = ? , status = ? where id = ? ` )
2024-01-26 05:52:57 +00:00
2024-01-25 17:59:18 +00:00
defer func ( ) {
2024-01-26 05:52:57 +00:00
cErr := statement . Close ( )
if cErr != nil {
finalErr = cErr
}
2024-01-25 17:59:18 +00:00
} ( )
2024-01-22 08:14:57 +00:00
if err != nil {
return richerror . New ( op ) . WithErr ( err ) .
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
2024-01-22 10:43:16 +00:00
_ , eErr := statement . ExecContext ( ctx , countAccepted , entity . KindBoxReqAcceptedStatus . String ( ) , kindBoxReqID )
2024-01-22 08:14:57 +00:00
if eErr != nil {
return richerror . New ( op ) . WithErr ( eErr ) .
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
return nil
}
2024-01-22 14:07:51 +00:00
func ( d DB ) GetByID ( ctx context . Context , id uint ) ( entity . KindBoxReq , error ) {
op := richerror . Op ( "mysqlkindboxreq.GetByID" )
2024-01-22 08:14:57 +00:00
row := d . conn . Conn ( ) . QueryRowContext ( ctx , ` select * from kind_box_reqs where id = ? ` , id )
k , err := scanKindBoxReq ( row )
if err != nil {
return entity . KindBoxReq { } , richerror . New ( op ) . WithErr ( err ) .
WithMessage ( errmsg . ErrorMsgCantScanQueryResult ) . WithKind ( richerror . KindUnexpected )
}
2024-01-22 14:07:51 +00:00
2024-01-22 08:14:57 +00:00
return k , nil
}
2024-01-22 14:07:51 +00:00
func ( d DB ) KindBoxRequestExist ( id uint ) ( bool , error ) {
op := richerror . Op ( "mysqlkindboxreq.KindBoxRequestExist" )
row := d . conn . Conn ( ) . QueryRow ( ` select * from kind_box_reqs where id = ? ` , 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
}
2024-01-26 05:52:57 +00:00
func ( d DB ) RejectKindBoxReq ( ctx context . Context , kindBoxReqID uint , description string ) ( finalErr error ) {
2024-01-22 15:21:13 +00:00
op := richerror . Op ( "mysqlkindboxreq.RejectKindBoxReq" )
statement , err := d . conn . Conn ( ) .
Prepare ( ` update kind_box_reqs set description = ? , status = ? where id = ? ` )
2024-01-25 17:59:18 +00:00
defer func ( ) {
2024-01-26 05:52:57 +00:00
cErr := statement . Close ( )
if cErr != nil {
finalErr = cErr
}
2024-01-25 17:59:18 +00:00
} ( )
2024-01-22 15:21:13 +00:00
if err != nil {
return richerror . New ( op ) . WithErr ( err ) .
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
_ , eErr := statement . ExecContext ( ctx , description , entity . KindBoxReqRejectedStatus . String ( ) , kindBoxReqID )
if eErr != nil {
return richerror . New ( op ) . WithErr ( eErr ) .
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
return nil
}
2024-01-25 15:15:53 +00:00
2024-01-26 05:52:57 +00:00
func ( d DB ) RollbackKindBoxRequestStatus ( ctx context . Context , id uint ) ( finalErr error ) {
2024-01-25 15:15:53 +00:00
op := richerror . Op ( "mysqlkindboxreq.RollbackKindBoxRequestStatus" )
statement , err := d . conn . Conn ( ) .
Prepare ( ` update kind_box_reqs set status = ? where id = ? ` )
2024-01-25 17:59:18 +00:00
defer func ( ) {
2024-01-26 05:52:57 +00:00
cErr := statement . Close ( )
if cErr != nil {
finalErr = cErr
}
2024-01-25 17:59:18 +00:00
} ( )
2024-01-25 15:15:53 +00:00
if err != nil {
return richerror . New ( op ) . WithErr ( err ) .
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
_ , eErr := statement . ExecContext ( ctx , entity . KindBoxReqPendingStatus . String ( ) , id )
if eErr != nil {
return richerror . New ( op ) . WithErr ( eErr ) .
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
2024-01-25 17:59:18 +00:00
2024-01-25 15:15:53 +00:00
return nil
}