pagination

package
v0.0.0-...-b9f6318 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package pagination provides support for pagination requests and responses.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLimit specifies the default page size
	DefaultLimit = 10
	// MaxPageSize specifies the maximum page size
	MaxPageSize = 1000
	// PageVar specifies the query parameter name for page number
	PageVar = "page"
	// PageSizeVar specifies the query parameter name for page size
	PageSizeVar = "limit"
	Sort        = "sort"
	Order       = "order"
)

Functions

This section is empty.

Types

type Pages

type Pages struct {
	Page       int    `json:"page" form:"page"`
	Limit      int    `json:"limit" form:"limit"`
	Sort       string `json:"sort" form:"sort"`
	Order      string `json:"order" form:"order"`
	PageCount  int    `json:"page_count"`
	TotalCount int    `json:"total_count"`
}

Pages represents a paginated list of data items.

func New

func New(page, limit, total int, sort, order string) *Pages

New creates a new Pages instance. The page parameter is 1-based and refers to the current page index/number. The limit parameter refers to the number of items on each page. And the total parameter specifies the total number of data items. If total is less than 0, it means total is unknown.

func NewFromRequest

func NewFromRequest(req *http.Request) *Pages

NewFromRequest creates a Pages object using the query parameters found in the given HTTP request. count stands for the total number of items. Use -1 if this is unknown.

func NewFromRequestWithTotal

func NewFromRequestWithTotal(req *http.Request, total int) *Pages

func (*Pages) BuildLinkHeader

func (p *Pages) BuildLinkHeader(baseURL string, defaultLimit int) string

BuildLinkHeader returns an HTTP header containing the links about the pagination.

func (p *Pages) BuildLinks(baseURL string, defaultLimit int) [4]string

BuildLinks returns the first, prev, next, and last links corresponding to the pagination. A link could be an empty string if it is not needed. For example, if the pagination is at the first page, then both first and prev links will be empty.

func (*Pages) Offset

func (p *Pages) Offset() int

Offset returns the OFFSET value that can be used in a SQL statement.

func (*Pages) OrderDB

func (p *Pages) OrderDB() string

OrderDB returns the formatted string that can be used in a SQL statement for ordering.

func (*Pages) UpdateTotal

func (p *Pages) UpdateTotal(total int)

UpdateTotal update current total to use in pagination response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL