Skip to content

Commit 70f4304

Browse files
committed
Use new query param parser for pagination fields
1 parent 2941b58 commit 70f4304

File tree

3 files changed

+26
-48
lines changed

3 files changed

+26
-48
lines changed

coderd/httpapi/queryparams.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package httpapi
33
import (
44
"fmt"
55
"net/http"
6+
"strconv"
67
"strings"
78

89
"github.com/google/uuid"
@@ -29,6 +30,17 @@ func (p QueryParamParser) ValidationErrors() []Error {
2930
return p.errors
3031
}
3132

33+
func (p *QueryParamParser) ParseInteger(r *http.Request, def int, queryParam string) int {
34+
v, err := parse(r, strconv.Atoi, def, queryParam)
35+
if err != nil {
36+
p.errors = append(p.errors, Error{
37+
Field: queryParam,
38+
Detail: fmt.Sprintf("Query param %q must be a valid integer (%s)", queryParam, err.Error()),
39+
})
40+
}
41+
return v
42+
}
43+
3244
func (p *QueryParamParser) ParseUUIDorMe(r *http.Request, def uuid.UUID, me uuid.UUID, queryParam string) uuid.UUID {
3345
if r.URL.Query().Get(queryParam) == "me" {
3446
return me

coderd/pagination.go

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package coderd
22

33
import (
44
"net/http"
5-
"strconv"
65

76
"github.com/google/uuid"
87

@@ -13,53 +12,20 @@ import (
1312
// parsePagination extracts pagination query params from the http request.
1413
// If an error is encountered, the error is written to w and ok is set to false.
1514
func parsePagination(w http.ResponseWriter, r *http.Request) (p codersdk.Pagination, ok bool) {
16-
var (
17-
afterID = uuid.Nil
18-
limit = -1 // Default to no limit and return all results.
19-
offset = 0
20-
)
21-
22-
var err error
23-
if s := r.URL.Query().Get("after_id"); s != "" {
24-
afterID, err = uuid.Parse(r.URL.Query().Get("after_id"))
25-
if err != nil {
26-
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
27-
Message: "Query param 'after_id' must be a valid UUID.",
28-
Validations: []httpapi.Error{
29-
{Field: "after_id", Detail: err.Error()},
30-
},
31-
})
32-
return p, false
33-
}
34-
}
35-
if s := r.URL.Query().Get("limit"); s != "" {
36-
limit, err = strconv.Atoi(s)
37-
if err != nil {
38-
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
39-
Message: "Query param 'limit' must be a valid integer.",
40-
Validations: []httpapi.Error{
41-
{Field: "limit", Detail: err.Error()},
42-
},
43-
})
44-
return p, false
45-
}
15+
parser := httpapi.NewQueryParamParser()
16+
params := codersdk.Pagination{
17+
AfterID: parser.ParseUUID(r, uuid.Nil, "after_id"),
18+
// Limit default to "-1" which returns all results
19+
Limit: parser.ParseInteger(r, -1, "limit"),
20+
Offset: parser.ParseInteger(r, 0, "offset"),
4621
}
47-
if s := r.URL.Query().Get("offset"); s != "" {
48-
offset, err = strconv.Atoi(s)
49-
if err != nil {
50-
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
51-
Message: "Query param 'offset' must be a valid integer.",
52-
Validations: []httpapi.Error{
53-
{Field: "offset", Detail: err.Error()},
54-
},
55-
})
56-
return p, false
57-
}
22+
if len(parser.ValidationErrors()) > 0 {
23+
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
24+
Message: "Query parameters have invalid values.",
25+
Validations: parser.ValidationErrors(),
26+
})
27+
return params, false
5828
}
5929

60-
return codersdk.Pagination{
61-
AfterID: afterID,
62-
Limit: limit,
63-
Offset: offset,
64-
}, true
30+
return params, true
6531
}

coderd/workspaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) {
141141
}
142142
if len(parser.ValidationErrors()) > 0 {
143143
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
144-
Message: fmt.Sprintf("Query parameters have invalid values"),
144+
Message: "Query parameters have invalid values.",
145145
Validations: parser.ValidationErrors(),
146146
})
147147
return

0 commit comments

Comments
 (0)