2024-04-28 11:27:23 +00:00
|
|
|
package param
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPageNumber = 1
|
2024-06-12 08:15:28 +00:00
|
|
|
defaultPageSize = 10
|
2024-04-28 11:27:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PaginationRequest struct {
|
2024-06-12 08:15:28 +00:00
|
|
|
PageSize uint `query:"page_size" example:"10"`
|
|
|
|
PageNumber uint `query:"page_number" example:"1"`
|
2024-04-28 11:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PaginationResponse struct {
|
2024-05-21 22:10:16 +00:00
|
|
|
PageSize uint `json:"page_size" example:"10"`
|
|
|
|
PageNumber uint `json:"page_number" example:"1"`
|
|
|
|
Total uint `json:"total" example:"100"`
|
2024-04-28 11:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PaginationRequest) GetPageNumber() uint {
|
|
|
|
if p.PageNumber <= 0 {
|
|
|
|
p.PageNumber = defaultPageNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.PageNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PaginationRequest) GetOffset() uint {
|
|
|
|
return (p.GetPageNumber() - 1) * p.GetPageSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PaginationRequest) GetPageSize() uint {
|
|
|
|
validPageSizes := []uint{10, 25, 50, 100}
|
|
|
|
for _, size := range validPageSizes {
|
|
|
|
if p.PageSize == size {
|
2024-06-12 08:15:28 +00:00
|
|
|
return p.PageSize
|
2024-04-28 11:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-12 08:15:28 +00:00
|
|
|
p.PageSize = defaultPageSize
|
2024-04-28 11:27:23 +00:00
|
|
|
|
2024-06-12 08:15:28 +00:00
|
|
|
return p.PageSize
|
2024-04-28 11:27:23 +00:00
|
|
|
}
|