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