mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 10:18:38 +00:00
[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:
@@ -47,12 +47,13 @@ func ListTopics(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/TopicNames"
|
||||
|
||||
topics, err := models.FindTopics(&models.FindTopicOptions{
|
||||
opts := &models.FindTopicOptions{
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
RepoID: ctx.Repo.Repository.ID,
|
||||
})
|
||||
}
|
||||
|
||||
topics, total, err := models.FindTopics(opts)
|
||||
if err != nil {
|
||||
log.Error("ListTopics failed: %v", err)
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
@@ -61,6 +62,8 @@ func ListTopics(ctx *context.APIContext) {
|
||||
for i, topic := range topics {
|
||||
topicNames[i] = topic.Name
|
||||
}
|
||||
|
||||
ctx.SetTotalCountHeader(total)
|
||||
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||||
"topics": topicNames,
|
||||
})
|
||||
@@ -164,15 +167,15 @@ func AddTopic(ctx *context.APIContext) {
|
||||
}
|
||||
|
||||
// Prevent adding more topics than allowed to repo
|
||||
topics, err := models.FindTopics(&models.FindTopicOptions{
|
||||
count, err := models.CountTopics(&models.FindTopicOptions{
|
||||
RepoID: ctx.Repo.Repository.ID,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("AddTopic failed: %v", err)
|
||||
log.Error("CountTopics failed: %v", err)
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
if len(topics) >= 25 {
|
||||
if count >= 25 {
|
||||
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
||||
"message": "Exceeding maximum allowed topics per repo.",
|
||||
})
|
||||
@@ -269,21 +272,13 @@ func TopicSearch(ctx *context.APIContext) {
|
||||
// "403":
|
||||
// "$ref": "#/responses/forbidden"
|
||||
|
||||
if ctx.User == nil {
|
||||
ctx.Error(http.StatusForbidden, "UserIsNil", "Only owners could change the topics.")
|
||||
return
|
||||
opts := &models.FindTopicOptions{
|
||||
Keyword: ctx.FormString("q"),
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
}
|
||||
|
||||
kw := ctx.FormString("q")
|
||||
|
||||
listOptions := utils.GetListOptions(ctx)
|
||||
|
||||
topics, err := models.FindTopics(&models.FindTopicOptions{
|
||||
Keyword: kw,
|
||||
ListOptions: listOptions,
|
||||
})
|
||||
topics, total, err := models.FindTopics(opts)
|
||||
if err != nil {
|
||||
log.Error("SearchTopics failed: %v", err)
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
@@ -292,6 +287,8 @@ func TopicSearch(ctx *context.APIContext) {
|
||||
for i, topic := range topics {
|
||||
topicResponses[i] = convert.ToTopicResponse(topic)
|
||||
}
|
||||
|
||||
ctx.SetTotalCountHeader(total)
|
||||
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||||
"topics": topicResponses,
|
||||
})
|
||||
|
Reference in New Issue
Block a user