Skip to content

Commit e0222da

Browse files
committed
api: factorize pagination offset calculation
Every list handler duplicated the same clamping of per_page against the configured defaults and the offset computation. Move it to a PageParams method so the rules live in one place and stay consistent across endpoints. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent c062ccf commit e0222da

12 files changed

Lines changed: 25 additions & 96 deletions

File tree

‎pkg/api/api.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ type PageParams struct {
2828
PerPage int `query:"per_page" minimum:"1" doc:"Results per page"`
2929
}
3030

31+
func (p PageParams) offsetPage(cfg *config.Config) (int, int) {
32+
perPage := p.PerPage
33+
if perPage < 1 {
34+
perPage = cfg.Http.ApiPageSize
35+
}
36+
if perPage > cfg.Http.ApiPageMax {
37+
perPage = cfg.Http.ApiPageMax
38+
}
39+
offset := (p.Page - 1) * perPage
40+
41+
return offset, perPage
42+
}
43+
3144
type SearchParams struct {
3245
Q string `query:"q" doc:"Search term"`
3346
Order string `query:"order" doc:"Ordering field"`

‎pkg/api/bundles.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,7 @@ func (h *handler) ListBundles(
9393
return nil, huma.Error500InternalServerError("Internal error.")
9494
}
9595

96-
perPage := input.PerPage
97-
if perPage < 1 {
98-
perPage = h.cfg.Http.ApiPageSize
99-
}
100-
if perPage > h.cfg.Http.ApiPageMax {
101-
perPage = h.cfg.Http.ApiPageMax
102-
}
103-
offset := (input.Page - 1) * perPage
96+
offset, perPage := input.offsetPage(h.cfg)
10497

10598
var bundles []db.Bundle
10699
if err := q.Model(&bundles).

‎pkg/api/checks.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,7 @@ func (h *handler) ListChecks(
8181
return nil, huma.Error500InternalServerError("Internal error.")
8282
}
8383

84-
perPage := input.PerPage
85-
if perPage < 1 {
86-
perPage = h.cfg.Http.ApiPageSize
87-
}
88-
if perPage > h.cfg.Http.ApiPageMax {
89-
perPage = h.cfg.Http.ApiPageMax
90-
}
91-
offset := (input.Page - 1) * perPage
84+
offset, perPage := input.offsetPage(h.cfg)
9285

9386
var checks []db.Check
9487
if err := q.Select(&checks).

‎pkg/api/comments.go‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,7 @@ func (h *handler) ListPatchComments(
7777
return nil, huma.Error500InternalServerError("Internal error.")
7878
}
7979

80-
perPage := input.PerPage
81-
if perPage < 1 {
82-
perPage = h.cfg.Http.ApiPageSize
83-
}
84-
if perPage > h.cfg.Http.ApiPageMax {
85-
perPage = h.cfg.Http.ApiPageMax
86-
}
87-
offset := (input.Page - 1) * perPage
80+
offset, perPage := input.offsetPage(h.cfg)
8881

8982
var comments []db.PatchComment
9083
if err := q.Model(&comments).Relation("Submitter").
@@ -223,14 +216,7 @@ func (h *handler) ListCoverComments(
223216
return nil, huma.Error500InternalServerError("Internal error.")
224217
}
225218

226-
perPage := input.PerPage
227-
if perPage < 1 {
228-
perPage = h.cfg.Http.ApiPageSize
229-
}
230-
if perPage > h.cfg.Http.ApiPageMax {
231-
perPage = h.cfg.Http.ApiPageMax
232-
}
233-
offset := (input.Page - 1) * perPage
219+
offset, perPage := input.offsetPage(h.cfg)
234220

235221
var comments []db.CoverComment
236222
if err := q.Model(&comments).Relation("Submitter").

‎pkg/api/covers.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,7 @@ func (h *handler) ListCovers(
6262
return nil, huma.Error500InternalServerError("Internal error.")
6363
}
6464

65-
perPage := input.PerPage
66-
if perPage < 1 {
67-
perPage = h.cfg.Http.ApiPageSize
68-
}
69-
if perPage > h.cfg.Http.ApiPageMax {
70-
perPage = h.cfg.Http.ApiPageMax
71-
}
72-
offset := (input.Page - 1) * perPage
65+
offset, perPage := input.offsetPage(h.cfg)
7366

7467
var covers []db.Cover
7568
if err := sq.Model(&covers).

‎pkg/api/events.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ func (h *handler) ListEvents(
5959
return nil, huma.Error500InternalServerError("Internal error.")
6060
}
6161

62-
perPage := input.PerPage
63-
if perPage < 1 {
64-
perPage = h.cfg.Http.ApiPageSize
65-
}
66-
if perPage > h.cfg.Http.ApiPageMax {
67-
perPage = h.cfg.Http.ApiPageMax
68-
}
69-
offset := (input.Page - 1) * perPage
62+
offset, perPage := input.offsetPage(h.cfg)
7063

7164
order := "event.date DESC"
7265
if input.Order == "date" {

‎pkg/api/patches.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,7 @@ func (h *handler) ListPatches(
8080
return nil, huma.Error500InternalServerError("Internal error.")
8181
}
8282

83-
perPage := input.PerPage
84-
if perPage < 1 {
85-
perPage = h.cfg.Http.ApiPageSize
86-
}
87-
if perPage > h.cfg.Http.ApiPageMax {
88-
perPage = h.cfg.Http.ApiPageMax
89-
}
90-
offset := (input.Page - 1) * perPage
83+
offset, perPage := input.offsetPage(h.cfg)
9184

9285
var patches []db.Patch
9386
if err := sq.Model(&patches).

‎pkg/api/people.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ func (h *handler) ListPeople(
5959
return nil, huma.Error500InternalServerError("Internal error.")
6060
}
6161

62-
perPage := input.PerPage
63-
if perPage < 1 {
64-
perPage = h.cfg.Http.ApiPageSize
65-
}
66-
if perPage > h.cfg.Http.ApiPageMax {
67-
perPage = h.cfg.Http.ApiPageMax
68-
}
69-
offset := (input.Page - 1) * perPage
62+
offset, perPage := input.offsetPage(h.cfg)
7063

7164
var people []db.Person
7265
if err := sq.Model(&people).Relation("User").

‎pkg/api/projects.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,7 @@ func (h *handler) ListProjects(
119119
return nil, huma.Error500InternalServerError("Internal error.")
120120
}
121121

122-
perPage := input.PerPage
123-
if perPage < 1 {
124-
perPage = h.cfg.Http.ApiPageSize
125-
}
126-
if perPage > h.cfg.Http.ApiPageMax {
127-
perPage = h.cfg.Http.ApiPageMax
128-
}
129-
offset := (input.Page - 1) * perPage
122+
offset, perPage := input.offsetPage(h.cfg)
130123

131124
var projects []db.Project
132125
if err := sq.OrderExpr("id ASC").Offset(offset).Limit(perPage).Scan(ctx, &projects); err != nil {

‎pkg/api/series.go‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,7 @@ func (h *handler) ListSeries(
6868
return nil, huma.Error500InternalServerError("Internal error.")
6969
}
7070

71-
perPage := input.PerPage
72-
if perPage < 1 {
73-
perPage = h.cfg.Http.ApiPageSize
74-
}
75-
if perPage > h.cfg.Http.ApiPageMax {
76-
perPage = h.cfg.Http.ApiPageMax
77-
}
78-
offset := (input.Page - 1) * perPage
71+
offset, perPage := input.offsetPage(h.cfg)
7972

8073
var series []db.Series
8174
if err := sq.Model(&series).

0 commit comments

Comments
 (0)