Skip to content

Commit

Permalink
Rename pageSize to perPage
Browse files Browse the repository at this point in the history
  • Loading branch information
sc-zenokerr committed Oct 2, 2024
1 parent 018632e commit 373d2bb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
45 changes: 24 additions & 21 deletions pagination/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,33 @@ along with the total number of items and other Metadata.
* it is NOT responsible for fetching the data from the database.
* it is NOT responsible for splitting a result set into pages.

The `PageRequest` struct is used to define the page number and the page size when requesting a page of items.
The `PageRequest` struct is used to define the page number and the number of items per page. Used when requesting a page
of items.

The `Paginated` struct is used to return a page of items along with the total number of items.

// Example data returned

```json
{
"data": [],
"meta": {
"current_page": 1,
"next_page": 1,
"page_size": 20,
"per_page": 20,
"prev_page": 1,
"total_count": 0,
"total_pages": 1
}
}
```


### Usage

#### Example of the first page of data of a small dataset:

In this example, we create a new `Paginated` struct with a slice of `Item` and a `PageRequest` struct.

* The total number of items in the database is 4.
* The `PageRequest` struct specifies that we want the first page with 10 items per page.
* The `items` slice contains 4 items (the same as the number of total items, but less than a full page).
Expand All @@ -49,13 +51,13 @@ In this example, we create a new `Paginated` struct with a slice of `Item` and a
package main

import (
"fmt"
"github.com/Scalingo/go-utils/pagination"
"fmt"
"github.com/Scalingo/go-utils/pagination"
)

type Item struct {
ID int
Name string
ID int
Name string
}

func main() {
Expand All @@ -65,33 +67,34 @@ func main() {
{ID: 3, Name: "Item 3"},
{ID: 4, Name: "Item 4"},
}
p := pagination.New[[]Item](items, pagination.NewPageRequest(1, 10), 4)
fmt.Println(p)

p := pagination.New[[]Item](items, pagination.NewPageRequest(1, 10), 4)
fmt.Println(p)
}
```

#### An example of the second page of data of a larger dataset:

In this example:

* The total number of items in the database is 123.
* The `PageRequest` struct specifies that we want the second page with 10 items per page.
* The `items` slice contains 10 items (the second page of data).

```go
items := []Item{
{ID: 21, Name: "Item 21"},
{ID: 22, Name: "Item 22"},
{ID: 23, Name: "Item 23"},
{ID: 24, Name: "Item 24"},
...
{ID: 29, Name: "Item 29"},
{ID: 30, Name: "Item 30"},
items := []Item{
{ID: 21, Name: "Item 21"},
{ID: 22, Name: "Item 22"},
{ID: 23, Name: "Item 23"},
{ID: 24, Name: "Item 24"},
...
{ID: 29, Name: "Item 29"},
{ID: 30, Name: "Item 30"},
}
p := pagination.New[[]Item](items, pagination.NewPageRequest(2, 10), 123)

p := pagination.New[[]Item](items, pagination.NewPageRequest(2, 10), 123)
```

In both examples we rely on the database query to return the correct page of data. The `Paginated` struct is used to
In both examples we rely on the database query to return the correct page of data. The `Paginated` struct is used to
transport the data to the client.

6 changes: 3 additions & 3 deletions pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "math"

type Meta struct {
CurrentPage int `json:"current_page"`
PageSize int `json:"page_size"`
PerPage int `json:"per_page"`
PrevPage int `json:"prev_page"`
NextPage int `json:"next_page"`
TotalPages int `json:"total_pages"`
Expand All @@ -22,7 +22,7 @@ func New[T any](data T, pageRequest Request, totalCount int64) Paginated[T] {
prevPage = 1
}

totalPages := int(math.Ceil(float64(totalCount) / float64(pageRequest.PageSize)))
totalPages := int(math.Ceil(float64(totalCount) / float64(pageRequest.PerPage)))
if totalPages == 0 {
// We always want at least one page, even if it is empty
totalPages = 1
Expand All @@ -37,7 +37,7 @@ func New[T any](data T, pageRequest Request, totalCount int64) Paginated[T] {
Data: data,
Meta: Meta{
CurrentPage: pageRequest.Page,
PageSize: pageRequest.PageSize,
PerPage: pageRequest.PerPage,
PrevPage: prevPage,
NextPage: nextPage,
TotalPages: totalPages,
Expand Down
8 changes: 4 additions & 4 deletions pagination/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type testTypeA []int

func TestNewPaginated(t *testing.T) {
t.Run("new paginated pageSize 10 empty", func(t *testing.T) {
t.Run("new paginated perPage 10 empty", func(t *testing.T) {
// Given
data := testTypeA{}
pageRequest := NewRequest(1, 10)
Expand All @@ -23,7 +23,7 @@ func TestNewPaginated(t *testing.T) {
Data: testTypeA{},
Meta: Meta{
CurrentPage: 1,
PageSize: 10,
PerPage: 10,
PrevPage: 1,
NextPage: 1,
TotalPages: 1,
Expand All @@ -32,7 +32,7 @@ func TestNewPaginated(t *testing.T) {
}, got)
})

t.Run("new paginated pageSize 10 one full page", func(t *testing.T) {
t.Run("new paginated perPage 10 one full page", func(t *testing.T) {
// Given
data := testTypeA{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
pageRequest := NewRequest(1, 10)
Expand All @@ -46,7 +46,7 @@ func TestNewPaginated(t *testing.T) {
Data: testTypeA{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
Meta: Meta{
CurrentPage: 1,
PageSize: 10,
PerPage: 10,
PrevPage: 1,
NextPage: 1,
TotalPages: 1,
Expand Down
20 changes: 10 additions & 10 deletions pagination/request.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package pagination

const defaultPageSize = 20
const defaultPerPage = 20

type Request struct {
Page int // page requested (default 1)
PageSize int // Number of items per page
Page int // page requested (default 1)
PerPage int // Number of items per page
}

func NewRequest(page, pageSize int) Request {
func NewRequest(page, perPage int) Request {
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = defaultPageSize
if perPage < 1 {
perPage = defaultPerPage
}
return Request{
Page: page,
PageSize: pageSize,
Page: page,
PerPage: perPage,
}
}

func (p Request) Limit() int {
return p.PageSize
return p.PerPage
}

func (p Request) Offset() int {
return (p.Page - 1) * p.PageSize
return (p.Page - 1) * p.PerPage
}
10 changes: 5 additions & 5 deletions pagination/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ func TestNewPageRequest(t *testing.T) {
t.Run("creates new page request correctly initialised", func(t *testing.T) {
got := NewRequest(2, 22)
require.Equal(t, Request{
Page: 2,
PageSize: 22,
Page: 2,
PerPage: 22,
}, got)
})

t.Run("creates new page request with defaults when badly initialised", func(t *testing.T) {
got := NewRequest(0, 0)
require.Equal(t, Request{
Page: 1,
PageSize: 20,
Page: 1,
PerPage: 20,
}, got)
})
}

func TestPageRequest_Limit(t *testing.T) {
t.Run("get limit same size as initialised pageSize", func(t *testing.T) {
t.Run("get limit same size as initialised perPage", func(t *testing.T) {
// Given
p := NewRequest(1, 20)

Expand Down

0 comments on commit 373d2bb

Please sign in to comment.