mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-10-12 09:00:05 +08:00
update: database records for all resources.
This commit is contained in:
46
pkg/util/pagination/pagination.go
Normal file
46
pkg/util/pagination/pagination.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package pagination
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/squirrel"
|
||||
"math"
|
||||
)
|
||||
|
||||
type PaginationRequest struct {
|
||||
Page uint64
|
||||
PageSize uint64
|
||||
}
|
||||
|
||||
func NewRequest(page, pageSize int32) PaginationRequest {
|
||||
if page == 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
if pageSize == 0 {
|
||||
pageSize = 15
|
||||
}
|
||||
|
||||
return PaginationRequest{
|
||||
Page: uint64(page),
|
||||
PageSize: uint64(pageSize),
|
||||
}
|
||||
}
|
||||
|
||||
func (pr *PaginationRequest) Offset() uint64 {
|
||||
// start at page 1.
|
||||
return (pr.Page - 1) * pr.PageSize
|
||||
}
|
||||
|
||||
func (pr *PaginationRequest) CalculatePages(count int) int32 {
|
||||
return int32(math.Ceil(float64(count) / float64(pr.PageSize)))
|
||||
}
|
||||
|
||||
func (pr *PaginationRequest) ApplyToSelect(sb *squirrel.SelectBuilder) *squirrel.SelectBuilder {
|
||||
if pr == nil {
|
||||
return sb
|
||||
}
|
||||
|
||||
result := sb.Limit(pr.PageSize).
|
||||
Offset(pr.Offset())
|
||||
|
||||
return &result
|
||||
}
|
Reference in New Issue
Block a user