Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pagination/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## To be released

* feat(pagination): add `ToURLValues` on `Request`

## v1.1.2

* feat(pagination): add `log` tag to `Request` and `Meta`
Expand Down
20 changes: 16 additions & 4 deletions pagination/request.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package pagination

import (
"net/url"
"strconv"
)

const defaultPerPage = 20

type Request struct {
Expand All @@ -21,13 +26,20 @@ func NewRequest(page, perPage int) Request {
}

// QueryLimit The limit value to use when querying the DB
func (p Request) QueryLimit() int32 {
func (r Request) QueryLimit() int32 {
// Returns an int32 so that the value can be used as a query argument without typecasting
return int32(p.PerPage)
return int32(r.PerPage)
}

// QueryOffset The offset value to use when querying the DB
func (p Request) QueryOffset() int32 {
func (r Request) QueryOffset() int32 {
// Returns an int32 so that the value can be used as a query argument without typecasting
return int32((p.Page - 1) * p.PerPage)
return int32((r.Page - 1) * r.PerPage)
}

func (r Request) ToURLValues() url.Values {
values := url.Values{}
values.Add("page", strconv.Itoa(r.Page))
values.Add("per_page", strconv.Itoa(r.PerPage))
return values
}
Loading