woodpecker/server/model/pagination.go

21 lines
371 B
Go
Raw Normal View History

package model
type ListOptions struct {
All bool
Page int
PerPage int
}
func ApplyPagination[T any](d *ListOptions, slice []T) []T {
if d.All {
return slice
}
if d.PerPage*(d.Page-1) > len(slice) {
return []T{}
}
if d.PerPage*(d.Page) > len(slice) {
return slice[d.PerPage*(d.Page-1):]
}
return slice[d.PerPage*(d.Page-1) : d.PerPage*(d.Page)]
}