From 82ec230b6643b5ce0e2bdfe88d7914c053f2be69 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 12 Apr 2022 16:44:03 +0800 Subject: [PATCH 1/2] Simple the code to get issue count --- models/issue.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/models/issue.go b/models/issue.go index b1fa2d02ad1b5..424592ac15157 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1510,20 +1510,14 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) { func CountIssues(opts *IssuesOptions) (int64, error) { e := db.GetEngine(db.DefaultContext) - countsSlice := make([]*struct { - Count int64 - }, 0, 1) - + var cnt int64 sess := e.Select("COUNT(issue.id) AS count").Table("issue") sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") opts.setupSessionNoLimit(sess) - if err := sess.Find(&countsSlice); err != nil { + if _, err := sess.Get(&cnt); err != nil { return 0, fmt.Errorf("unable to CountIssues: %w", err) } - if len(countsSlice) != 1 { - return 0, fmt.Errorf("unable to get one row result when CountIssues, row count=%d", len(countsSlice)) - } - return countsSlice[0].Count, nil + return cnt, nil } // GetParticipantsIDsByIssueID returns the IDs of all users who participated in comments of an issue, From 16103bbef5bdda1f847d0e9e5d1084fb7c35a7a1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 12 Apr 2022 19:40:43 +0800 Subject: [PATCH 2/2] Improve codes --- models/issue.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/models/issue.go b/models/issue.go index 424592ac15157..68582b2b148d4 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1510,14 +1510,10 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) { func CountIssues(opts *IssuesOptions) (int64, error) { e := db.GetEngine(db.DefaultContext) - var cnt int64 sess := e.Select("COUNT(issue.id) AS count").Table("issue") sess.Join("INNER", "repository", "`issue`.repo_id = `repository`.id") opts.setupSessionNoLimit(sess) - if _, err := sess.Get(&cnt); err != nil { - return 0, fmt.Errorf("unable to CountIssues: %w", err) - } - return cnt, nil + return sess.Count() } // GetParticipantsIDsByIssueID returns the IDs of all users who participated in comments of an issue,