aboutsummaryrefslogtreecommitdiffstats
path: root/models/topic.go
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2021-08-12 14:43:08 +0200
committerGitHub <noreply@github.com>2021-08-12 14:43:08 +0200
commit2289580bb7ef8dfa4124c2b3bfb89897dbb35f46 (patch)
treee68aae604bb3d738b44156504a1415adaf042c68 /models/topic.go
parentca13e1d56c561f72cf8ad251fe61b1898abfec51 (diff)
downloadgitea-2289580bb7ef8dfa4124c2b3bfb89897dbb35f46.tar.gz
gitea-2289580bb7ef8dfa4124c2b3bfb89897dbb35f46.zip
[API] generalize list header (#16551)
* Add info about list endpoints to CONTRIBUTING.md * Let all list endpoints return X-Total-Count header * Add TODOs for GetCombinedCommitStatusByRef * Fix models/issue_stopwatch.go * Rrefactor models.ListDeployKeys * Introduce helper func and use them for SetLinkHeader related func
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 {