-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_response.go
61 lines (50 loc) · 1.06 KB
/
content_response.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
55
56
57
58
59
60
61
package service
import (
"math"
)
type ContentResponse struct {
Content []interface{} `json:"content"`
First bool `json:"first"`
Last bool `json:"last"`
TotalPages int `json:"total_pages"`
ContentResponseMeta
}
type ContentResponseMeta struct {
Size int `json:"size"`
Total int `json:"total"`
Page int `json:"current_page"`
}
func ToContentResponse(
resources []interface{},
meta ContentResponseMeta,
) ContentResponse {
if meta.Total == 0 {
meta.Total = len(resources)
}
if meta.Size == 0 {
meta.Size = 25
}
if meta.Page == 0 {
meta.Page = 1
}
pages := meta.Total / meta.Size
total_pages := float64(pages)
if pages <= 0 {
total_pages = math.Ceil(total_pages)
}
last := false
if len(resources) == 0 || int(total_pages) == meta.Page {
last = true
}
first := false
if meta.Page == 1 {
first = true
}
return ContentResponse{
Content: resources,
First: first,
Last: last,
TotalPages: int(total_pages),
ContentResponseMeta: meta,
}
}