38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package requests
|
|
|
|
import "github.com/samber/lo"
|
|
|
|
// Pager is a common paginated API response envelope.
|
|
type Pager struct {
|
|
// Pagination contains paging inputs (page/limit) echoed back in the response.
|
|
Pagination ` json:",inline"`
|
|
// Total is the total number of items matching current filter (before paging).
|
|
Total int64 `json:"total"`
|
|
// Items is the paged result list; concrete type depends on endpoint.
|
|
Items any `json:"items"`
|
|
}
|
|
|
|
// Pagination defines common paging query parameters used by list endpoints.
|
|
type Pagination struct {
|
|
// Page is 1-based page index; values <= 0 are normalized to 1.
|
|
Page int64 `json:"page" form:"page" query:"page"`
|
|
// Limit is page size; only values in {10,20,50,100} are accepted (otherwise defaults to 10).
|
|
Limit int64 `json:"limit" form:"limit" query:"limit"`
|
|
}
|
|
|
|
func (filter *Pagination) Offset() int64 {
|
|
return (filter.Page - 1) * filter.Limit
|
|
}
|
|
|
|
func (filter *Pagination) Format() *Pagination {
|
|
if filter.Page <= 0 {
|
|
filter.Page = 1
|
|
}
|
|
|
|
if !lo.Contains([]int64{10, 20, 50, 100}, filter.Limit) {
|
|
filter.Limit = 10
|
|
}
|
|
|
|
return filter
|
|
}
|