-
Notifications
You must be signed in to change notification settings - Fork 7
/
page.go
54 lines (43 loc) · 1.12 KB
/
page.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
50
51
52
53
54
package types
import (
"fmt"
"strings"
)
// Page is the page response contract
type Page[T any] struct {
Content []T `json:"content"`
TotalElements uint64 `json:"totalElements"`
}
// SortDirection is the field sort direction
type SortDirection string
const (
ASC SortDirection = "ASC"
DESC SortDirection = "DESC"
)
// Sort is the contract to sort an field
type Sort struct {
Direction SortDirection
Field string
}
// NewSort returns a new Sort
func NewSort(direction SortDirection, field string) Sort {
return Sort{direction, field}
}
// PageRequest is the contract of request page
type PageRequest struct {
Page uint16
Size uint16
Order []Sort
}
// NewPageRequest returns a new page request pointer
func NewPageRequest(page uint16, size uint16, order []Sort) *PageRequest {
return &PageRequest{page, size, order}
}
// GetOrder returns string contains concated order list
func (p *PageRequest) GetOrder() string {
orders := make([]string, 0, len(p.Order))
for _, order := range p.Order {
orders = append(orders, fmt.Sprintf("%s %s", order.Field, order.Direction))
}
return strings.Join(orders, ", ")
}