niki/repository/mysql/kind_box_req/get_all.go

72 lines
2.5 KiB
Go
Raw Normal View History

package mysqlkindboxreq
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
2024-06-11 18:46:51 +00:00
orderparam "git.gocasts.ir/ebhomengo/niki/param"
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
2024-06-11 18:46:51 +00:00
func (d DB) GetAllKindBoxReq(ctx context.Context, pagination paginationparam.PaginationRequest, order orderparam.OrderRequest) ([]entity.KindBoxReq, paginationparam.PaginationResponse, error) {
const op = "mysqlkindboxreq.GetAllKindBoxReq"
// TODO: create getCount function
var count uint
rows, err := d.conn.Conn().QueryContext(ctx, "SELECT COUNT(*) FROM kind_box_reqs where deleted_at is null")
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)
}
// TODO - add sort and filter
2024-06-11 18:46:51 +00:00
rows, err = d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs where deleted_at is null limit ? offset ? order by created_at ", pagination.GetPageSize(), pagination.GetOffset(), order.GetOrder())
if err != nil {
return nil, paginationparam.PaginationResponse{},
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 {
return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithErr(sErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
kindBoxReqs = append(kindBoxReqs, kindBoxReq)
}
if rErr := rows.Err(); rErr != nil {
return nil, paginationparam.PaginationResponse{}, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return kindBoxReqs, paginationparam.PaginationResponse{
PageSize: pagination.GetPageSize(),
PageNumber: pagination.GetPageNumber(),
Total: count,
}, nil
}