1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
alice-lg-alice-lg/backend/api_pagination.go

41 lines
702 B
Go
Raw Normal View History

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)
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
}
pagination := api.Pagination{
Page: page,
TotalPages: totalPages,
TotalResults: totalResults,
}
// Safeguards
if offset >= totalResults {
return api.Routes{}, pagination
}
return routes[offset:rindex], pagination
}