mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-05-20 14:00:04 +02:00
Backport #25806 by @yp05327 sort type `oldest` should be `Asc`. Added a test for this. I see we have `SearchOrderBy` in db model, but we are using many different ways to define the sort type. ~Maybe we can improve this later.~ ↑ Improved in this PR Co-authored-by: yp05327 <576951401@qq.com>
This commit is contained in:
parent
c1a10be07e
commit
052e65e63f
5 changed files with 64 additions and 15 deletions
|
@ -196,7 +196,7 @@ type SearchOptions struct {
|
|||
RepoID int64
|
||||
Page int
|
||||
IsClosed util.OptionalBool
|
||||
SortType string
|
||||
OrderBy db.SearchOrderBy
|
||||
Type Type
|
||||
}
|
||||
|
||||
|
@ -226,26 +226,28 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) {
|
|||
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Project))
|
||||
}
|
||||
|
||||
func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy {
|
||||
switch sortType {
|
||||
case "oldest":
|
||||
return db.SearchOrderByOldest
|
||||
case "recentupdate":
|
||||
return db.SearchOrderByRecentUpdated
|
||||
case "leastupdate":
|
||||
return db.SearchOrderByLeastUpdated
|
||||
default:
|
||||
return db.SearchOrderByNewest
|
||||
}
|
||||
}
|
||||
|
||||
// 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).Where(opts.toConds())
|
||||
e := db.GetEngine(ctx).Where(opts.toConds()).OrderBy(opts.OrderBy.String())
|
||||
projects := make([]*Project, 0, setting.UI.IssuePagingNum)
|
||||
|
||||
if opts.Page > 0 {
|
||||
e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
|
||||
}
|
||||
|
||||
switch opts.SortType {
|
||||
case "oldest":
|
||||
e.Desc("created_unix")
|
||||
case "recentupdate":
|
||||
e.Desc("updated_unix")
|
||||
case "leastupdate":
|
||||
e.Asc("updated_unix")
|
||||
default:
|
||||
e.Asc("created_unix")
|
||||
}
|
||||
|
||||
count, err := e.FindAndCount(&projects)
|
||||
return projects, count, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue