niki/repository/mysql/kind_box/get_all.go

48 lines
1.8 KiB
Go
Raw Normal View History

2024-07-09 17:17:04 +00:00
package mysqlkindbox
import (
"context"
"git.gocasts.ir/ebhomengo/niki/entity"
params "git.gocasts.ir/ebhomengo/niki/param"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
builder "git.gocasts.ir/ebhomengo/niki/pkg/query_builder/mysql"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (d DB) GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) {
const op = "mysqlkindbox.GetAllKindBox"
baseQuery := `SELECT * FROM kind_boxes WHERE deleted_at IS NULL`
query, args := builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
rows, qErr := d.conn.Conn().QueryContext(ctx, query, args...)
if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
defer rows.Close()
kindBoxes := make([]entity.KindBox, 0)
for rows.Next() {
kindBox, sErr := scanKindBox(rows)
if sErr != nil {
return nil, 0, richerror.New(op).WithErr(sErr).
WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
}
kindBoxes = append(kindBoxes, kindBox)
}
if rErr := rows.Err(); rErr != nil {
return nil, 0, richerror.New(op).WithErr(rErr).
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
var total uint
baseQuery = `SELECT COUNT(*) FROM kind_boxes WHERE deleted_at IS NULL`
query, args = builder.BuildGetAllQuery(baseQuery, filter, pagination, sort)
qErr = d.conn.Conn().QueryRowContext(ctx, query, args...).Scan(&total)
if qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
}
return kindBoxes, total, nil
}