@@ -2,19 +2,24 @@ package pagination
2
2
3
3
import (
4
4
"encoding/json"
5
+ "math"
5
6
"net/http"
6
7
"strconv"
7
8
)
8
9
9
- type PaginationRequest struct {
10
+ type LimitOffsetRequest struct {
10
11
Limit int `json:"limit"`
11
12
Offset int `json:"offset"`
12
13
Page int `json:"page"`
13
14
}
14
15
16
+ type CursorRequest struct {
17
+ Cursor int `json:"cursor"`
18
+ }
19
+
15
20
type PaginationResponse struct {
16
- NumPages int `json:"numPages"`
17
- ResultsArray []int `json:"resultsArray "`
21
+ NumPages int `json:"numPages"`
22
+ ResultArray []int `json:"resultArray "`
18
23
}
19
24
20
25
const total = 20
@@ -23,35 +28,30 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
23
28
queryLimit := r .FormValue ("limit" )
24
29
queryPage := r .FormValue ("page" )
25
30
26
- var pagination PaginationRequest
31
+ var pagination LimitOffsetRequest
27
32
hasBody := true
28
33
if err := json .NewDecoder (r .Body ).Decode (& pagination ); err != nil {
29
34
hasBody = false
30
35
}
31
- limit , err := getValue (queryLimit , hasBody , pagination .Limit , w )
32
- if err != nil {
33
- return
34
- }
35
- page , err := getValue (queryPage , hasBody , pagination .Page , w )
36
- if err != nil {
37
- return
36
+ limit := getValue (queryLimit , hasBody , pagination .Limit )
37
+ if limit == 0 {
38
+ limit = 20
38
39
}
40
+ page := getValue (queryPage , hasBody , pagination .Page )
39
41
40
42
start := (page - 1 ) * limit
41
- if start > total {
42
- w .WriteHeader (404 )
43
- }
44
43
45
44
res := PaginationResponse {
46
- NumPages : total / limit ,
47
- ResultsArray : make ([]int , 0 ),
45
+ NumPages : int ( math . Ceil ( float64 ( total ) / float64 ( limit ))) ,
46
+ ResultArray : make ([]int , 0 ),
48
47
}
49
48
50
- for i := start ; i < total && len (res .ResultsArray ) < limit ; i ++ {
51
- res .ResultsArray = append (res .ResultsArray , i )
49
+ for i := start ; i < total && len (res .ResultArray ) < limit ; i ++ {
50
+ res .ResultArray = append (res .ResultArray , i )
52
51
}
53
52
54
- err = json .NewEncoder (w ).Encode (res )
53
+ w .Header ().Set ("Content-Type" , "application/json" )
54
+ err := json .NewEncoder (w ).Encode (res )
55
55
if err != nil {
56
56
w .WriteHeader (500 )
57
57
}
@@ -61,48 +61,69 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
61
61
queryLimit := r .FormValue ("limit" )
62
62
queryOffset := r .FormValue ("offset" )
63
63
64
- var pagination PaginationRequest
64
+ var pagination LimitOffsetRequest
65
65
hasBody := true
66
66
if err := json .NewDecoder (r .Body ).Decode (& pagination ); err != nil {
67
67
hasBody = false
68
68
}
69
- limit , err := getValue (queryLimit , hasBody , pagination .Limit , w )
70
- if err != nil {
71
- return
69
+
70
+ limit := getValue (queryLimit , hasBody , pagination .Limit )
71
+ if limit == 0 {
72
+ limit = 20
73
+ }
74
+ offset := getValue (queryOffset , hasBody , pagination .Offset )
75
+
76
+ res := PaginationResponse {
77
+ NumPages : int (math .Ceil (float64 (total ) / float64 (limit ))),
78
+ ResultArray : make ([]int , 0 ),
72
79
}
73
- offset , err := getValue (queryOffset , hasBody , pagination .Offset , w )
80
+
81
+ for i := offset ; i < total && len (res .ResultArray ) < limit ; i ++ {
82
+ res .ResultArray = append (res .ResultArray , i )
83
+ }
84
+
85
+ w .Header ().Set ("Content-Type" , "application/json" )
86
+ err := json .NewEncoder (w ).Encode (res )
74
87
if err != nil {
75
- return
88
+ w . WriteHeader ( 500 )
76
89
}
90
+ }
77
91
78
- if offset > total {
79
- w .WriteHeader (404 )
92
+ func HandleCursor (w http.ResponseWriter , r * http.Request ) {
93
+ queryCursor := r .FormValue ("cursor" )
94
+
95
+ var pagination CursorRequest
96
+ hasBody := true
97
+ if err := json .NewDecoder (r .Body ).Decode (& pagination ); err != nil {
98
+ hasBody = false
80
99
}
81
100
101
+ cursor := getValue (queryCursor , hasBody , pagination .Cursor )
102
+
82
103
res := PaginationResponse {
83
- NumPages : total / limit ,
84
- ResultsArray : make ([]int , 0 ),
104
+ NumPages : 0 ,
105
+ ResultArray : make ([]int , 0 ),
85
106
}
86
107
87
- for i := offset ; i < total && len (res .ResultsArray ) < limit ; i ++ {
88
- res .ResultsArray = append (res .ResultsArray , i )
108
+ for i := cursor + 1 ; i < total && len (res .ResultArray ) < 15 ; i ++ {
109
+ res .ResultArray = append (res .ResultArray , i )
89
110
}
90
111
91
- err = json .NewEncoder (w ).Encode (res )
112
+ w .Header ().Set ("Content-Type" , "application/json" )
113
+ err := json .NewEncoder (w ).Encode (res )
92
114
if err != nil {
93
115
w .WriteHeader (500 )
94
116
}
95
117
}
96
118
97
- func getValue (queryValue string , hasBody bool , paginationValue int , w http. ResponseWriter ) ( int , error ) {
98
- if hasBody && queryValue == "" {
99
- return paginationValue , nil
119
+ func getValue (queryValue string , hasBody bool , paginationValue int ) int {
120
+ if hasBody {
121
+ return paginationValue
100
122
} else {
101
123
value , err := strconv .Atoi (queryValue )
102
124
if err != nil {
103
- w .WriteHeader (400 )
104
- return 0 , err
125
+ return 0
105
126
}
106
- return value , nil
127
+ return value
107
128
}
108
129
}
0 commit comments