Skip to content
This repository was archived by the owner on Jun 12, 2023. It is now read-only.

Commit 6a01f5b

Browse files
authored
Merge pull request #4 from speakeasy-api/fix/spe-1358
Fix/spe 1358
2 parents 7cd8d68 + f51061d commit 6a01f5b

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ 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

2526
log.Println("Listening on :8080")
2627
if err := http.ListenAndServe(":8080", r); err != nil {

internal/pagination/service.go

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ package pagination
22

33
import (
44
"encoding/json"
5+
"math"
56
"net/http"
67
"strconv"
78
)
89

9-
type PaginationRequest struct {
10+
type LimitOffsetRequest struct {
1011
Limit int `json:"limit"`
1112
Offset int `json:"offset"`
1213
Page int `json:"page"`
1314
}
1415

16+
type CursorRequest struct {
17+
Cursor int `json:"cursor"`
18+
}
19+
1520
type PaginationResponse struct {
16-
NumPages int `json:"numPages"`
17-
ResultsArray []int `json:"resultsArray"`
21+
NumPages int `json:"numPages"`
22+
ResultArray []int `json:"resultArray"`
1823
}
1924

2025
const total = 20
@@ -23,35 +28,30 @@ func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
2328
queryLimit := r.FormValue("limit")
2429
queryPage := r.FormValue("page")
2530

26-
var pagination PaginationRequest
31+
var pagination LimitOffsetRequest
2732
hasBody := true
2833
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
2934
hasBody = false
3035
}
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
3839
}
40+
page := getValue(queryPage, hasBody, pagination.Page)
3941

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

4544
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),
4847
}
4948

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)
5251
}
5352

54-
err = json.NewEncoder(w).Encode(res)
53+
w.Header().Set("Content-Type", "application/json")
54+
err := json.NewEncoder(w).Encode(res)
5555
if err != nil {
5656
w.WriteHeader(500)
5757
}
@@ -61,48 +61,69 @@ func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
6161
queryLimit := r.FormValue("limit")
6262
queryOffset := r.FormValue("offset")
6363

64-
var pagination PaginationRequest
64+
var pagination LimitOffsetRequest
6565
hasBody := true
6666
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
6767
hasBody = false
6868
}
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),
7279
}
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)
7487
if err != nil {
75-
return
88+
w.WriteHeader(500)
7689
}
90+
}
7791

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
8099
}
81100

101+
cursor := getValue(queryCursor, hasBody, pagination.Cursor)
102+
82103
res := PaginationResponse{
83-
NumPages: total / limit,
84-
ResultsArray: make([]int, 0),
104+
NumPages: 0,
105+
ResultArray: make([]int, 0),
85106
}
86107

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)
89110
}
90111

91-
err = json.NewEncoder(w).Encode(res)
112+
w.Header().Set("Content-Type", "application/json")
113+
err := json.NewEncoder(w).Encode(res)
92114
if err != nil {
93115
w.WriteHeader(500)
94116
}
95117
}
96118

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
100122
} else {
101123
value, err := strconv.Atoi(queryValue)
102124
if err != nil {
103-
w.WriteHeader(400)
104-
return 0, err
125+
return 0
105126
}
106-
return value, nil
127+
return value
107128
}
108129
}

0 commit comments

Comments
 (0)