Returning pagination metadata.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 2m48s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 2m48s
This commit is contained in:
@@ -50,3 +50,27 @@ func (f Filters) sortDirection() string {
|
||||
}
|
||||
return "ASC"
|
||||
}
|
||||
|
||||
// Metadata struct for holding the pagination metadata
|
||||
type Metadata struct {
|
||||
CurrentPage int `json:"current_page,omitempty"`
|
||||
PageSize int `json:"page_size,omitempty"`
|
||||
FirstPage int `json:"first_page,omitempty"`
|
||||
LastPage int `json:"last_page,omitempty"`
|
||||
TotalRecords int `json:"total_records,omitempty"`
|
||||
}
|
||||
|
||||
// calculateMetadata : calculates the appropriate pagination metadata values given the total number of records, current page, and page size values. Note that when the last page value is calculated, we are dividing two int values, and when dividing integer types in Go the result will also be an integer type, with the modulus (or reminder) dropped. So, for instance, if there were 12 records in total and a page of 5, the last page value would be (12+5-1)/5 = 3.2, which is then truncated to 3 by Go.
|
||||
func calculateMetadata(totalRecords, page, pageSize int) Metadata {
|
||||
if totalRecords == 0 {
|
||||
return Metadata{}
|
||||
}
|
||||
|
||||
return Metadata{
|
||||
CurrentPage: page,
|
||||
PageSize: pageSize,
|
||||
FirstPage: 1,
|
||||
LastPage: (totalRecords + pageSize - 1) / pageSize,
|
||||
TotalRecords: totalRecords,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user