2018-07-13 16:19:51 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
Paginate api routes responses
|
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alice-lg/alice-lg/backend/api"
|
|
|
|
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
|
|
|
func apiPaginateRoutes(
|
|
|
|
routes api.Routes, page, pageSize int,
|
|
|
|
) (api.Routes, api.Pagination) {
|
|
|
|
totalResults := len(routes)
|
2018-07-23 12:27:16 +02:00
|
|
|
|
|
|
|
// In case pageSize is 0, we assume pagination
|
|
|
|
// is disabled.
|
|
|
|
if pageSize == 0 {
|
|
|
|
pagination := api.Pagination{
|
|
|
|
Page: page,
|
|
|
|
PageSize: pageSize,
|
|
|
|
TotalPages: 0,
|
|
|
|
TotalResults: totalResults,
|
|
|
|
}
|
|
|
|
return routes, pagination
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the number of pages we get
|
2018-07-13 16:19:51 +02:00
|
|
|
totalPages := int(math.Ceil(float64(totalResults) / float64(pageSize)))
|
|
|
|
|
|
|
|
offset := page * pageSize
|
|
|
|
rindex := offset + pageSize
|
|
|
|
|
|
|
|
// Don't access out of bounds
|
|
|
|
if rindex > totalResults {
|
|
|
|
rindex = totalResults
|
|
|
|
}
|
2018-07-13 16:43:12 +02:00
|
|
|
if offset < 0 {
|
|
|
|
offset = 0
|
|
|
|
}
|
2018-07-13 16:19:51 +02:00
|
|
|
|
|
|
|
pagination := api.Pagination{
|
|
|
|
Page: page,
|
2018-07-18 11:45:14 +02:00
|
|
|
PageSize: pageSize,
|
2018-07-13 16:19:51 +02:00
|
|
|
TotalPages: totalPages,
|
|
|
|
TotalResults: totalResults,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safeguards
|
|
|
|
if offset >= totalResults {
|
|
|
|
return api.Routes{}, pagination
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes[offset:rindex], pagination
|
|
|
|
}
|
2018-09-24 22:29:13 +02:00
|
|
|
|
|
|
|
func apiPaginateLookupRoutes(
|
|
|
|
routes api.LookupRoutes,
|
|
|
|
page, pageSize int,
|
|
|
|
) (api.LookupRoutes, api.Pagination) {
|
|
|
|
totalResults := len(routes)
|
|
|
|
|
|
|
|
// In case pageSize is 0, we assume pagination
|
|
|
|
// is disabled.
|
|
|
|
if pageSize == 0 {
|
|
|
|
pagination := api.Pagination{
|
|
|
|
Page: page,
|
|
|
|
PageSize: pageSize,
|
|
|
|
TotalPages: 0,
|
|
|
|
TotalResults: totalResults,
|
|
|
|
}
|
|
|
|
return routes, pagination
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the number of pages we get
|
|
|
|
totalPages := int(math.Ceil(float64(totalResults) / float64(pageSize)))
|
|
|
|
|
|
|
|
offset := page * pageSize
|
|
|
|
rindex := offset + pageSize
|
|
|
|
|
|
|
|
// Don't access out of bounds
|
|
|
|
if rindex > totalResults {
|
|
|
|
rindex = totalResults
|
|
|
|
}
|
|
|
|
if offset < 0 {
|
|
|
|
offset = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pagination := api.Pagination{
|
|
|
|
Page: page,
|
|
|
|
PageSize: pageSize,
|
|
|
|
TotalPages: totalPages,
|
|
|
|
TotalResults: totalResults,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safeguards
|
|
|
|
if offset >= totalResults {
|
|
|
|
return api.LookupRoutes{}, pagination
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes[offset:rindex], pagination
|
|
|
|
}
|