forked from ebhomengo/niki
1
0
Fork 0

feat(niki): benefactor get/getall kindbox sort/filter/pagination

This commit is contained in:
Iman Mirazimi 2024-07-21 17:08:54 +03:30
parent caf564d474
commit 4c52cd18c1
12 changed files with 639 additions and 32 deletions

View File

@ -31,9 +31,10 @@ import (
// @Param filter_sender_agent_id query int false "Filter by sender agent ID"
// @Param filter_deliver_refer_date query string false "Filter by deliver refer date" Format(date)
// @Param filter_deliver_address_id query int false "Filter by deliver address ID"
// @Param filter_deliver_refer_time_id query int false "Filter by deliver refer time ID"
// @Param page_number query int false "Page number"
// @Param page_size query int false "Page size"
// @Param sort_field query string false "Sort by field" Enums(id,kind_box_req_id,benefactor_id,kind_box_type,amount,serial_number,status,delivered_at,return_refer_time_id,return_refer_date,return_address_id,receiver_agent_id,returned_at,sender_agent_id,deliver_refer_date,deliver_address_id)
// @Param sort_field query string false "Sort by field" Enums(id,kind_box_req_id,benefactor_id,kind_box_type,amount,serial_number,status,delivered_at,return_refer_time_id,return_refer_date,return_address_id,receiver_agent_id,returned_at,sender_agent_id,deliver_refer_date,deliver_address_id,deliver_refer_time_id)
// @Param sort_direction query string false "Sort order" Enums(asc,desc)
// @Success 200 {object} param.KindBoxGetAllResponse
// @Failure 400 {string} "Bad request"

View File

@ -1,28 +1,63 @@
package benefactorkindboxhandler
import (
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
"net/http"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
echo "github.com/labstack/echo/v4"
queryparam "git.gocasts.ir/ebhomengo/niki/pkg/query_param"
"github.com/labstack/echo/v4"
)
// GetAll godoc
// @Summary Get all kind boxes for a benefactor
// @Summary Get all KindBoxes by benefactor
// @Description Retrieves a list of all KindBoxes with filtering, sorting, and pagination options
// @Tags KindBox
// @Accept json
// @Produce json
// @Success 200 {object} param.KindBoxGetResponse
// @Param filter_id query int false "Filter by ID"
// @Param filter_kind_box_req_id query int false "Filter by KindBox request ID"
// @Param filter_benefactor_id query int false "Filter by benefactor ID"
// @Param filter_kind_box_type query string false "Filter by KindBox type" Enums(on-table,cylindrical,stand-up)
// @Param filter_amount query int false "Filter by amount"
// @Param filter_serial_number query string false "Filter by serial number"
// @Param filter_status query string false "Filter by status" Enums(delivered,ready-to-return,assigned-receiver-agent,returned,enumerated)
// @Param filter_delivered_at query string false "Filter by delivered at" Format(date-time)
// @Param filter_return_refer_time_id query int false "Filter by return refer time ID"
// @Param filter_return_refer_date query string false "Filter by return refer date" Format(date)
// @Param filter_return_address_id query int false "Filter by return address ID"
// @Param filter_receiver_agent_id query int false "Filter by receiver agent ID"
// @Param filter_returned_at query string false "Filter by returned at" Format(date-time)
// @Param filter_sender_agent_id query int false "Filter by sender agent ID"
// @Param filter_deliver_refer_date query string false "Filter by deliver refer date" Format(date)
// @Param filter_deliver_address_id query int false "Filter by deliver address ID"
// @Param filter_deliver_refer_time_id query int false "Filter by deliver refer time ID"
// @Param page_number query int false "Page number"
// @Param page_size query int false "Page size"
// @Param sort_field query string false "Sort by field" Enums(id,kind_box_req_id,benefactor_id,kind_box_type,amount,serial_number,status,delivered_at,return_refer_time_id,return_refer_date,return_address_id,receiver_agent_id,returned_at,sender_agent_id,deliver_refer_date,deliver_address_id,deliver_refer_time_id)
// @Param sort_direction query string false "Sort order" Enums(asc,desc)
// @Success 200 {object} param.KindBoxGetAllResponse
// @Failure 400 {string} "Bad request"
// @Security AuthBearerBenefactor
// @Router /benefactor/kindboxes [get]
// @Router /benefactor/kindboxes [get].
func (h Handler) GetAll(c echo.Context) error {
var req param.KindBoxGetAllRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
req.Filter = queryparam.GetFilterParams(c)
if fieldErrors, err := h.benefactorKindBoxVld.ValidateGetAll(req); err != nil {
msg, code := httpmsg.Error(err)
return c.JSON(code, echo.Map{
"message": msg,
"errors": fieldErrors,
})
}
req.Filter["benefactor_id"] = claim.GetClaimsFromEchoContext(c).UserID
resp, sErr := h.benefactorKindBoxSvc.GetAll(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)

View File

@ -398,6 +398,12 @@ const docTemplate = `{
"name": "filter_deliver_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver refer time ID",
"name": "filter_deliver_refer_time_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
@ -427,7 +433,8 @@ const docTemplate = `{
"returned_at",
"sender_agent_id",
"deliver_refer_date",
"deliver_address_id"
"deliver_address_id",
"deliver_refer_time_id"
],
"type": "string",
"description": "Sort by field",
@ -1187,6 +1194,7 @@ const docTemplate = `{
"AuthBearerBenefactor": []
}
],
"description": "Retrieves a list of all KindBoxes with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
@ -1196,12 +1204,179 @@ const docTemplate = `{
"tags": [
"KindBox"
],
"summary": "Get all kind boxes for a benefactor",
"summary": "Get all KindBoxes by benefactor",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by KindBox request ID",
"name": "filter_kind_box_req_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
"on-table",
"cylindrical",
"stand-up"
],
"type": "string",
"description": "Filter by KindBox type",
"name": "filter_kind_box_type",
"in": "query"
},
{
"type": "integer",
"description": "Filter by amount",
"name": "filter_amount",
"in": "query"
},
{
"type": "string",
"description": "Filter by serial number",
"name": "filter_serial_number",
"in": "query"
},
{
"enum": [
"delivered",
"ready-to-return",
"assigned-receiver-agent",
"returned",
"enumerated"
],
"type": "string",
"description": "Filter by status",
"name": "filter_status",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by delivered at",
"name": "filter_delivered_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return refer time ID",
"name": "filter_return_refer_time_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by return refer date",
"name": "filter_return_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return address ID",
"name": "filter_return_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by receiver agent ID",
"name": "filter_receiver_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by returned at",
"name": "filter_returned_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by sender agent ID",
"name": "filter_sender_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by deliver refer date",
"name": "filter_deliver_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver address ID",
"name": "filter_deliver_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver refer time ID",
"name": "filter_deliver_refer_time_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page_number",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "page_size",
"in": "query"
},
{
"enum": [
"id",
"kind_box_req_id",
"benefactor_id",
"kind_box_type",
"amount",
"serial_number",
"status",
"delivered_at",
"return_refer_time_id",
"return_refer_date",
"return_address_id",
"receiver_agent_id",
"returned_at",
"sender_agent_id",
"deliver_refer_date",
"deliver_address_id",
"deliver_refer_time_id"
],
"type": "string",
"description": "Sort by field",
"name": "sort_field",
"in": "query"
},
{
"enum": [
"asc",
"desc"
],
"type": "string",
"description": "Sort order",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/benefactorkindboxparam.KindBoxGetResponse"
"$ref": "#/definitions/benefactorkindboxparam.KindBoxGetAllResponse"
}
},
"400": {
@ -2362,6 +2537,20 @@ const docTemplate = `{
}
}
},
"benefactorkindboxparam.KindBoxGetAllResponse": {
"type": "object",
"properties": {
"all_kind_box": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBox"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"benefactorkindboxparam.KindBoxGetResponse": {
"type": "object",
"properties": {

View File

@ -387,6 +387,12 @@
"name": "filter_deliver_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver refer time ID",
"name": "filter_deliver_refer_time_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
@ -416,7 +422,8 @@
"returned_at",
"sender_agent_id",
"deliver_refer_date",
"deliver_address_id"
"deliver_address_id",
"deliver_refer_time_id"
],
"type": "string",
"description": "Sort by field",
@ -1176,6 +1183,7 @@
"AuthBearerBenefactor": []
}
],
"description": "Retrieves a list of all KindBoxes with filtering, sorting, and pagination options",
"consumes": [
"application/json"
],
@ -1185,12 +1193,179 @@
"tags": [
"KindBox"
],
"summary": "Get all kind boxes for a benefactor",
"summary": "Get all KindBoxes by benefactor",
"parameters": [
{
"type": "integer",
"description": "Filter by ID",
"name": "filter_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by KindBox request ID",
"name": "filter_kind_box_req_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by benefactor ID",
"name": "filter_benefactor_id",
"in": "query"
},
{
"enum": [
"on-table",
"cylindrical",
"stand-up"
],
"type": "string",
"description": "Filter by KindBox type",
"name": "filter_kind_box_type",
"in": "query"
},
{
"type": "integer",
"description": "Filter by amount",
"name": "filter_amount",
"in": "query"
},
{
"type": "string",
"description": "Filter by serial number",
"name": "filter_serial_number",
"in": "query"
},
{
"enum": [
"delivered",
"ready-to-return",
"assigned-receiver-agent",
"returned",
"enumerated"
],
"type": "string",
"description": "Filter by status",
"name": "filter_status",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by delivered at",
"name": "filter_delivered_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return refer time ID",
"name": "filter_return_refer_time_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by return refer date",
"name": "filter_return_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by return address ID",
"name": "filter_return_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by receiver agent ID",
"name": "filter_receiver_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Filter by returned at",
"name": "filter_returned_at",
"in": "query"
},
{
"type": "integer",
"description": "Filter by sender agent ID",
"name": "filter_sender_agent_id",
"in": "query"
},
{
"type": "string",
"format": "date",
"description": "Filter by deliver refer date",
"name": "filter_deliver_refer_date",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver address ID",
"name": "filter_deliver_address_id",
"in": "query"
},
{
"type": "integer",
"description": "Filter by deliver refer time ID",
"name": "filter_deliver_refer_time_id",
"in": "query"
},
{
"type": "integer",
"description": "Page number",
"name": "page_number",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "page_size",
"in": "query"
},
{
"enum": [
"id",
"kind_box_req_id",
"benefactor_id",
"kind_box_type",
"amount",
"serial_number",
"status",
"delivered_at",
"return_refer_time_id",
"return_refer_date",
"return_address_id",
"receiver_agent_id",
"returned_at",
"sender_agent_id",
"deliver_refer_date",
"deliver_address_id",
"deliver_refer_time_id"
],
"type": "string",
"description": "Sort by field",
"name": "sort_field",
"in": "query"
},
{
"enum": [
"asc",
"desc"
],
"type": "string",
"description": "Sort order",
"name": "sort_direction",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/benefactorkindboxparam.KindBoxGetResponse"
"$ref": "#/definitions/benefactorkindboxparam.KindBoxGetAllResponse"
}
},
"400": {
@ -2351,6 +2526,20 @@
}
}
},
"benefactorkindboxparam.KindBoxGetAllResponse": {
"type": "object",
"properties": {
"all_kind_box": {
"type": "array",
"items": {
"$ref": "#/definitions/entity.KindBox"
}
},
"pagination": {
"$ref": "#/definitions/param.PaginationResponse"
}
}
},
"benefactorkindboxparam.KindBoxGetResponse": {
"type": "object",
"properties": {

View File

@ -464,6 +464,15 @@ definitions:
refresh_token:
type: string
type: object
benefactorkindboxparam.KindBoxGetAllResponse:
properties:
all_kind_box:
items:
$ref: '#/definitions/entity.KindBox'
type: array
pagination:
$ref: '#/definitions/param.PaginationResponse'
type: object
benefactorkindboxparam.KindBoxGetResponse:
properties:
amount:
@ -1003,6 +1012,10 @@ paths:
in: query
name: filter_deliver_address_id
type: integer
- description: Filter by deliver refer time ID
in: query
name: filter_deliver_refer_time_id
type: integer
- description: Page number
in: query
name: page_number
@ -1029,6 +1042,7 @@ paths:
- sender_agent_id
- deliver_refer_date
- deliver_address_id
- deliver_refer_time_id
in: query
name: sort_field
type: string
@ -1516,20 +1530,142 @@ paths:
get:
consumes:
- application/json
description: Retrieves a list of all KindBoxes with filtering, sorting, and
pagination options
parameters:
- description: Filter by ID
in: query
name: filter_id
type: integer
- description: Filter by KindBox request ID
in: query
name: filter_kind_box_req_id
type: integer
- description: Filter by benefactor ID
in: query
name: filter_benefactor_id
type: integer
- description: Filter by KindBox type
enum:
- on-table
- cylindrical
- stand-up
in: query
name: filter_kind_box_type
type: string
- description: Filter by amount
in: query
name: filter_amount
type: integer
- description: Filter by serial number
in: query
name: filter_serial_number
type: string
- description: Filter by status
enum:
- delivered
- ready-to-return
- assigned-receiver-agent
- returned
- enumerated
in: query
name: filter_status
type: string
- description: Filter by delivered at
format: date-time
in: query
name: filter_delivered_at
type: string
- description: Filter by return refer time ID
in: query
name: filter_return_refer_time_id
type: integer
- description: Filter by return refer date
format: date
in: query
name: filter_return_refer_date
type: string
- description: Filter by return address ID
in: query
name: filter_return_address_id
type: integer
- description: Filter by receiver agent ID
in: query
name: filter_receiver_agent_id
type: integer
- description: Filter by returned at
format: date-time
in: query
name: filter_returned_at
type: string
- description: Filter by sender agent ID
in: query
name: filter_sender_agent_id
type: integer
- description: Filter by deliver refer date
format: date
in: query
name: filter_deliver_refer_date
type: string
- description: Filter by deliver address ID
in: query
name: filter_deliver_address_id
type: integer
- description: Filter by deliver refer time ID
in: query
name: filter_deliver_refer_time_id
type: integer
- description: Page number
in: query
name: page_number
type: integer
- description: Page size
in: query
name: page_size
type: integer
- description: Sort by field
enum:
- id
- kind_box_req_id
- benefactor_id
- kind_box_type
- amount
- serial_number
- status
- delivered_at
- return_refer_time_id
- return_refer_date
- return_address_id
- receiver_agent_id
- returned_at
- sender_agent_id
- deliver_refer_date
- deliver_address_id
- deliver_refer_time_id
in: query
name: sort_field
type: string
- description: Sort order
enum:
- asc
- desc
in: query
name: sort_direction
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/benefactorkindboxparam.KindBoxGetResponse'
$ref: '#/definitions/benefactorkindboxparam.KindBoxGetAllResponse'
"400":
description: Bad request
schema:
type: string
security:
- AuthBearerBenefactor: []
summary: Get all kind boxes for a benefactor
summary: Get all KindBoxes by benefactor
tags:
- KindBox
/benefactor/kindboxes/{id}:

View File

@ -1,11 +1,17 @@
package benefactorkindboxparam
import entity "git.gocasts.ir/ebhomengo/niki/entity"
import (
"git.gocasts.ir/ebhomengo/niki/entity"
"git.gocasts.ir/ebhomengo/niki/param"
)
type KindBoxGetAllRequest struct {
BenefactorID uint
Pagination param.PaginationRequest
Sort param.SortRequest
Filter param.FilterRequest
}
type KindBoxGetAllResponse struct {
AllKindBox []entity.KindBox
AllKindBox []entity.KindBox `json:"all_kind_box"`
Pagination param.PaginationResponse `json:"pagination"`
}

View File

@ -2,6 +2,7 @@ package mysqlkindbox
import (
"database/sql"
"fmt"
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
@ -35,6 +36,7 @@ func scanKindBox(scanner mysql.Scanner) (entity.KindBox, error) {
&amount,
&serialNumber,
&status,
&kindBox.DeliverReferTimeID,
&kindBox.DeliverReferDate,
&kindBox.DeliverAddressID,
&kindBox.SenderAgentID,
@ -49,6 +51,7 @@ func scanKindBox(scanner mysql.Scanner) (entity.KindBox, error) {
&deletedAt,
)
if err != nil {
fmt.Println(err)
return entity.KindBox{}, err
}

View File

@ -2,20 +2,26 @@ package benefactorkindboxservice
import (
"context"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
// TODO: Pagination, Filters, Sort.
func (s Service) GetAll(ctx context.Context, req param.KindBoxGetAllRequest) (param.KindBoxGetAllResponse, error) {
const op = "userkindboxservice.GetAll"
const op = "benefactorkindboxservice.GetAll"
allKindBox, _, err := s.repo.GetAllKindBox(ctx, params.FilterRequest{}, params.PaginationRequest{}, params.SortRequest{})
allKindBox, total, err := s.repo.GetAllKindBox(ctx, req.Filter, req.Pagination, req.Sort)
if err != nil {
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
return param.KindBoxGetAllResponse{}, richerror.New(op).WithErr(err)
}
return param.KindBoxGetAllResponse{AllKindBox: allKindBox}, nil
return param.KindBoxGetAllResponse{
AllKindBox: allKindBox,
Pagination: params.PaginationResponse{
PageSize: req.Pagination.GetPageSize(),
PageNumber: req.Pagination.GetPageNumber(),
Total: total,
},
}, nil
}

View File

@ -2,13 +2,13 @@ package benefactorkindboxreqservice
import (
"context"
paginationparam "git.gocasts.ir/ebhomengo/niki/param"
params "git.gocasts.ir/ebhomengo/niki/param"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetAll(ctx context.Context, req param.GetAllRequest) (param.GetAllResponse, error) {
const op = "adminkindboxreqservice.GetAllAwaitingDelivery"
const op = "benefactorkindboxreqservice.GetAll"
allKindBoxReq, total, err := s.repo.GetAllKindBoxReq(ctx, req.Filter, req.Pagination, req.Sort)
if err != nil {
@ -18,7 +18,7 @@ func (s Service) GetAll(ctx context.Context, req param.GetAllRequest) (param.Get
return param.GetAllResponse{
AllKindBoxReq: allKindBoxReq,
Pagination: paginationparam.PaginationResponse{
Pagination: params.PaginationResponse{
PageSize: req.Pagination.GetPageSize(),
PageNumber: req.Pagination.GetPageNumber(),
Total: total,

View File

@ -14,7 +14,7 @@ func (v Validator) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]st
validFields := []string{
"id", "kind_box_req_id", "benefactor_id", "kind_box_type", "amount", "serial_number",
"status", "deliver_refer_date", "deliver_address_id", "sender_agent_id",
"status", "deliver_refer_time_id", "deliver_refer_date", "deliver_address_id", "sender_agent_id",
"delivered_at", "return_refer_time_id", "return_refer_date",
"return_address_id", "receiver_agent_id", "returned_at",
}

View File

@ -9,14 +9,20 @@ import (
validation "github.com/go-ozzo/ozzo-validation/v4"
)
func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[string]string, error) {
const op = "userkindboxvalidator.ValidateGetAllRequest"
func (v Validator) ValidateGetAll(req param.KindBoxGetAllRequest) (map[string]string, error) {
const op = "benefactorkindboxvalidator.ValidateGetAll"
validFields := []string{
"id", "kind_box_req_id", "kind_box_type", "amount", "serial_number",
"status", "deliver_refer_time_id", "deliver_refer_date", "deliver_address_id", "sender_agent_id",
"delivered_at", "return_refer_time_id", "return_refer_date",
"return_address_id", "receiver_agent_id", "returned_at",
}
if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID,
validation.Required,
validation.By(v.doesBenefactorExist)),
validation.Field(&req.Filter, validation.By(v.areFilterFieldsValid(validFields))),
validation.Field(&req.Sort, validation.By(v.areSortFieldsValid(validFields))),
); err != nil {
fieldErrors := make(map[string]string)
var errV validation.Errors

View File

@ -3,6 +3,8 @@ package benefactorkindboxvalidator
import (
"context"
"fmt"
params "git.gocasts.ir/ebhomengo/niki/param"
"slices"
"time"
"git.gocasts.ir/ebhomengo/niki/entity"
@ -137,3 +139,37 @@ func (v Validator) doesKindBoxHaveDeliveredStatus(value interface{}) error {
return nil
}
func (v Validator) areFilterFieldsValid(validFilters []string) validation.RuleFunc {
return func(value interface{}) error {
filters, ok := value.(params.FilterRequest)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
for filter := range filters {
if !slices.Contains(validFilters, filter) {
return fmt.Errorf(errmsg.ErrorMsgFiltersAreNotValid)
}
}
return nil
}
}
func (v Validator) areSortFieldsValid(validSortFields []string) validation.RuleFunc {
return func(value interface{}) error {
sort, ok := value.(params.SortRequest)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if sort.Field == "" && sort.Direction != "" {
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsRequired)
}
if sort.Direction != "" && sort.Direction != params.AscSortDirection && sort.Direction != params.DescSortDirection {
return fmt.Errorf(errmsg.ErrorMsgSortDirectionShouldBeAscOrDesc)
}
if sort.Field != "" && !slices.Contains(validSortFields, sort.Field) {
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsNotValid)
}
return nil
}
}