diff options
Diffstat (limited to 'routers/api/v1/repo/topic.go')
-rw-r--r-- | routers/api/v1/repo/topic.go | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/routers/api/v1/repo/topic.go b/routers/api/v1/repo/topic.go index a7c52e0bcc..fc277cb3fe 100644 --- a/routers/api/v1/repo/topic.go +++ b/routers/api/v1/repo/topic.go @@ -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, }) |