ch9.8
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user