@@ -197,10 +197,13 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
197
197
198
198
// GetRepoTopicByName retrives topic from name for a repo if it exist
199
199
func GetRepoTopicByName (repoID int64 , topicName string ) (* Topic , error ) {
200
+ return getRepoTopicByName (x , repoID , topicName )
201
+ }
202
+ func getRepoTopicByName (e Engine , repoID int64 , topicName string ) (* Topic , error ) {
200
203
var cond = builder .NewCond ()
201
204
var topic Topic
202
205
cond = cond .And (builder.Eq {"repo_topic.repo_id" : repoID }).And (builder.Eq {"topic.name" : topicName })
203
- sess := x .Table ("topic" ).Where (cond )
206
+ sess := e .Table ("topic" ).Where (cond )
204
207
sess .Join ("INNER" , "repo_topic" , "repo_topic.topic_id = topic.id" )
205
208
has , err := sess .Get (& topic )
206
209
if has {
@@ -211,7 +214,13 @@ func GetRepoTopicByName(repoID int64, topicName string) (*Topic, error) {
211
214
212
215
// AddTopic adds a topic name to a repository (if it does not already have it)
213
216
func AddTopic (repoID int64 , topicName string ) (* Topic , error ) {
214
- topic , err := GetRepoTopicByName (repoID , topicName )
217
+ sess := x .NewSession ()
218
+ defer sess .Close ()
219
+ if err := sess .Begin (); err != nil {
220
+ return nil , err
221
+ }
222
+
223
+ topic , err := getRepoTopicByName (sess , repoID , topicName )
215
224
if err != nil {
216
225
return nil , err
217
226
}
@@ -220,7 +229,25 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) {
220
229
return topic , nil
221
230
}
222
231
223
- return addTopicByNameToRepo (x , repoID , topicName )
232
+ topic , err = addTopicByNameToRepo (sess , repoID , topicName )
233
+ if err != nil {
234
+ return nil , err
235
+ }
236
+
237
+ topicNames := make ([]string , 0 , 25 )
238
+ if err := sess .Select ("name" ).Table ("topic" ).
239
+ Join ("INNER" , "repo_topic" , "repo_topic.topic_id = topic.id" ).
240
+ Where ("repo_topic.repo_id = ?" , repoID ).Desc ("topic.repo_count" ).Find (& topicNames ); err != nil {
241
+ return nil , err
242
+ }
243
+
244
+ if _ , err := sess .ID (repoID ).Cols ("topics" ).Update (& Repository {
245
+ Topics : topicNames ,
246
+ }); err != nil {
247
+ return nil , err
248
+ }
249
+
250
+ return topic , sess .Commit ()
224
251
}
225
252
226
253
// DeleteTopic removes a topic name from a repository (if it has it)
0 commit comments