Added calculation of the total number of pages to the web handler

This commit is contained in:
kolaente 2019-10-23 16:44:16 +02:00
parent 8edfc5d33c
commit 3ee093147b
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 16 additions and 5 deletions

View File

@ -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.

View File

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

2
web.go
View File

@ -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
}