diff --git a/models/topic.go b/models/topic.go index b62b809530d2b..6784853a72f84 100644 --- a/models/topic.go +++ b/models/topic.go @@ -149,18 +149,26 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) { return topics, sess.Desc("topic.repo_count").Find(&topics) } +// GetRepoTopicByName retrives topic from name for a repo if it exist +func GetRepoTopicByName(repoID int64, topicName string) (topic *Topic, err error) { + sess := x.Select("topic.*").Where("repo_topic.repo_id = ?", repoID).And("topic.name = ?", topicName) + sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id") + has, err := sess.Get(&topic) + if has { + return topic, err + } + return nil, err +} + // AddTopic adds a topic name to a repository (if it does not already have it) func AddTopic(repoID int64, topicName string) (*Topic, error) { - topics, err := FindTopics(&FindTopicOptions{ - RepoID: repoID, - Keyword: topicName, - }) + topic, err := GetRepoTopicByName(repoID, topicName) if err != nil { return nil, err } - if len(topics) != 0 { + if topic != nil { // Repo already have topic - return topics[0], nil + return topic, nil } return addTopicByNameToRepo(repoID, topicName, x) @@ -168,18 +176,14 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) { // DeleteTopic removes a topic name from a repository (if it has it) func DeleteTopic(repoID int64, topicName string) (*Topic, error) { - topics, err := FindTopics(&FindTopicOptions{ - RepoID: repoID, - Keyword: topicName, - }) + topic, err := GetRepoTopicByName(repoID, topicName) if err != nil { return nil, err } - if len(topics) == 0 { + if topic == nil { // Repo doesn't have topic, can't be removed return nil, nil } - topic := topics[0] err = removeTopicFromRepo(repoID, topic, x) diff --git a/routers/api/v1/repo/topic.go b/routers/api/v1/repo/topic.go index 8aebcc95b4f4f..eca2b981608bc 100644 --- a/routers/api/v1/repo/topic.go +++ b/routers/api/v1/repo/topic.go @@ -87,10 +87,7 @@ func HasTopic(ctx *context.APIContext) { // "$ref": "#/responses/empty" topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic"))) - topics, err := models.FindTopics(&models.FindTopicOptions{ - RepoID: ctx.Repo.Repository.ID, - Keyword: topicName, - }) + topic, err := models.GetRepoTopicByName(ctx.Repo.Repository.ID, topicName) if err != nil { log.Error("HasTopic failed: %v", err) ctx.JSON(500, map[string]interface{}{ @@ -99,12 +96,12 @@ func HasTopic(ctx *context.APIContext) { return } - if len(topics) == 0 { + if topic == nil { ctx.NotFound() } ctx.JSON(200, map[string]interface{}{ - "topic": convert.ToTopicResponse(topics[0]), + "topic": convert.ToTopicResponse(topic), }) }