Skip to content

Commit

Permalink
Can't use FindTopics when looking for a single repo topic, as it does…
Browse files Browse the repository at this point in the history
…nt use exact match

Signed-off-by: David Svantesson <davidsvantesson@gmail.com>
  • Loading branch information
davidsvantesson committed Aug 24, 2019
1 parent 7c721f9 commit f0f49bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
28 changes: 16 additions & 12 deletions models/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,37 +149,41 @@ 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)
}

// 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)

Expand Down
9 changes: 3 additions & 6 deletions routers/api/v1/repo/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand All @@ -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),
})
}

Expand Down

0 comments on commit f0f49bd

Please sign in to comment.