1
0
Fork 0
forked from forgejo/forgejo

[API] generalize list header (#16551)

* Add info about list endpoints to CONTRIBUTING.md

* Let all list endpoints return X-Total-Count header 

* Add TODOs for GetCombinedCommitStatusByRef

* Fix models/issue_stopwatch.go

* Rrefactor models.ListDeployKeys

* Introduce helper func and use them for SetLinkHeader related func
This commit is contained in:
6543 2021-08-12 14:43:08 +02:00 committed by GitHub
parent ca13e1d56c
commit 2289580bb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
88 changed files with 637 additions and 329 deletions

View file

@ -14,23 +14,22 @@ import (
"code.gitea.io/gitea/routers/api/v1/utils"
)
// getWatchedRepos returns the repos that the user with the specified userID is
// watching
func getWatchedRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, error) {
watchedRepos, err := models.GetWatchedRepos(user.ID, private, listOptions)
// getWatchedRepos returns the repos that the user with the specified userID is watching
func getWatchedRepos(user *models.User, private bool, listOptions models.ListOptions) ([]*api.Repository, int64, error) {
watchedRepos, total, err := models.GetWatchedRepos(user.ID, private, listOptions)
if err != nil {
return nil, err
return nil, 0, err
}
repos := make([]*api.Repository, len(watchedRepos))
for i, watched := range watchedRepos {
access, err := models.AccessLevel(user, watched)
if err != nil {
return nil, err
return nil, 0, err
}
repos[i] = convert.ToRepo(watched, access)
}
return repos, nil
return repos, total, nil
}
// GetWatchedRepos returns the repos that the user specified in ctx is watching
@ -60,10 +59,12 @@ func GetWatchedRepos(ctx *context.APIContext) {
user := GetUserByParams(ctx)
private := user.ID == ctx.User.ID
repos, err := getWatchedRepos(user, private, utils.GetListOptions(ctx))
repos, total, err := getWatchedRepos(user, private, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
}
ctx.SetTotalCountHeader(total)
ctx.JSON(http.StatusOK, &repos)
}
@ -87,10 +88,12 @@ func GetMyWatchedRepos(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/RepositoryList"
repos, err := getWatchedRepos(ctx.User, true, utils.GetListOptions(ctx))
repos, total, err := getWatchedRepos(ctx.User, true, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "getWatchedRepos", err)
}
ctx.SetTotalCountHeader(total)
ctx.JSON(http.StatusOK, &repos)
}