This commit is contained in:
lbenedar
2026-03-18 18:16:31 +03:00
parent 95d46644e0
commit 5cc1196085
4 changed files with 41 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
package data
import (
"math"
"strings"
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
@@ -13,6 +14,28 @@ type Filters struct {
SortSafelist []string
}
type Metadata struct {
CurrentPage int `json:"current_page,omitempty"`
PageSize int `json:"page_size,omitempty"`
FirstPage int `json:"first_page,omitempty"`
LastPage int `json:"last_page,omitempty"`
TotalRecords int `json:"total_records,omitempty"`
}
func calculateMetadata(totalRecords, page, pageSize int) Metadata {
if totalRecords == 0 {
return Metadata{}
}
return Metadata{
CurrentPage: page,
PageSize: pageSize,
FirstPage: 1,
LastPage: int(math.Ceil(float64(totalRecords) / float64(pageSize))),
TotalRecords: totalRecords,
}
}
func ValidateFilters(v *validator.Validator, f Filters) {
v.Check(f.Page > 0, "page", "must be greater than zero")
v.Check(f.Page <= 10_000_000, "page", "must be a maximum of 10 million")