From 7d7a7a51fd89032903b611c5da8d44d471227000 Mon Sep 17 00:00:00 2001 From: wass3rw3rk <49894298+wass3rw3rk@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:30:47 -0600 Subject: [PATCH] fix: pagination pagination did not honor any query params that might be part of the current pagination context --- api/pagination.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/api/pagination.go b/api/pagination.go index 6abb3f3d9..6c0c12a19 100644 --- a/api/pagination.go +++ b/api/pagination.go @@ -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(), @@ -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") @@ -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, )