@@ -149,37 +149,41 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
149
149
return topics , sess .Desc ("topic.repo_count" ).Find (& topics )
150
150
}
151
151
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
+
152
163
// AddTopic adds a topic name to a repository (if it does not already have it)
153
164
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 )
158
166
if err != nil {
159
167
return nil , err
160
168
}
161
- if len ( topics ) != 0 {
169
+ if topic != nil {
162
170
// Repo already have topic
163
- return topics [ 0 ] , nil
171
+ return topic , nil
164
172
}
165
173
166
174
return addTopicByNameToRepo (repoID , topicName , x )
167
175
}
168
176
169
177
// DeleteTopic removes a topic name from a repository (if it has it)
170
178
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 )
175
180
if err != nil {
176
181
return nil , err
177
182
}
178
- if len ( topics ) == 0 {
183
+ if topic == nil {
179
184
// Repo doesn't have topic, can't be removed
180
185
return nil , nil
181
186
}
182
- topic := topics [0 ]
183
187
184
188
err = removeTopicFromRepo (repoID , topic , x )
185
189
0 commit comments