Skip to content

Commit 6b8eadb

Browse files
author
alexadrake
committed
add endpoints for pagination
1 parent 0b28aa2 commit 6b8eadb

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

internal/pagination/service.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pagination
22

33
import (
44
"encoding/json"
5+
"math"
56
"net/http"
67
"strconv"
78
)
@@ -32,19 +33,16 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
3233
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
3334
hasBody = false
3435
}
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
4239
}
40+
page := getValue(queryPage, hasBody, pagination.Page)
4341

4442
start := (page - 1) * limit
4543

4644
res := PaginationResponse{
47-
NumPages: total / limit,
45+
NumPages: int(math.Ceil(float64(total) / float64(limit))),
4846
ResultArray: make([]int, 0),
4947
}
5048

@@ -53,7 +51,7 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
5351
}
5452

5553
w.Header().Set("Content-Type", "application/json")
56-
err = json.NewEncoder(w).Encode(res)
54+
err := json.NewEncoder(w).Encode(res)
5755
if err != nil {
5856
w.WriteHeader(500)
5957
}
@@ -69,17 +67,14 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
6967
hasBody = false
7068
}
7169

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
7973
}
74+
offset := getValue(queryOffset, hasBody, pagination.Offset)
8075

8176
res := PaginationResponse{
82-
NumPages: total / limit,
77+
NumPages: int(math.Ceil(float64(total) / float64(limit))),
8378
ResultArray: make([]int, 0),
8479
}
8580

@@ -88,7 +83,7 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
8883
}
8984

9085
w.Header().Set("Content-Type", "application/json")
91-
err = json.NewEncoder(w).Encode(res)
86+
err := json.NewEncoder(w).Encode(res)
9287
if err != nil {
9388
w.WriteHeader(500)
9489
}
@@ -103,10 +98,7 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
10398
hasBody = false
10499
}
105100

106-
cursor, err := getValue(queryCursor, hasBody, pagination.Cursor, w)
107-
if err != nil {
108-
return
109-
}
101+
cursor := getValue(queryCursor, hasBody, pagination.Cursor)
110102

111103
res := PaginationResponse{
112104
NumPages: 0,
@@ -118,21 +110,20 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
118110
}
119111

120112
w.Header().Set("Content-Type", "application/json")
121-
err = json.NewEncoder(w).Encode(res)
113+
err := json.NewEncoder(w).Encode(res)
122114
if err != nil {
123115
w.WriteHeader(500)
124116
}
125117
}
126118

127-
func getValue(queryValue string, hasBody bool, paginationValue int, w http.ResponseWriter) (int, error) {
119+
func getValue(queryValue string, hasBody bool, paginationValue int) int {
128120
if hasBody {
129-
return paginationValue, nil
121+
return paginationValue
130122
} else {
131123
value, err := strconv.Atoi(queryValue)
132124
if err != nil {
133-
w.WriteHeader(400)
134-
return 0, err
125+
return 0
135126
}
136-
return value, nil
127+
return value
137128
}
138129
}

0 commit comments

Comments
 (0)