Skip to content

Commit 0b28aa2

Browse files
author
alexadrake
committed
fix
1 parent 7cd8d68 commit 0b28aa2

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

cmd/server/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ func main() {
2121
r.HandleFunc("/vendorjson", responseHeaders.HandleVendorJsonResponseHeaders).Methods(http.MethodGet)
2222
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
2323
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
24+
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
2425

25-
log.Println("Listening on :8080")
26-
if err := http.ListenAndServe(":8080", r); err != nil {
26+
log.Println("Listening on :35456")
27+
if err := http.ListenAndServe(":35456", r); err != nil {
2728
log.Fatal(err)
2829
}
2930
}

internal/pagination/service.go

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ import (
66
"strconv"
77
)
88

9-
type PaginationRequest struct {
9+
type LimitOffsetRequest struct {
1010
Limit int `json:"limit"`
1111
Offset int `json:"offset"`
1212
Page int `json:"page"`
1313
}
1414

15+
type CursorRequest struct {
16+
Cursor int `json:"cursor"`
17+
}
18+
1519
type PaginationResponse struct {
16-
NumPages int `json:"numPages"`
17-
ResultsArray []int `json:"resultsArray"`
20+
NumPages int `json:"numPages"`
21+
ResultArray []int `json:"resultArray"`
1822
}
1923

2024
const total = 20
@@ -23,7 +27,7 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
2327
queryLimit := r.FormValue("limit")
2428
queryPage := r.FormValue("page")
2529

26-
var pagination PaginationRequest
30+
var pagination LimitOffsetRequest
2731
hasBody := true
2832
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
2933
hasBody = false
@@ -38,19 +42,17 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
3842
}
3943

4044
start := (page - 1) * limit
41-
if start > total {
42-
w.WriteHeader(404)
43-
}
4445

4546
res := PaginationResponse{
46-
NumPages: total / limit,
47-
ResultsArray: make([]int, 0),
47+
NumPages: total / limit,
48+
ResultArray: make([]int, 0),
4849
}
4950

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)
5253
}
5354

55+
w.Header().Set("Content-Type", "application/json")
5456
err = json.NewEncoder(w).Encode(res)
5557
if err != nil {
5658
w.WriteHeader(500)
@@ -61,11 +63,12 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
6163
queryLimit := r.FormValue("limit")
6264
queryOffset := r.FormValue("offset")
6365

64-
var pagination PaginationRequest
66+
var pagination LimitOffsetRequest
6567
hasBody := true
6668
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
6769
hasBody = false
6870
}
71+
6972
limit, err := getValue(queryLimit, hasBody, pagination.Limit, w)
7073
if err != nil {
7174
return
@@ -75,27 +78,54 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
7578
return
7679
}
7780

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
80109
}
81110

82111
res := PaginationResponse{
83-
NumPages: total / limit,
84-
ResultsArray: make([]int, 0),
112+
NumPages: 0,
113+
ResultArray: make([]int, 0),
85114
}
86115

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)
89118
}
90119

120+
w.Header().Set("Content-Type", "application/json")
91121
err = json.NewEncoder(w).Encode(res)
92122
if err != nil {
93123
w.WriteHeader(500)
94124
}
95125
}
96126

97127
func getValue(queryValue string, hasBody bool, paginationValue int, w http.ResponseWriter) (int, error) {
98-
if hasBody && queryValue == "" {
128+
if hasBody {
99129
return paginationValue, nil
100130
} else {
101131
value, err := strconv.Atoi(queryValue)

0 commit comments

Comments
 (0)