Added rounding of pages
This commit is contained in:
parent
502bbbbd9d
commit
f337750c35
|
@ -17,6 +17,7 @@ package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
@ -82,7 +83,9 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the number of pages from the number of items
|
// Calculate the number of pages from the number of items
|
||||||
var numberOfPages = numberOfItems / int64(perPageNumber)
|
// We always round up, because if we don't have a number of items which is exactly dividable by the number of items per page,
|
||||||
|
// we would get a result that is one page off.
|
||||||
|
var numberOfPages = math.Ceil(float64(numberOfItems) / float64(perPageNumber))
|
||||||
// If we return all results, we only have one page
|
// If we return all results, we only have one page
|
||||||
if pageNumber < 0 {
|
if pageNumber < 0 {
|
||||||
numberOfPages = 1
|
numberOfPages = 1
|
||||||
|
@ -92,7 +95,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
|
||||||
numberOfPages = 0
|
numberOfPages = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Response().Header().Set("x-pagination-total-pages", strconv.FormatInt(numberOfPages, 10))
|
ctx.Response().Header().Set("x-pagination-total-pages", strconv.FormatFloat(numberOfPages, 'f', 0, 64))
|
||||||
ctx.Response().Header().Set("x-pagination-result-count", strconv.FormatInt(int64(resultCount), 10))
|
ctx.Response().Header().Set("x-pagination-result-count", strconv.FormatInt(int64(resultCount), 10))
|
||||||
ctx.Response().Header().Set("Access-Control-Expose-Headers", "x-pagination-total-pages, x-pagination-result-count")
|
ctx.Response().Header().Set("Access-Control-Expose-Headers", "x-pagination-total-pages, x-pagination-result-count")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue