Skip to content

Commit 5b87c05

Browse files
yp05327lunny
andauthored
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 <[email protected]>
1 parent 0bc8bb3 commit 5b87c05

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

Diff for: models/project/project.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,8 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) {
217217

218218
// FindProjects returns a list of all projects that have been created in the repository
219219
func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) {
220-
e := db.GetEngine(ctx)
220+
e := db.GetEngine(ctx).Where(opts.toConds())
221221
projects := make([]*Project, 0, setting.UI.IssuePagingNum)
222-
cond := opts.toConds()
223-
224-
count, err := e.Where(cond).Count(new(Project))
225-
if err != nil {
226-
return nil, 0, fmt.Errorf("Count: %w", err)
227-
}
228-
229-
e = e.Where(cond)
230222

231223
if opts.Page > 0 {
232224
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
243235
e.Asc("created_unix")
244236
}
245237

246-
return projects, count, e.Find(&projects)
238+
count, err := e.FindAndCount(&projects)
239+
return projects, count, err
247240
}
248241

249242
// NewProject creates a new Project

0 commit comments

Comments
 (0)