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:
Cirno the Strongest 2020-06-21 10:22:06 +02:00 committed by GitHub
parent a07cc0df76
commit 81324cf37c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 51 additions and 14 deletions

View file

@ -6,6 +6,7 @@
package admin package admin
import ( import (
"fmt"
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -101,10 +102,12 @@ func GetAllOrgs(ctx *context.APIContext) {
// "403": // "403":
// "$ref": "#/responses/forbidden" // "$ref": "#/responses/forbidden"
users, _, err := models.SearchUsers(&models.SearchUserOptions{ listOptions := utils.GetListOptions(ctx)
users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{
Type: models.UserTypeOrganization, Type: models.UserTypeOrganization,
OrderBy: models.SearchOrderByAlphabetically, OrderBy: models.SearchOrderByAlphabetically,
ListOptions: utils.GetListOptions(ctx), ListOptions: listOptions,
Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate}, Visible: []api.VisibleType{api.VisibleTypePublic, api.VisibleTypeLimited, api.VisibleTypePrivate},
}) })
if err != nil { if err != nil {
@ -115,5 +118,8 @@ func GetAllOrgs(ctx *context.APIContext) {
for i := range users { for i := range users {
orgs[i] = convert.ToOrganization(users[i]) orgs[i] = convert.ToOrganization(users[i])
} }
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
ctx.JSON(http.StatusOK, &orgs) ctx.JSON(http.StatusOK, &orgs)
} }

View file

@ -351,10 +351,12 @@ func GetAllUsers(ctx *context.APIContext) {
// "403": // "403":
// "$ref": "#/responses/forbidden" // "$ref": "#/responses/forbidden"
users, _, err := models.SearchUsers(&models.SearchUserOptions{ listOptions := utils.GetListOptions(ctx)
users, maxResults, err := models.SearchUsers(&models.SearchUserOptions{
Type: models.UserTypeIndividual, Type: models.UserTypeIndividual,
OrderBy: models.SearchOrderByAlphabetically, OrderBy: models.SearchOrderByAlphabetically,
ListOptions: utils.GetListOptions(ctx), ListOptions: listOptions,
}) })
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetAllUsers", err) ctx.Error(http.StatusInternalServerError, "GetAllUsers", err)
@ -366,5 +368,7 @@ func GetAllUsers(ctx *context.APIContext) {
results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin) results[i] = convert.ToUser(users[i], ctx.IsSigned, ctx.User.IsAdmin)
} }
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
ctx.JSON(http.StatusOK, &results) ctx.JSON(http.StatusOK, &results)
} }

View file

@ -20,6 +20,7 @@ import (
// listMembers list an organization's members // listMembers list an organization's members
func listMembers(ctx *context.APIContext, publicOnly bool) { func listMembers(ctx *context.APIContext, publicOnly bool) {
var members []*models.User var members []*models.User
members, _, err := models.FindOrgMembers(&models.FindOrgMembersOpts{ members, _, err := models.FindOrgMembers(&models.FindOrgMembersOpts{
OrgID: ctx.Org.Organization.ID, OrgID: ctx.Org.Organization.ID,
PublicOnly: publicOnly, PublicOnly: publicOnly,
@ -34,6 +35,7 @@ func listMembers(ctx *context.APIContext, publicOnly bool) {
for i, member := range members { for i, member := range members {
apiMembers[i] = convert.ToUser(member, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin) apiMembers[i] = convert.ToUser(member, ctx.IsSigned, ctx.User != nil && ctx.User.IsAdmin)
} }
ctx.JSON(http.StatusOK, apiMembers) ctx.JSON(http.StatusOK, apiMembers)
} }

View file

@ -6,6 +6,7 @@
package org package org
import ( import (
"fmt"
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -115,8 +116,10 @@ func GetAll(ctx *context.APIContext) {
} }
} }
publicOrgs, _, err := models.SearchUsers(&models.SearchUserOptions{ listOptions := utils.GetListOptions(ctx)
ListOptions: utils.GetListOptions(ctx),
publicOrgs, maxResults, err := models.SearchUsers(&models.SearchUserOptions{
ListOptions: listOptions,
Type: models.UserTypeOrganization, Type: models.UserTypeOrganization,
OrderBy: models.SearchOrderByAlphabetically, OrderBy: models.SearchOrderByAlphabetically,
Visible: vMode, Visible: vMode,
@ -130,6 +133,8 @@ func GetAll(ctx *context.APIContext) {
orgs[i] = convert.ToOrganization(publicOrgs[i]) orgs[i] = convert.ToOrganization(publicOrgs[i])
} }
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
ctx.JSON(http.StatusOK, &orgs) ctx.JSON(http.StatusOK, &orgs)
} }

View file

@ -6,6 +6,7 @@
package org package org
import ( import (
"fmt"
"net/http" "net/http"
"strings" "strings"
@ -650,15 +651,17 @@ func SearchTeam(ctx *context.APIContext) {
// items: // items:
// "$ref": "#/definitions/Team" // "$ref": "#/definitions/Team"
listOptions := utils.GetListOptions(ctx)
opts := &models.SearchTeamOptions{ opts := &models.SearchTeamOptions{
UserID: ctx.User.ID, UserID: ctx.User.ID,
Keyword: strings.TrimSpace(ctx.Query("q")), Keyword: strings.TrimSpace(ctx.Query("q")),
OrgID: ctx.Org.Organization.ID, OrgID: ctx.Org.Organization.ID,
IncludeDesc: (ctx.Query("include_desc") == "" || ctx.QueryBool("include_desc")), IncludeDesc: (ctx.Query("include_desc") == "" || ctx.QueryBool("include_desc")),
ListOptions: utils.GetListOptions(ctx), ListOptions: listOptions,
} }
teams, _, err := models.SearchTeam(opts) teams, maxResults, err := models.SearchTeam(opts)
if err != nil { if err != nil {
log.Error("SearchTeam failed: %v", err) log.Error("SearchTeam failed: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
@ -681,6 +684,8 @@ func SearchTeam(ctx *context.APIContext) {
apiTeams[i] = convert.ToTeam(teams[i]) apiTeams[i] = convert.ToTeam(teams[i])
} }
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
ctx.JSON(http.StatusOK, map[string]interface{}{ ctx.JSON(http.StatusOK, map[string]interface{}{
"ok": true, "ok": true,
"data": apiTeams, "data": apiTeams,

View file

@ -202,14 +202,16 @@ func GetAllCommits(ctx *context.APIContext) {
i++ i++
} }
ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize) // kept for backwards compatibility
ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page)) ctx.Header().Set("X-Page", strconv.Itoa(listOptions.Page))
ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) ctx.Header().Set("X-PerPage", strconv.Itoa(listOptions.PageSize))
ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10)) ctx.Header().Set("X-Total", strconv.FormatInt(commitsCountTotal, 10))
ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount)) ctx.Header().Set("X-PageCount", strconv.Itoa(pageCount))
ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount)) ctx.Header().Set("X-HasMore", strconv.FormatBool(listOptions.Page < pageCount))
ctx.SetLinkHeader(int(commitsCountTotal), listOptions.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", commitsCountTotal))
ctx.JSON(http.StatusOK, &apiCommits) ctx.JSON(http.StatusOK, &apiCommits)
} }

View file

@ -322,6 +322,8 @@ func ListIssues(ctx *context.APIContext) {
} }
ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, listOptions.PageSize) ctx.SetLinkHeader(ctx.Repo.Repository.NumIssues, listOptions.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", ctx.Repo.Repository.NumIssues))
ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues)) ctx.JSON(http.StatusOK, convert.ToAPIIssueList(issues))
} }

View file

@ -208,8 +208,10 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
} }
repo := ctx.Repo.Repository repo := ctx.Repo.Repository
statuses, _, err := models.GetCommitStatuses(repo, sha, &models.CommitStatusOptions{ listOptions := utils.GetListOptions(ctx)
ListOptions: utils.GetListOptions(ctx),
statuses, maxResults, err := models.GetCommitStatuses(repo, sha, &models.CommitStatusOptions{
ListOptions: listOptions,
SortType: ctx.QueryTrim("sort"), SortType: ctx.QueryTrim("sort"),
State: ctx.QueryTrim("state"), State: ctx.QueryTrim("state"),
}) })
@ -223,6 +225,9 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
apiStatuses = append(apiStatuses, status.APIFormat()) apiStatuses = append(apiStatuses, status.APIFormat())
} }
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
ctx.JSON(http.StatusOK, apiStatuses) ctx.JSON(http.StatusOK, apiStatuses)
} }

View file

@ -6,6 +6,7 @@
package user package user
import ( import (
"fmt"
"net/http" "net/http"
"strings" "strings"
@ -56,14 +57,16 @@ func Search(ctx *context.APIContext) {
// items: // items:
// "$ref": "#/definitions/User" // "$ref": "#/definitions/User"
listOptions := utils.GetListOptions(ctx)
opts := &models.SearchUserOptions{ opts := &models.SearchUserOptions{
Keyword: strings.Trim(ctx.Query("q"), " "), Keyword: strings.Trim(ctx.Query("q"), " "),
UID: com.StrTo(ctx.Query("uid")).MustInt64(), UID: com.StrTo(ctx.Query("uid")).MustInt64(),
Type: models.UserTypeIndividual, Type: models.UserTypeIndividual,
ListOptions: utils.GetListOptions(ctx), ListOptions: listOptions,
} }
users, _, err := models.SearchUsers(opts) users, maxResults, err := models.SearchUsers(opts)
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"ok": false, "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) 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{}{ ctx.JSON(http.StatusOK, map[string]interface{}{
"ok": true, "ok": true,
"data": results, "data": results,