aboutsummaryrefslogtreecommitdiffstats
path: root/models/topic.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/topic.go')
-rw-r--r--models/topic.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/models/topic.go b/models/topic.go
index 19c572fefe..7fc34f5bef 100644
--- a/models/topic.go
+++ b/models/topic.go
@@ -184,7 +184,7 @@ func (opts *FindTopicOptions) toConds() builder.Cond {
}
// FindTopics retrieves the topics via FindTopicOptions
-func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
+func FindTopics(opts *FindTopicOptions) ([]*Topic, int64, error) {
sess := x.Select("topic.*").Where(opts.toConds())
if opts.RepoID > 0 {
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
@@ -192,7 +192,18 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
if opts.PageSize != 0 && opts.Page != 0 {
sess = opts.setSessionPagination(sess)
}
- return topics, sess.Desc("topic.repo_count").Find(&topics)
+ topics := make([]*Topic, 0, 10)
+ total, err := sess.Desc("topic.repo_count").FindAndCount(&topics)
+ return topics, total, err
+}
+
+// CountTopics counts the number of topics matching the FindTopicOptions
+func CountTopics(opts *FindTopicOptions) (int64, error) {
+ sess := x.Where(opts.toConds())
+ if opts.RepoID > 0 {
+ sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
+ }
+ return sess.Count(new(Topic))
}
// GetRepoTopicByName retrieves topic from name for a repo if it exist
@@ -269,7 +280,7 @@ func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
// SaveTopics save topics to a repository
func SaveTopics(repoID int64, topicNames ...string) error {
- topics, err := FindTopics(&FindTopicOptions{
+ topics, _, err := FindTopics(&FindTopicOptions{
RepoID: repoID,
})
if err != nil {