diff --git a/Readme.md b/Readme.md index 4497a4b..d82ad9e 100644 --- a/Readme.md +++ b/Readme.md @@ -147,7 +147,7 @@ handler.SetLoggingProvider(&log.Log) The `ReadAll`-method has a number of parameters: ```go -ReadAll(auth Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfPages int, err error) +ReadAll(auth Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfItems int64, err error) ``` The third parameter contains the requested page, the fourth parameter contains the number of items per page. @@ -161,7 +161,7 @@ You need to return a number of things: * The result itself, usually a slice * The number of items you return in `result`. Most of the time, this is just `len(result)`. You need to return this value to make the clients aware if they requested a number of items > max items per page. -* The total number of pages available. This value can then be used by the clients to build client-side pagination or similar. +* The total number of items available. We use the total number of items here and not the number pages so the implementations don't have to deal with calculating the number of pages from that. The total number of clients is then calculated and returned to the client, ite can then be used by the clients to build client-side pagination or similar. * An error. The number of items and the total number of pages available will be returned in the `x-pagination-total-pages` and `x-pagination-result-count` response headers. diff --git a/handler/read_all.go b/handler/read_all.go index aebc35a..36c4483 100644 --- a/handler/read_all.go +++ b/handler/read_all.go @@ -71,12 +71,23 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { // Search search := ctx.QueryParam("s") - result, resultCount, numberOfPages, err := currentStruct.ReadAll(currentAuth, search, pageNumber, perPageNumber) + result, resultCount, numberOfItems, err := currentStruct.ReadAll(currentAuth, search, pageNumber, perPageNumber) if err != nil { return HandleHTTPError(err, ctx) } - ctx.Response().Header().Set("x-pagination-total-pages", strconv.FormatInt(int64(numberOfPages), 10)) + // Calculate the number of pages from the number of items + var numberOfPages = numberOfItems / int64(perPageNumber) + // If we return all results, we only have one page + if pageNumber < 0 { + numberOfPages = 1 + } + // If we don't have results, we don't have a page + if resultCount == 0 { + numberOfPages = 0 + } + + ctx.Response().Header().Set("x-pagination-total-pages", strconv.FormatInt(numberOfPages, 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") diff --git a/web.go b/web.go index bf80bc0..7e57af8 100644 --- a/web.go +++ b/web.go @@ -31,7 +31,7 @@ type Rights interface { type CRUDable interface { Create(Auth) error ReadOne() error - ReadAll(auth Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfPages int, err error) + ReadAll(auth Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfItems int64, err error) Update() error Delete() error }