added seasons list
This commit is contained in:
96
internal/db/paginate.go
Normal file
96
internal/db/paginate.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package db
|
||||
|
||||
import "github.com/uptrace/bun"
|
||||
|
||||
type PageOpts struct {
|
||||
Page int
|
||||
PerPage int
|
||||
Order bun.Order
|
||||
OrderBy string
|
||||
}
|
||||
|
||||
type OrderOpts struct {
|
||||
Order bun.Order
|
||||
OrderBy string
|
||||
Label string
|
||||
}
|
||||
|
||||
// TotalPages calculates the total number of pages
|
||||
func (p *PageOpts) TotalPages(total int) int {
|
||||
if p.PerPage == 0 {
|
||||
return 0
|
||||
}
|
||||
pages := total / p.PerPage
|
||||
if total%p.PerPage > 0 {
|
||||
pages++
|
||||
}
|
||||
return pages
|
||||
}
|
||||
|
||||
// HasPrevPage checks if there is a previous page
|
||||
func (p *PageOpts) HasPrevPage() bool {
|
||||
return p.Page > 1
|
||||
}
|
||||
|
||||
// HasNextPage checks if there is a next page
|
||||
func (p *PageOpts) HasNextPage(total int) bool {
|
||||
return p.Page < p.TotalPages(total)
|
||||
}
|
||||
|
||||
// GetPageRange returns an array of page numbers to display
|
||||
// maxButtons controls how many page buttons to show
|
||||
func (p *PageOpts) GetPageRange(total int, maxButtons int) []int {
|
||||
totalPages := p.TotalPages(total)
|
||||
if totalPages == 0 {
|
||||
return []int{}
|
||||
}
|
||||
|
||||
// If total pages is less than max buttons, show all pages
|
||||
if totalPages <= maxButtons {
|
||||
pages := make([]int, totalPages)
|
||||
for i := 0; i < totalPages; i++ {
|
||||
pages[i] = i + 1
|
||||
}
|
||||
return pages
|
||||
}
|
||||
|
||||
// Calculate range around current page
|
||||
halfButtons := maxButtons / 2
|
||||
start := p.Page - halfButtons
|
||||
end := p.Page + halfButtons
|
||||
|
||||
// Adjust if at beginning
|
||||
if start < 1 {
|
||||
start = 1
|
||||
end = maxButtons
|
||||
}
|
||||
|
||||
// Adjust if at end
|
||||
if end > totalPages {
|
||||
end = totalPages
|
||||
start = totalPages - maxButtons + 1
|
||||
}
|
||||
|
||||
pages := make([]int, 0, maxButtons)
|
||||
for i := start; i <= end; i++ {
|
||||
pages = append(pages, i)
|
||||
}
|
||||
return pages
|
||||
}
|
||||
|
||||
// StartItem returns the number of the first item on the current page
|
||||
func (p *PageOpts) StartItem() int {
|
||||
if p.Page < 1 {
|
||||
return 0
|
||||
}
|
||||
return (p.Page-1)*p.PerPage + 1
|
||||
}
|
||||
|
||||
// EndItem returns the number of the last item on the current page
|
||||
func (p *PageOpts) EndItem(total int) int {
|
||||
end := p.Page * p.PerPage
|
||||
if end > total {
|
||||
return total
|
||||
}
|
||||
return end
|
||||
}
|
||||
Reference in New Issue
Block a user