Added pager tests.

This commit is contained in:
mikestefanello 2021-12-18 18:08:04 -05:00
parent 8eb8264d6e
commit a1a54a7b7d
5 changed files with 103 additions and 19 deletions

View file

@ -10,23 +10,35 @@ import (
const (
// DefaultItemsPerPage stores the default amount of items per page
DefaultItemsPerPage = 20
// PageQueryKey stores the query key used to indicate the current page
PageQueryKey = "page"
)
// Pager provides a mechanism to allow a user to page results via a query parameter
type Pager struct {
Items int
Page int
// Items stores the total amount of items in the result set
Items int
// Page stores the current page number
Page int
// ItemsPerPage stores the amount of items to display per page
ItemsPerPage int
Pages int
// Pages stores the total amount of pages in the result set
Pages int
}
func NewPager(c echo.Context) Pager {
// NewPager creates a new Pager
func NewPager(ctx echo.Context, itemsPerPage int) Pager {
p := Pager{
ItemsPerPage: DefaultItemsPerPage,
ItemsPerPage: itemsPerPage,
Page: 1,
}
if page := c.QueryParam("page"); page != "" {
if pageInt, err := strconv.Atoi(page); err != nil {
if page := ctx.QueryParam(PageQueryKey); page != "" {
if pageInt, err := strconv.Atoi(page); err == nil {
if pageInt > 0 {
p.Page = pageInt
}
@ -36,6 +48,9 @@ func NewPager(c echo.Context) Pager {
return p
}
// SetItems sets the amount of items in total for the pager and calculate the amount
// of total pages based off on the item per page.
// This should be used rather than setting either items or pages directly.
func (p *Pager) SetItems(items int) {
p.Items = items
p.Pages = int(math.Ceil(float64(items) / float64(p.ItemsPerPage)))
@ -45,14 +60,18 @@ func (p *Pager) SetItems(items int) {
}
}
// IsBeginning determines if the pager is at the beginning of the pages
func (p *Pager) IsBeginning() bool {
return p.Page == 1
}
// IsEnd determines if the pager is at the end of the pages
func (p *Pager) IsEnd() bool {
return p.Page >= p.Pages
}
// GetOffset determines the offset of the results in order to get the items for
// the current page
func (p *Pager) GetOffset() int {
if p.Page == 0 {
p.Page = 1