Skip to content

Commit 052e65e

Browse files
GiteaBotyp05327
andauthored
Fix incorrect oldest sort in project list (go-gitea#25806) (go-gitea#25835)
Backport go-gitea#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>
1 parent c1a10be commit 052e65e

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

models/fixtures/project.yml

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
creator_id: 2
88
board_type: 1
99
type: 2
10+
created_unix: 1688973030
11+
updated_unix: 1688973030
1012

1113
-
1214
id: 2
@@ -17,6 +19,8 @@
1719
creator_id: 3
1820
board_type: 1
1921
type: 2
22+
created_unix: 1688973010
23+
updated_unix: 1688973010
2024

2125
-
2226
id: 3
@@ -27,6 +31,8 @@
2731
creator_id: 5
2832
board_type: 1
2933
type: 2
34+
created_unix: 1688973020
35+
updated_unix: 1688973020
3036

3137
-
3238
id: 4
@@ -37,3 +43,5 @@
3743
creator_id: 2
3844
board_type: 1
3945
type: 2
46+
created_unix: 1688973000
47+
updated_unix: 1688973000

models/project/project.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ type SearchOptions struct {
196196
RepoID int64
197197
Page int
198198
IsClosed util.OptionalBool
199-
SortType string
199+
OrderBy db.SearchOrderBy
200200
Type Type
201201
}
202202

@@ -226,26 +226,28 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) {
226226
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Project))
227227
}
228228

229+
func GetSearchOrderByBySortType(sortType string) db.SearchOrderBy {
230+
switch sortType {
231+
case "oldest":
232+
return db.SearchOrderByOldest
233+
case "recentupdate":
234+
return db.SearchOrderByRecentUpdated
235+
case "leastupdate":
236+
return db.SearchOrderByLeastUpdated
237+
default:
238+
return db.SearchOrderByNewest
239+
}
240+
}
241+
229242
// FindProjects returns a list of all projects that have been created in the repository
230243
func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) {
231-
e := db.GetEngine(ctx).Where(opts.toConds())
244+
e := db.GetEngine(ctx).Where(opts.toConds()).OrderBy(opts.OrderBy.String())
232245
projects := make([]*Project, 0, setting.UI.IssuePagingNum)
233246

234247
if opts.Page > 0 {
235248
e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
236249
}
237250

238-
switch opts.SortType {
239-
case "oldest":
240-
e.Desc("created_unix")
241-
case "recentupdate":
242-
e.Desc("updated_unix")
243-
case "leastupdate":
244-
e.Asc("updated_unix")
245-
default:
246-
e.Asc("created_unix")
247-
}
248-
249251
count, err := e.FindAndCount(&projects)
250252
return projects, count, err
251253
}

models/project/project_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,42 @@ func TestProject(t *testing.T) {
8282

8383
assert.True(t, projectFromDB.IsClosed)
8484
}
85+
86+
func TestProjectsSort(t *testing.T) {
87+
assert.NoError(t, unittest.PrepareTestDatabase())
88+
89+
tests := []struct {
90+
sortType string
91+
wants []int64
92+
}{
93+
{
94+
sortType: "default",
95+
wants: []int64{1, 3, 2, 4},
96+
},
97+
{
98+
sortType: "oldest",
99+
wants: []int64{4, 2, 3, 1},
100+
},
101+
{
102+
sortType: "recentupdate",
103+
wants: []int64{1, 3, 2, 4},
104+
},
105+
{
106+
sortType: "leastupdate",
107+
wants: []int64{4, 2, 3, 1},
108+
},
109+
}
110+
111+
for _, tt := range tests {
112+
projects, count, err := FindProjects(db.DefaultContext, SearchOptions{
113+
OrderBy: GetSearchOrderByBySortType(tt.sortType),
114+
})
115+
assert.NoError(t, err)
116+
assert.EqualValues(t, int64(4), count)
117+
if assert.Len(t, projects, 4) {
118+
for i := range projects {
119+
assert.EqualValues(t, tt.wants[i], projects[i].ID)
120+
}
121+
}
122+
}
123+
}

routers/web/org/projects.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Projects(ctx *context.Context) {
6161
OwnerID: ctx.ContextUser.ID,
6262
Page: page,
6363
IsClosed: util.OptionalBoolOf(isShowClosed),
64-
SortType: sortType,
64+
OrderBy: project_model.GetSearchOrderByBySortType(sortType),
6565
Type: projectType,
6666
})
6767
if err != nil {

routers/web/repo/projects.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func Projects(ctx *context.Context) {
7474
RepoID: repo.ID,
7575
Page: page,
7676
IsClosed: util.OptionalBoolOf(isShowClosed),
77-
SortType: sortType,
77+
OrderBy: project_model.GetSearchOrderByBySortType(sortType),
7878
Type: project_model.TypeRepository,
7979
})
8080
if err != nil {

0 commit comments

Comments
 (0)