Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit61410a5

Browse files
committed
Move ParsePagination from httpapi to coderd and unexport, return codersdk sturct
1 parente2a3741 commit61410a5

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
returnmsg
138135
}
139-
140-
typePaginationstruct {
141-
AfterID uuid.UUID
142-
Limitint
143-
Offsetint
144-
}
145-
146-
funcParsePagination(r*http.Request) (pPagination,errerror) {
147-
var (
148-
afterID=uuid.Nil
149-
limit=-1// Default to no limit and return all results.
150-
offset=0
151-
)
152-
153-
ifs:=r.URL.Query().Get("after_id");s!="" {
154-
afterID,err=uuid.Parse(r.URL.Query().Get("after_id"))
155-
iferr!=nil {
156-
returnp,xerrors.Errorf("after_id must be a valid uuid: %w",err.Error())
157-
}
158-
}
159-
ifs:=r.URL.Query().Get("limit");s!="" {
160-
limit,err=strconv.Atoi(s)
161-
iferr!=nil {
162-
returnp,xerrors.Errorf("limit must be an integer: %w",err.Error())
163-
}
164-
}
165-
ifs:=r.URL.Query().Get("offset");s!="" {
166-
offset,err=strconv.Atoi(s)
167-
iferr!=nil {
168-
returnp,xerrors.Errorf("offset must be an integer: %w",err.Error())
169-
}
170-
}
171-
172-
returnPagination{
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+
funcparsePagination(r*http.Request) (p codersdk.Pagination,errerror) {
14+
var (
15+
afterID=uuid.Nil
16+
limit=-1// Default to no limit and return all results.
17+
offset=0
18+
)
19+
20+
ifs:=r.URL.Query().Get("after_id");s!="" {
21+
afterID,err=uuid.Parse(r.URL.Query().Get("after_id"))
22+
iferr!=nil {
23+
returnp,xerrors.Errorf("after_id must be a valid uuid: %w",err.Error())
24+
}
25+
}
26+
ifs:=r.URL.Query().Get("limit");s!="" {
27+
limit,err=strconv.Atoi(s)
28+
iferr!=nil {
29+
returnp,xerrors.Errorf("limit must be an integer: %w",err.Error())
30+
}
31+
}
32+
ifs:=r.URL.Query().Get("offset");s!="" {
33+
offset,err=strconv.Atoi(s)
34+
iferr!=nil {
35+
returnp,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
iferr!=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
iferr!=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)

[8]ページ先頭

©2009-2025 Movatter.jp