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

Commit

Permalink
Merge pull request #4 from speakeasy-api/fix/spe-1358
Browse files Browse the repository at this point in the history
Fix/spe 1358
  • Loading branch information
alexadrake authored May 26, 2023
2 parents 7cd8d68 + f51061d commit 6a01f5b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 38 deletions.
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
r.HandleFunc("/vendorjson", responseHeaders.HandleVendorJsonResponseHeaders).Methods(http.MethodGet)
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)

log.Println("Listening on :8080")
if err := http.ListenAndServe(":8080", r); err != nil {
Expand Down
97 changes: 59 additions & 38 deletions internal/pagination/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ package pagination

import (
"encoding/json"
"math"
"net/http"
"strconv"
)

type PaginationRequest struct {
type LimitOffsetRequest struct {
Limit int `json:"limit"`
Offset int `json:"offset"`
Page int `json:"page"`
}

type CursorRequest struct {
Cursor int `json:"cursor"`
}

type PaginationResponse struct {
NumPages int `json:"numPages"`
ResultsArray []int `json:"resultsArray"`
NumPages int `json:"numPages"`
ResultArray []int `json:"resultArray"`
}

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

var pagination PaginationRequest
var pagination LimitOffsetRequest
hasBody := true
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
hasBody = false
}
limit, err := getValue(queryLimit, hasBody, pagination.Limit, w)
if err != nil {
return
}
page, err := getValue(queryPage, hasBody, pagination.Page, w)
if err != nil {
return
limit := getValue(queryLimit, hasBody, pagination.Limit)
if limit == 0 {
limit = 20
}
page := getValue(queryPage, hasBody, pagination.Page)

start := (page - 1) * limit
if start > total {
w.WriteHeader(404)
}

res := PaginationResponse{
NumPages: total / limit,
ResultsArray: make([]int, 0),
NumPages: int(math.Ceil(float64(total) / float64(limit))),
ResultArray: make([]int, 0),
}

for i := start; i < total && len(res.ResultsArray) < limit; i++ {
res.ResultsArray = append(res.ResultsArray, i)
for i := start; i < total && len(res.ResultArray) < limit; i++ {
res.ResultArray = append(res.ResultArray, i)
}

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

var pagination PaginationRequest
var pagination LimitOffsetRequest
hasBody := true
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
hasBody = false
}
limit, err := getValue(queryLimit, hasBody, pagination.Limit, w)
if err != nil {
return

limit := getValue(queryLimit, hasBody, pagination.Limit)
if limit == 0 {
limit = 20
}
offset := getValue(queryOffset, hasBody, pagination.Offset)

res := PaginationResponse{
NumPages: int(math.Ceil(float64(total) / float64(limit))),
ResultArray: make([]int, 0),
}
offset, err := getValue(queryOffset, hasBody, pagination.Offset, w)

for i := offset; i < total && len(res.ResultArray) < limit; i++ {
res.ResultArray = append(res.ResultArray, i)
}

w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(res)
if err != nil {
return
w.WriteHeader(500)
}
}

if offset > total {
w.WriteHeader(404)
func HandleCursor(w http.ResponseWriter, r *http.Request) {
queryCursor := r.FormValue("cursor")

var pagination CursorRequest
hasBody := true
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
hasBody = false
}

cursor := getValue(queryCursor, hasBody, pagination.Cursor)

res := PaginationResponse{
NumPages: total / limit,
ResultsArray: make([]int, 0),
NumPages: 0,
ResultArray: make([]int, 0),
}

for i := offset; i < total && len(res.ResultsArray) < limit; i++ {
res.ResultsArray = append(res.ResultsArray, i)
for i := cursor + 1; i < total && len(res.ResultArray) < 15; i++ {
res.ResultArray = append(res.ResultArray, i)
}

err = json.NewEncoder(w).Encode(res)
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(res)
if err != nil {
w.WriteHeader(500)
}
}

func getValue(queryValue string, hasBody bool, paginationValue int, w http.ResponseWriter) (int, error) {
if hasBody && queryValue == "" {
return paginationValue, nil
func getValue(queryValue string, hasBody bool, paginationValue int) int {
if hasBody {
return paginationValue
} else {
value, err := strconv.Atoi(queryValue)
if err != nil {
w.WriteHeader(400)
return 0, err
return 0
}
return value, nil
return value
}
}

0 comments on commit 6a01f5b

Please sign in to comment.