1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-30 09:41:11 -05:00

Fix incorrect oldest sort in project list (#25806) (#25835)

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:
Giteabot 2023-07-12 01:22:17 -04:00 committed by GitHub
parent c1a10be07e
commit 052e65e63f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 15 deletions

View file

@ -7,6 +7,8 @@
creator_id: 2 creator_id: 2
board_type: 1 board_type: 1
type: 2 type: 2
created_unix: 1688973030
updated_unix: 1688973030
- -
id: 2 id: 2
@ -17,6 +19,8 @@
creator_id: 3 creator_id: 3
board_type: 1 board_type: 1
type: 2 type: 2
created_unix: 1688973010
updated_unix: 1688973010
- -
id: 3 id: 3
@ -27,6 +31,8 @@
creator_id: 5 creator_id: 5
board_type: 1 board_type: 1
type: 2 type: 2
created_unix: 1688973020
updated_unix: 1688973020
- -
id: 4 id: 4
@ -37,3 +43,5 @@
creator_id: 2 creator_id: 2
board_type: 1 board_type: 1
type: 2 type: 2
created_unix: 1688973000
updated_unix: 1688973000

View file

@ -196,7 +196,7 @@ type SearchOptions struct {
RepoID int64 RepoID int64
Page int Page int
IsClosed util.OptionalBool IsClosed util.OptionalBool
SortType string OrderBy db.SearchOrderBy
Type Type 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)) 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 // 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).Where(opts.toConds()) e := db.GetEngine(ctx).Where(opts.toConds()).OrderBy(opts.OrderBy.String())
projects := make([]*Project, 0, setting.UI.IssuePagingNum) projects := make([]*Project, 0, setting.UI.IssuePagingNum)
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)
} }
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) count, err := e.FindAndCount(&projects)
return projects, count, err return projects, count, err
} }

View file

@ -82,3 +82,42 @@ func TestProject(t *testing.T) {
assert.True(t, projectFromDB.IsClosed) assert.True(t, projectFromDB.IsClosed)
} }
func TestProjectsSort(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
tests := []struct {
sortType string
wants []int64
}{
{
sortType: "default",
wants: []int64{1, 3, 2, 4},
},
{
sortType: "oldest",
wants: []int64{4, 2, 3, 1},
},
{
sortType: "recentupdate",
wants: []int64{1, 3, 2, 4},
},
{
sortType: "leastupdate",
wants: []int64{4, 2, 3, 1},
},
}
for _, tt := range tests {
projects, count, err := FindProjects(db.DefaultContext, SearchOptions{
OrderBy: GetSearchOrderByBySortType(tt.sortType),
})
assert.NoError(t, err)
assert.EqualValues(t, int64(4), count)
if assert.Len(t, projects, 4) {
for i := range projects {
assert.EqualValues(t, tt.wants[i], projects[i].ID)
}
}
}
}

View file

@ -61,7 +61,7 @@ func Projects(ctx *context.Context) {
OwnerID: ctx.ContextUser.ID, OwnerID: ctx.ContextUser.ID,
Page: page, Page: page,
IsClosed: util.OptionalBoolOf(isShowClosed), IsClosed: util.OptionalBoolOf(isShowClosed),
SortType: sortType, OrderBy: project_model.GetSearchOrderByBySortType(sortType),
Type: projectType, Type: projectType,
}) })
if err != nil { if err != nil {

View file

@ -74,7 +74,7 @@ func Projects(ctx *context.Context) {
RepoID: repo.ID, RepoID: repo.ID,
Page: page, Page: page,
IsClosed: util.OptionalBoolOf(isShowClosed), IsClosed: util.OptionalBoolOf(isShowClosed),
SortType: sortType, OrderBy: project_model.GetSearchOrderByBySortType(sortType),
Type: project_model.TypeRepository, Type: project_model.TypeRepository,
}) })
if err != nil { if err != nil {