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

Commit 7cd8d68

Browse files
authored
Merge pull request #3 from speakeasy-api/feat/spe-1358
add offset/limit pagination
2 parents b514b52 + b3a4baa commit 7cd8d68

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

cmd/server/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/pagination"
45
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/responseHeaders"
56
"log"
67
"net/http"
@@ -18,6 +19,8 @@ func main() {
1819
r.HandleFunc("/auth", auth.HandleAuth).Methods(http.MethodPost)
1920
r.HandleFunc("/requestbody", requestbody.HandleRequestBody).Methods(http.MethodPost)
2021
r.HandleFunc("/vendorjson", responseHeaders.HandleVendorJsonResponseHeaders).Methods(http.MethodGet)
22+
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
23+
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
2124

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

internal/pagination/service.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package pagination
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"strconv"
7+
)
8+
9+
type PaginationRequest struct {
10+
Limit int `json:"limit"`
11+
Offset int `json:"offset"`
12+
Page int `json:"page"`
13+
}
14+
15+
type PaginationResponse struct {
16+
NumPages int `json:"numPages"`
17+
ResultsArray []int `json:"resultsArray"`
18+
}
19+
20+
const total = 20
21+
22+
func HandleLimitOffsetPage(w http.ResponseWriter, r *http.Request) {
23+
queryLimit := r.FormValue("limit")
24+
queryPage := r.FormValue("page")
25+
26+
var pagination PaginationRequest
27+
hasBody := true
28+
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
29+
hasBody = false
30+
}
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
38+
}
39+
40+
start := (page - 1) * limit
41+
if start > total {
42+
w.WriteHeader(404)
43+
}
44+
45+
res := PaginationResponse{
46+
NumPages: total / limit,
47+
ResultsArray: make([]int, 0),
48+
}
49+
50+
for i := start; i < total && len(res.ResultsArray) < limit; i++ {
51+
res.ResultsArray = append(res.ResultsArray, i)
52+
}
53+
54+
err = json.NewEncoder(w).Encode(res)
55+
if err != nil {
56+
w.WriteHeader(500)
57+
}
58+
}
59+
60+
func HandleLimitOffsetOffset(w http.ResponseWriter, r *http.Request) {
61+
queryLimit := r.FormValue("limit")
62+
queryOffset := r.FormValue("offset")
63+
64+
var pagination PaginationRequest
65+
hasBody := true
66+
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
67+
hasBody = false
68+
}
69+
limit, err := getValue(queryLimit, hasBody, pagination.Limit, w)
70+
if err != nil {
71+
return
72+
}
73+
offset, err := getValue(queryOffset, hasBody, pagination.Offset, w)
74+
if err != nil {
75+
return
76+
}
77+
78+
if offset > total {
79+
w.WriteHeader(404)
80+
}
81+
82+
res := PaginationResponse{
83+
NumPages: total / limit,
84+
ResultsArray: make([]int, 0),
85+
}
86+
87+
for i := offset; i < total && len(res.ResultsArray) < limit; i++ {
88+
res.ResultsArray = append(res.ResultsArray, i)
89+
}
90+
91+
err = json.NewEncoder(w).Encode(res)
92+
if err != nil {
93+
w.WriteHeader(500)
94+
}
95+
}
96+
97+
func getValue(queryValue string, hasBody bool, paginationValue int, w http.ResponseWriter) (int, error) {
98+
if hasBody && queryValue == "" {
99+
return paginationValue, nil
100+
} else {
101+
value, err := strconv.Atoi(queryValue)
102+
if err != nil {
103+
w.WriteHeader(400)
104+
return 0, err
105+
}
106+
return value, nil
107+
}
108+
}

0 commit comments

Comments
 (0)