improve FindProjects (#23085)

I found `FindAndCount` which can `Find` and `Count` in the same time
Maybe it is better to use it in `FindProjects`

---------

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
yp05327 2023-02-24 14:18:52 +09:00 committed by GitHub
parent 0bc8bb3cc4
commit 5b87c05a95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 10 deletions

View File

@ -217,16 +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 // FindProjects returns a list of all projects that have been created in the repository
func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) { func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) {
e := db.GetEngine(ctx) e := db.GetEngine(ctx).Where(opts.toConds())
projects := make([]*Project, 0, setting.UI.IssuePagingNum) 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 { if opts.Page > 0 {
e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum) e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
@ -243,7 +235,8 @@ func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, e
e.Asc("created_unix") e.Asc("created_unix")
} }
return projects, count, e.Find(&projects) count, err := e.FindAndCount(&projects)
return projects, count, err
} }
// NewProject creates a new Project // NewProject creates a new Project