pagination

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package pagination is useful for simple search and pagination.

func list(c echo.Context) error {
	q := pagination.Query{
		MaxPer:     50,
		DefaultPer: 20,
	}
	c.Bind(&q)
	cond := "WHERE app_id = $1"
	qt, qa := q.GetQuery()
	if qt != "" {
		cond += " AND (name ILIKE $2 OR message ILIKE $2)"
	}
	count := modelPost.MustCount(append([]interface{}{cond, appId}, qa...)...)
	posts := []models.Post{}
	sql := cond + " ORDER BY created_at DESC " + q.LimitOffset()
	modelPost.Find(append([]interface{}{sql, appId}, qa...)...).MustQuery(&posts)
	return c.JSON(200, struct {
		Posts      []models.Post
		Pagination pagination.Result
	}{posts, q.Result(count)})
}

Index

Constants

View Source
const (
	DefaultMaxPer     = 100
	DefaultDefaultPer = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Query

type Query struct {
	Query string `query:"query"`
	Page  int    `query:"page"`
	Per   int    `query:"per"`

	MaxPer     int
	DefaultPer int
}

func (Query) GetPage

func (q Query) GetPage() int

func (Query) GetPer

func (q Query) GetPer() int

func (Query) GetQuery

func (q Query) GetQuery() (query string, args []interface{})

func (Query) LimitOffset

func (q Query) LimitOffset() string

func (Query) Result

func (q Query) Result(count int) (p Result)

type Result

type Result struct {
	CurrentPage *int
	NextPage    *int
	PrevPage    *int
	TotalPages  *int
	TotalCount  *int
	LimitValue  *int
}

Jump to

Keyboard shortcuts

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