@@ -2,6 +2,7 @@ package pagination
2
2
3
3
import (
4
4
"encoding/json"
5
+ "math"
5
6
"net/http"
6
7
"strconv"
7
8
)
@@ -32,19 +33,16 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
32
33
if err := json .NewDecoder (r .Body ).Decode (& pagination ); err != nil {
33
34
hasBody = false
34
35
}
35
- limit , err := getValue (queryLimit , hasBody , pagination .Limit , w )
36
- if err != nil {
37
- return
38
- }
39
- page , err := getValue (queryPage , hasBody , pagination .Page , w )
40
- if err != nil {
41
- return
36
+ limit := getValue (queryLimit , hasBody , pagination .Limit )
37
+ if limit == 0 {
38
+ limit = 20
42
39
}
40
+ page := getValue (queryPage , hasBody , pagination .Page )
43
41
44
42
start := (page - 1 ) * limit
45
43
46
44
res := PaginationResponse {
47
- NumPages : total / limit ,
45
+ NumPages : int ( math . Ceil ( float64 ( total ) / float64 ( limit ))) ,
48
46
ResultArray : make ([]int , 0 ),
49
47
}
50
48
@@ -53,7 +51,7 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
53
51
}
54
52
55
53
w .Header ().Set ("Content-Type" , "application/json" )
56
- err = json .NewEncoder (w ).Encode (res )
54
+ err : = json .NewEncoder (w ).Encode (res )
57
55
if err != nil {
58
56
w .WriteHeader (500 )
59
57
}
@@ -69,17 +67,14 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
69
67
hasBody = false
70
68
}
71
69
72
- limit , err := getValue (queryLimit , hasBody , pagination .Limit , w )
73
- if err != nil {
74
- return
75
- }
76
- offset , err := getValue (queryOffset , hasBody , pagination .Offset , w )
77
- if err != nil {
78
- return
70
+ limit := getValue (queryLimit , hasBody , pagination .Limit )
71
+ if limit == 0 {
72
+ limit = 20
79
73
}
74
+ offset := getValue (queryOffset , hasBody , pagination .Offset )
80
75
81
76
res := PaginationResponse {
82
- NumPages : total / limit ,
77
+ NumPages : int ( math . Ceil ( float64 ( total ) / float64 ( limit ))) ,
83
78
ResultArray : make ([]int , 0 ),
84
79
}
85
80
@@ -88,7 +83,7 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
88
83
}
89
84
90
85
w .Header ().Set ("Content-Type" , "application/json" )
91
- err = json .NewEncoder (w ).Encode (res )
86
+ err : = json .NewEncoder (w ).Encode (res )
92
87
if err != nil {
93
88
w .WriteHeader (500 )
94
89
}
@@ -103,10 +98,7 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
103
98
hasBody = false
104
99
}
105
100
106
- cursor , err := getValue (queryCursor , hasBody , pagination .Cursor , w )
107
- if err != nil {
108
- return
109
- }
101
+ cursor := getValue (queryCursor , hasBody , pagination .Cursor )
110
102
111
103
res := PaginationResponse {
112
104
NumPages : 0 ,
@@ -118,21 +110,20 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
118
110
}
119
111
120
112
w .Header ().Set ("Content-Type" , "application/json" )
121
- err = json .NewEncoder (w ).Encode (res )
113
+ err : = json .NewEncoder (w ).Encode (res )
122
114
if err != nil {
123
115
w .WriteHeader (500 )
124
116
}
125
117
}
126
118
127
- func getValue (queryValue string , hasBody bool , paginationValue int , w http. ResponseWriter ) ( int , error ) {
119
+ func getValue (queryValue string , hasBody bool , paginationValue int ) int {
128
120
if hasBody {
129
- return paginationValue , nil
121
+ return paginationValue
130
122
} else {
131
123
value , err := strconv .Atoi (queryValue )
132
124
if err != nil {
133
- w .WriteHeader (400 )
134
- return 0 , err
125
+ return 0
135
126
}
136
- return value , nil
127
+ return value
137
128
}
138
129
}
0 commit comments