@@ -197,10 +197,13 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
197197
198198// GetRepoTopicByName retrives topic from name for a repo if it exist
199199func 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 ) {
200203 var cond = builder .NewCond ()
201204 var topic Topic
202205 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 )
204207 sess .Join ("INNER" , "repo_topic" , "repo_topic.topic_id = topic.id" )
205208 has , err := sess .Get (& topic )
206209 if has {
@@ -211,7 +214,13 @@ func GetRepoTopicByName(repoID int64, topicName string) (*Topic, error) {
211214
212215// AddTopic adds a topic name to a repository (if it does not already have it)
213216func 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 )
215224 if err != nil {
216225 return nil , err
217226 }
@@ -220,7 +229,25 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) {
220229 return topic , nil
221230 }
222231
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 ()
224251}
225252
226253// DeleteTopic removes a topic name from a repository (if it has it)
0 commit comments