From 25797b9604ed068e219523e89bd4b29d7b020197 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 23 Feb 2023 13:33:05 +0900 Subject: [PATCH 1/2] improve FindProjects --- models/project/project.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/models/project/project.go b/models/project/project.go index 931ef44675afd..0e2c6e020ae41 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -217,15 +217,10 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) { // FindProjects returns a list of all projects that have been created in the repository func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) { - e := db.GetEngine(ctx) + e := db.GetEngine(ctx).Table("project") projects := make([]*Project, 0, setting.UI.IssuePagingNum) cond := opts.toConds() - count, err := e.Where(cond).Count(new(Project)) - if err != nil { - return nil, 0, fmt.Errorf("Count: %w", err) - } - e = e.Where(cond) if opts.Page > 0 { @@ -243,7 +238,8 @@ func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, e e.Asc("created_unix") } - return projects, count, e.Find(&projects) + count, err := e.FindAndCount(&projects) + return projects, count, err } // NewProject creates a new Project From 5739442543757737746fa03ebe686c9fef69c48c Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Fri, 24 Feb 2023 00:03:25 +0900 Subject: [PATCH 2/2] remove Table --- models/project/project.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/models/project/project.go b/models/project/project.go index 0e2c6e020ae41..46b5c07c424f1 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -217,11 +217,8 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) { // FindProjects returns a list of all projects that have been created in the repository func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) { - e := db.GetEngine(ctx).Table("project") + e := db.GetEngine(ctx).Where(opts.toConds()) projects := make([]*Project, 0, setting.UI.IssuePagingNum) - cond := opts.toConds() - - e = e.Where(cond) if opts.Page > 0 { e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)