Added rounding of pages

This commit is contained in:
kolaente 2019-10-23 22:25:26 +02:00
parent 502bbbbd9d
commit f337750c35
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 5 additions and 2 deletions

View File

@ -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")