Skip to content

Commit f0f49bd

Browse files
Can't use FindTopics when looking for a single repo topic, as it doesnt use exact match
Signed-off-by: David Svantesson <[email protected]>
1 parent 7c721f9 commit f0f49bd

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

models/topic.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -149,37 +149,41 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
149149
return topics, sess.Desc("topic.repo_count").Find(&topics)
150150
}
151151

152+
// GetRepoTopicByName retrives topic from name for a repo if it exist
153+
func GetRepoTopicByName(repoID int64, topicName string) (topic *Topic, err error) {
154+
sess := x.Select("topic.*").Where("repo_topic.repo_id = ?", repoID).And("topic.name = ?", topicName)
155+
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
156+
has, err := sess.Get(&topic)
157+
if has {
158+
return topic, err
159+
}
160+
return nil, err
161+
}
162+
152163
// AddTopic adds a topic name to a repository (if it does not already have it)
153164
func AddTopic(repoID int64, topicName string) (*Topic, error) {
154-
topics, err := FindTopics(&FindTopicOptions{
155-
RepoID: repoID,
156-
Keyword: topicName,
157-
})
165+
topic, err := GetRepoTopicByName(repoID, topicName)
158166
if err != nil {
159167
return nil, err
160168
}
161-
if len(topics) != 0 {
169+
if topic != nil {
162170
// Repo already have topic
163-
return topics[0], nil
171+
return topic, nil
164172
}
165173

166174
return addTopicByNameToRepo(repoID, topicName, x)
167175
}
168176

169177
// DeleteTopic removes a topic name from a repository (if it has it)
170178
func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
171-
topics, err := FindTopics(&FindTopicOptions{
172-
RepoID: repoID,
173-
Keyword: topicName,
174-
})
179+
topic, err := GetRepoTopicByName(repoID, topicName)
175180
if err != nil {
176181
return nil, err
177182
}
178-
if len(topics) == 0 {
183+
if topic == nil {
179184
// Repo doesn't have topic, can't be removed
180185
return nil, nil
181186
}
182-
topic := topics[0]
183187

184188
err = removeTopicFromRepo(repoID, topic, x)
185189

routers/api/v1/repo/topic.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ func HasTopic(ctx *context.APIContext) {
8787
// "$ref": "#/responses/empty"
8888
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
8989

90-
topics, err := models.FindTopics(&models.FindTopicOptions{
91-
RepoID: ctx.Repo.Repository.ID,
92-
Keyword: topicName,
93-
})
90+
topic, err := models.GetRepoTopicByName(ctx.Repo.Repository.ID, topicName)
9491
if err != nil {
9592
log.Error("HasTopic failed: %v", err)
9693
ctx.JSON(500, map[string]interface{}{
@@ -99,12 +96,12 @@ func HasTopic(ctx *context.APIContext) {
9996
return
10097
}
10198

102-
if len(topics) == 0 {
99+
if topic == nil {
103100
ctx.NotFound()
104101
}
105102

106103
ctx.JSON(200, map[string]interface{}{
107-
"topic": convert.ToTopicResponse(topics[0]),
104+
"topic": convert.ToTopicResponse(topic),
108105
})
109106
}
110107

0 commit comments

Comments
 (0)