mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-19 00:10:15 +00:00
Add pagination headers on endpoints that support total count from database (#11145)
* begin work * import fmt * more work * empty commit Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
a07cc0df76
commit
81324cf37c
9 changed files with 51 additions and 14 deletions
|
@ -6,6 +6,7 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
|
@ -56,14 +57,16 @@ func Search(ctx *context.APIContext) {
|
|||
// items:
|
||||
// "$ref": "#/definitions/User"
|
||||
|
||||
listOptions := utils.GetListOptions(ctx)
|
||||
|
||||
opts := &models.SearchUserOptions{
|
||||
Keyword: strings.Trim(ctx.Query("q"), " "),
|
||||
UID: com.StrTo(ctx.Query("uid")).MustInt64(),
|
||||
Type: models.UserTypeIndividual,
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
ListOptions: listOptions,
|
||||
}
|
||||
|
||||
users, _, err := models.SearchUsers(opts)
|
||||
users, maxResults, err := models.SearchUsers(opts)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
"ok": false,
|
||||
|
@ -77,6 +80,9 @@ func Search(ctx *context.APIContext) {
|
|||
results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
|
||||
}
|
||||
|
||||
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
|
||||
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
|
||||
|
||||
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||||
"ok": true,
|
||||
"data": results,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue