2024-02-03 04:56:27 +00:00
package mysqlkindboxreq
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
2024-04-28 11:27:23 +00:00
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
2024-02-03 04:56:27 +00:00
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
2024-04-28 11:27:23 +00:00
func ( d DB ) GetAllKindBoxReq ( ctx context . Context , pagination paginationparam . PaginationRequest ) ( [ ] entity . KindBoxReq , paginationparam . PaginationResponse , error ) {
2024-02-03 04:56:27 +00:00
const op = "mysqlkindboxreq.GetAllKindBoxReq"
2024-04-28 11:27:23 +00:00
// TODO: create getCount function
var count uint
2024-05-31 14:49:04 +00:00
rows , err := d . conn . Conn ( ) . QueryContext ( ctx , "SELECT COUNT(*) FROM kind_box_reqs where deleted_at is null" )
2024-04-28 11:27:23 +00:00
if err != nil {
return nil , paginationparam . PaginationResponse { } ,
richerror . New ( op ) . WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithErr ( err ) . WithKind ( richerror . KindUnexpected )
}
defer rows . Close ( )
// Iterate through the rows (should only be one) and extract the count:
for rows . Next ( ) {
err := rows . Scan ( & count )
if err != nil {
panic ( err )
}
}
if rErr := rows . Err ( ) ; rErr != nil {
return nil , paginationparam . PaginationResponse { } , richerror . New ( op ) . WithErr ( rErr ) .
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
2024-02-03 04:56:27 +00:00
// TODO - add sort and filter
2024-05-31 14:49:04 +00:00
rows , err = d . conn . Conn ( ) . QueryContext ( ctx , "select * from kind_box_reqs where deleted_at is null limit ? offset ?" , pagination . GetPageSize ( ) , pagination . GetOffset ( ) )
2024-02-03 04:56:27 +00:00
if err != nil {
2024-04-28 11:27:23 +00:00
return nil , paginationparam . PaginationResponse { } ,
2024-02-03 04:56:27 +00:00
richerror . New ( op ) . WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithErr ( err ) . WithKind ( richerror . KindUnexpected )
}
defer rows . Close ( )
// An album slice to hold data from returned rows.
var kindBoxReqs [ ] entity . KindBoxReq
// Loop through rows, using Scan to assign column data to struct fields.
for rows . Next ( ) {
kindBoxReq , sErr := scanKindBoxReq ( rows )
if sErr != nil {
2024-04-28 11:27:23 +00:00
return nil , paginationparam . PaginationResponse { } , richerror . New ( op ) . WithErr ( sErr ) .
2024-02-03 04:56:27 +00:00
WithMessage ( errmsg . ErrorMsgCantScanQueryResult ) . WithKind ( richerror . KindUnexpected )
}
kindBoxReqs = append ( kindBoxReqs , kindBoxReq )
}
if rErr := rows . Err ( ) ; rErr != nil {
2024-04-28 11:27:23 +00:00
return nil , paginationparam . PaginationResponse { } , richerror . New ( op ) . WithErr ( rErr ) .
2024-02-03 04:56:27 +00:00
WithMessage ( errmsg . ErrorMsgSomethingWentWrong ) . WithKind ( richerror . KindUnexpected )
}
2024-04-28 11:27:23 +00:00
return kindBoxReqs , paginationparam . PaginationResponse {
PageSize : pagination . GetPageSize ( ) ,
PageNumber : pagination . GetPageNumber ( ) ,
Total : count ,
} , nil
2024-02-03 04:56:27 +00:00
}