Skip to content

Commit 61410a5

Browse files
committed
Move ParsePagination from httpapi to coderd and unexport, return codersdk sturct
1 parent e2a3741 commit 61410a5

File tree

4 files changed

+46
-44
lines changed

4 files changed

+46
-44
lines changed

coderd/httpapi/httpapi.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ import (
88
"net/http"
99
"reflect"
1010
"regexp"
11-
"strconv"
1211
"strings"
1312

1413
"github.com/go-playground/validator/v10"
15-
"github.com/google/uuid"
16-
"golang.org/x/xerrors"
1714
)
1815

1916
var (
@@ -136,42 +133,3 @@ func WebsocketCloseSprintf(format string, vars ...any) string {
136133

137134
return msg
138135
}
139-
140-
type Pagination struct {
141-
AfterID uuid.UUID
142-
Limit int
143-
Offset int
144-
}
145-
146-
func ParsePagination(r *http.Request) (p Pagination, err error) {
147-
var (
148-
afterID = uuid.Nil
149-
limit = -1 // Default to no limit and return all results.
150-
offset = 0
151-
)
152-
153-
if s := r.URL.Query().Get("after_id"); s != "" {
154-
afterID, err = uuid.Parse(r.URL.Query().Get("after_id"))
155-
if err != nil {
156-
return p, xerrors.Errorf("after_id must be a valid uuid: %w", err.Error())
157-
}
158-
}
159-
if s := r.URL.Query().Get("limit"); s != "" {
160-
limit, err = strconv.Atoi(s)
161-
if err != nil {
162-
return p, xerrors.Errorf("limit must be an integer: %w", err.Error())
163-
}
164-
}
165-
if s := r.URL.Query().Get("offset"); s != "" {
166-
offset, err = strconv.Atoi(s)
167-
if err != nil {
168-
return p, xerrors.Errorf("offset must be an integer: %w", err.Error())
169-
}
170-
}
171-
172-
return Pagination{
173-
AfterID: afterID,
174-
Limit: limit,
175-
Offset: offset,
176-
}, nil
177-
}

coderd/pagination.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package coderd
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
7+
"github.com/google/uuid"
8+
"golang.org/x/xerrors"
9+
10+
"github.com/coder/coder/codersdk"
11+
)
12+
13+
func parsePagination(r *http.Request) (p codersdk.Pagination, err error) {
14+
var (
15+
afterID = uuid.Nil
16+
limit = -1 // Default to no limit and return all results.
17+
offset = 0
18+
)
19+
20+
if s := r.URL.Query().Get("after_id"); s != "" {
21+
afterID, err = uuid.Parse(r.URL.Query().Get("after_id"))
22+
if err != nil {
23+
return p, xerrors.Errorf("after_id must be a valid uuid: %w", err.Error())
24+
}
25+
}
26+
if s := r.URL.Query().Get("limit"); s != "" {
27+
limit, err = strconv.Atoi(s)
28+
if err != nil {
29+
return p, xerrors.Errorf("limit must be an integer: %w", err.Error())
30+
}
31+
}
32+
if s := r.URL.Query().Get("offset"); s != "" {
33+
offset, err = strconv.Atoi(s)
34+
if err != nil {
35+
return p, xerrors.Errorf("offset must be an integer: %w", err.Error())
36+
}
37+
}
38+
39+
return codersdk.Pagination{
40+
AfterID: afterID,
41+
Limit: limit,
42+
Offset: offset,
43+
}, nil
44+
}

coderd/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (api *api) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
7575
func (api *api) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Request) {
7676
template := httpmw.TemplateParam(r)
7777

78-
paginationParams, err := httpapi.ParsePagination(r)
78+
paginationParams, err := parsePagination(r)
7979
if err != nil {
8080
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{Message: fmt.Sprintf("parse pagination request: %s", err.Error())})
8181
return

coderd/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
109109
statusFilter = r.URL.Query().Get("status")
110110
)
111111

112-
paginationParams, err := httpapi.ParsePagination(r)
112+
paginationParams, err := parsePagination(r)
113113
if err != nil {
114114
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{Message: fmt.Sprintf("parse pagination request: %s", err.Error())})
115115
return

0 commit comments

Comments
 (0)