Commit 4635f2c3cd374ddaf963f577535485bcddcf1bf4
1 parent
b3f624151c
Exists in
master
pagination utility
Showing
2 changed files
with
45 additions
and
42 deletions
Show diff stats
pagination.go
... | ... | @@ -0,0 +1,34 @@ |
1 | +package webutility | |
2 | + | |
3 | +import "net/http" | |
4 | + | |
5 | +type PaginationParams struct { | |
6 | + URL string | |
7 | + Offset int64 | |
8 | + Limit int64 | |
9 | + SortBy string | |
10 | + Order string | |
11 | +} | |
12 | + | |
13 | +func (p *PaginationParams) links() PaginationLinks { | |
14 | + return PaginationLinks{} | |
15 | +} | |
16 | + | |
17 | +// PaginationLinks ... | |
18 | +type PaginationLinks struct { | |
19 | + Count int64 | |
20 | + Total int64 | |
21 | + Base string `json:"base"` | |
22 | + Next string `json:"next"` | |
23 | + Prev string `json:"prev"` | |
24 | + Self string `json:"self"` | |
25 | +} | |
26 | + | |
27 | +func GetPaginationParameters(req *http.Request) (p PaginationParams) { | |
28 | + p.Offset = StringToInt64(req.FormValue("offset")) | |
29 | + p.Limit = StringToInt64(req.FormValue("limit")) | |
30 | + p.SortBy = req.FormValue("sortBy") | |
31 | + p.Order = req.FormValue("order") | |
32 | + | |
33 | + return p | |
34 | +} | ... | ... |
payload.go
... | ... | @@ -48,34 +48,6 @@ type Translation struct { |
48 | 48 | FieldsLabels map[string]string `json:"fieldsLabels"` |
49 | 49 | } |
50 | 50 | |
51 | -// PaginationLinks ... | |
52 | -type PaginationLinks struct { | |
53 | - Base string `json:"base"` | |
54 | - Next string `json:"next"` | |
55 | - Prev string `json:"prev"` | |
56 | - Self string `json:"self"` | |
57 | -} | |
58 | - | |
59 | -// PaginationParameters ... | |
60 | -type PaginationParameters struct { | |
61 | - URL string `json:"-"` | |
62 | - Offset int64 `json:"offset"` | |
63 | - Limit int64 `json:"limit"` | |
64 | - SortBy string `json:"sortBy"` | |
65 | - Order string `json:"order"` | |
66 | -} | |
67 | - | |
68 | -// GetPaginationParameters ... | |
69 | -// TODO(marko) | |
70 | -func GetPaginationParameters(req *http.Request) (p PaginationParameters) { | |
71 | - return p | |
72 | -} | |
73 | - | |
74 | -// TODO(marko) | |
75 | -func (p *PaginationParameters) paginationLinks() (links PaginationLinks) { | |
76 | - return links | |
77 | -} | |
78 | - | |
79 | 51 | // Payload ... |
80 | 52 | type Payload struct { |
81 | 53 | Method string `json:"method"` |
... | ... | @@ -85,15 +57,19 @@ type Payload struct { |
85 | 57 | Correlations []CorrelationField `json:"correlationFields"` |
86 | 58 | IDField string `json:"idField"` |
87 | 59 | |
88 | - // Pagination | |
89 | - Count int64 `json:"count"` | |
90 | - Total int64 `json:"total"` | |
91 | 60 | Links PaginationLinks `json:"_links"` |
92 | 61 | |
93 | 62 | // Data holds JSON payload. It can't be used for itteration. |
94 | 63 | Data interface{} `json:"data"` |
95 | 64 | } |
96 | 65 | |
66 | +// NewPayload returs a payload sceleton for entity described with key. | |
67 | +func NewPayload(r *http.Request, key string) Payload { | |
68 | + p := metadata[key] | |
69 | + p.Method = r.Method + " " + r.RequestURI | |
70 | + return p | |
71 | +} | |
72 | + | |
97 | 73 | func (p *Payload) addLang(code string, labels map[string]string) { |
98 | 74 | t := Translation{ |
99 | 75 | Language: code, |
... | ... | @@ -108,17 +84,10 @@ func (p *Payload) SetData(data interface{}) { |
108 | 84 | } |
109 | 85 | |
110 | 86 | // SetPaginationInfo ... |
111 | -func (p *Payload) SetPaginationInfo(count, total int64, params PaginationParameters) { | |
112 | - p.Count = count | |
113 | - p.Total = total | |
114 | - p.Links = params.paginationLinks() | |
115 | -} | |
116 | - | |
117 | -// NewPayload returs a payload sceleton for entity described with key. | |
118 | -func NewPayload(r *http.Request, key string) Payload { | |
119 | - p := metadata[key] | |
120 | - p.Method = r.Method + " " + r.RequestURI | |
121 | - return p | |
87 | +func (p *Payload) SetPaginationInfo(count, total int64, params PaginationParams) { | |
88 | + p.Links.Count = count | |
89 | + p.Links.Total = total | |
90 | + p.Links = params.links() | |
122 | 91 | } |
123 | 92 | |
124 | 93 | // InitPayloadsMetadata loads all payloads' information into 'metadata' variable. | ... | ... |