/
filters.go
49 lines (40 loc) · 1.08 KB
/
filters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package data
import (
"math"
"github.com/bxffour/crest-countries/internal/validator"
)
type Filters struct {
Page int
PageSize int
}
type Metadata struct {
CurrentPage int `json:"currentPage"`
PageSize int `json:"pageSize"`
FirstPage int `json:"firstPage"`
LastPage int `json:"lastPage"`
TotalRecords int `json:"totalRecords"`
}
func ValidateFilters(v *validator.Validator, f Filters) {
v.Check(f.Page != 0, "page", "must be greater than 0")
v.Check(f.Page <= 250, "page", "must be a maximum of 250")
v.Check(f.PageSize != 0, "page_size", "must be greater than 0")
v.Check(f.PageSize <= 250, "page_size", "must be a maximum of 250")
}
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 (f Filters) limit() int {
return f.PageSize
}
func (f Filters) offset() int {
return (f.Page - 1) * f.PageSize
}