You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
2.8 KiB
Go
121 lines
2.8 KiB
Go
// Package httputil provides HTTP utility functions for the KnowFoolery application.
|
|
package httputil
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
|
|
"knowfoolery/backend/shared/domain/types"
|
|
)
|
|
|
|
// PaginationFromQuery extracts pagination parameters from query string.
|
|
func PaginationFromQuery(c fiber.Ctx) types.Pagination {
|
|
page := c.Query("page", "1")
|
|
pageSize := c.Query("page_size", strconv.Itoa(types.DefaultPageSize))
|
|
|
|
pageNum, err := strconv.Atoi(page)
|
|
if err != nil || pageNum < 1 {
|
|
pageNum = 1
|
|
}
|
|
|
|
pageSizeNum, err := strconv.Atoi(pageSize)
|
|
if err != nil || pageSizeNum < 1 {
|
|
pageSizeNum = types.DefaultPageSize
|
|
}
|
|
if pageSizeNum > types.MaxPageSize {
|
|
pageSizeNum = types.MaxPageSize
|
|
}
|
|
|
|
return types.Pagination{
|
|
Page: pageNum,
|
|
PageSize: pageSizeNum,
|
|
}
|
|
}
|
|
|
|
// SortingParams holds sorting parameters.
|
|
type SortingParams struct {
|
|
Field string
|
|
Direction string // "asc" or "desc"
|
|
}
|
|
|
|
// DefaultSorting returns default sorting parameters.
|
|
func DefaultSorting(defaultField string) SortingParams {
|
|
return SortingParams{
|
|
Field: defaultField,
|
|
Direction: "asc",
|
|
}
|
|
}
|
|
|
|
// SortingFromQuery extracts sorting parameters from query string.
|
|
func SortingFromQuery(c fiber.Ctx, defaultField string, allowedFields []string) SortingParams {
|
|
sort := c.Query("sort", defaultField)
|
|
direction := c.Query("direction", "asc")
|
|
|
|
// Validate sort field
|
|
isAllowed := false
|
|
for _, field := range allowedFields {
|
|
if field == sort {
|
|
isAllowed = true
|
|
break
|
|
}
|
|
}
|
|
if !isAllowed {
|
|
sort = defaultField
|
|
}
|
|
|
|
// Validate direction
|
|
if direction != "asc" && direction != "desc" {
|
|
direction = "asc"
|
|
}
|
|
|
|
return SortingParams{
|
|
Field: sort,
|
|
Direction: direction,
|
|
}
|
|
}
|
|
|
|
// FilterParams holds common filter parameters.
|
|
type FilterParams struct {
|
|
Search string
|
|
Status string
|
|
DateFrom string
|
|
DateTo string
|
|
Custom map[string]string
|
|
}
|
|
|
|
// FiltersFromQuery extracts common filter parameters from query string.
|
|
func FiltersFromQuery(c fiber.Ctx) FilterParams {
|
|
return FilterParams{
|
|
Search: c.Query("search"),
|
|
Status: c.Query("status"),
|
|
DateFrom: c.Query("date_from"),
|
|
DateTo: c.Query("date_to"),
|
|
Custom: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
// WithCustomFilter adds a custom filter parameter.
|
|
func (f *FilterParams) WithCustomFilter(c fiber.Ctx, name string) *FilterParams {
|
|
if value := c.Query(name); value != "" {
|
|
f.Custom[name] = value
|
|
}
|
|
return f
|
|
}
|
|
|
|
// QueryParams holds all common query parameters.
|
|
type QueryParams struct {
|
|
Pagination types.Pagination
|
|
Sorting SortingParams
|
|
Filters FilterParams
|
|
}
|
|
|
|
// QueryParamsFromContext extracts all common query parameters.
|
|
func QueryParamsFromContext(c fiber.Ctx, defaultSortField string, allowedSortFields []string) QueryParams {
|
|
return QueryParams{
|
|
Pagination: PaginationFromQuery(c),
|
|
Sorting: SortingFromQuery(c, defaultSortField, allowedSortFields),
|
|
Filters: FiltersFromQuery(c),
|
|
}
|
|
}
|