@@ -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)
153164func 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)
170178func 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
0 commit comments