Skip to content

Commit

Permalink
fix: pagination
Browse files Browse the repository at this point in the history
pagination did not honor any query params that might be part of the
current pagination context
  • Loading branch information
wass3rw3rk committed Jan 14, 2025
1 parent daf1d91 commit 7d7a7a5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions api/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (p *Pagination) SetHeaderLink(c *gin.Context) {
l := []string{}
r := c.Request

q := r.URL.Query()

hl := HeaderLink{
"first": 1,
"last": p.TotalPages(),
Expand All @@ -42,6 +44,12 @@ func (p *Pagination) SetHeaderLink(c *gin.Context) {
return
}

// delete existing paging information
q.Del("page")
q.Del("per_page")

q.Set("per_page", strconv.Itoa(p.PerPage))

// drop first, prev on the first page
if p.Page == 1 {
delete(hl, "first")
Expand All @@ -54,14 +62,17 @@ func (p *Pagination) SetHeaderLink(c *gin.Context) {
delete(hl, "next")
}

// loop over the fields that make up the header links
for rel, page := range hl {
// set the page info for the current field
q.Set("page", strconv.Itoa(page))

ls := fmt.Sprintf(
`<%s://%s%s?per_page=%d&page=%d>; rel="%s"`,
`<%s://%s%s?%s>; rel="%s"`,
resolveScheme(r),
r.Host,
r.URL.Path,
p.PerPage,
page,
q.Encode(),
rel,
)

Expand Down

0 comments on commit 7d7a7a5

Please sign in to comment.