2022-11-17 07:37:29 -08:00
|
|
|
package pagination
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// Pagination definition.
|
2022-11-17 07:37:29 -08:00
|
|
|
type Pagination struct {
|
|
|
|
Offset int64
|
|
|
|
PageSize int64
|
|
|
|
SortOrder string
|
|
|
|
SortBy string
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// FirstPage return a *Pagination with default values offset and page size.
|
2022-11-17 07:37:29 -08:00
|
|
|
func FirstPage() *Pagination {
|
|
|
|
return &Pagination{Offset: 0, PageSize: 50}
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// BuildPagination create a new *Pagination.
|
2022-11-17 07:37:29 -08:00
|
|
|
func BuildPagination(page, pageSize int64, sortOrder, sortBy string) *Pagination {
|
|
|
|
p := Pagination{}
|
|
|
|
p.SetPage(page).SetPageSize(pageSize).SetSortOrder(sortOrder).SetSortBy(sortBy)
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// SetPageSize set the PageSize field of the Pagination struct.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (p *Pagination) SetPageSize(limit int64) *Pagination {
|
|
|
|
p.PageSize = limit
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// SetOffset set the Offset field of the Pagination struct.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (p *Pagination) SetOffset(offset int64) *Pagination {
|
|
|
|
p.Offset = offset
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// SetPage set the Page field of the Pagination struct.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (p *Pagination) SetPage(page int64) *Pagination {
|
|
|
|
p.Offset = page * p.PageSize
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// SetSortOrder set the SortOrder field of the Pagination struct.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (p *Pagination) SetSortOrder(order string) *Pagination {
|
|
|
|
p.SortOrder = order
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// SetSortBy set the SortBy field of the Pagination struct.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (p *Pagination) SetSortBy(by string) *Pagination {
|
|
|
|
p.SortBy = by
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:06:29 -08:00
|
|
|
// GetSortInt mapping to mongodb sort values.
|
2022-11-17 07:37:29 -08:00
|
|
|
func (p *Pagination) GetSortInt() int {
|
|
|
|
if p.SortOrder == "ASC" {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|