We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 491cc06 commit 44572e9Copy full SHA for 44572e9
models/fixtures/project.yml
@@ -7,6 +7,8 @@
7
creator_id: 2
8
board_type: 1
9
type: 2
10
+ created_unix: 1688973030
11
+ updated_unix: 1688973030
12
13
-
14
id: 2
@@ -17,6 +19,8 @@
17
19
creator_id: 3
18
20
21
22
+ created_unix: 1688973010
23
+ updated_unix: 1688973010
24
25
26
id: 3
@@ -27,6 +31,8 @@
27
31
creator_id: 5
28
32
29
33
34
+ created_unix: 1688973020
35
+ updated_unix: 1688973020
30
36
37
38
id: 4
@@ -37,3 +43,5 @@
43
44
39
45
46
+ created_unix: 1688973000
47
+ updated_unix: 1688973000
models/project/project.go
@@ -196,7 +196,7 @@ type SearchOptions struct {
196
RepoID int64
197
Page int
198
IsClosed util.OptionalBool
199
- SortType string
+ OrderBy db.SearchOrderBy
200
Type Type
201
}
202
@@ -226,26 +226,28 @@ func CountProjects(ctx context.Context, opts SearchOptions) (int64, error) {
226
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Project))
227
228
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
+
242
// FindProjects returns a list of all projects that have been created in the repository
243
func FindProjects(ctx context.Context, opts SearchOptions) ([]*Project, int64, error) {
- e := db.GetEngine(ctx).Where(opts.toConds())
244
+ e := db.GetEngine(ctx).Where(opts.toConds()).OrderBy(opts.OrderBy.String())
245
projects := make([]*Project, 0, setting.UI.IssuePagingNum)
246
247
if opts.Page > 0 {
248
e = e.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)
249
250
- 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")
- }
251
count, err := e.FindAndCount(&projects)
252
return projects, count, err
253
models/project/project_test.go
@@ -82,3 +82,42 @@ func TestProject(t *testing.T) {
82
83
assert.True(t, projectFromDB.IsClosed)
84
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
104
105
106
+ sortType: "leastupdate",
107
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
@@ -62,7 +62,7 @@ func Projects(ctx *context.Context) {
62
OwnerID: ctx.ContextUser.ID,
63
Page: page,
64
IsClosed: util.OptionalBoolOf(isShowClosed),
65
- SortType: sortType,
+ OrderBy: project_model.GetSearchOrderByBySortType(sortType),
66
Type: projectType,
67
})
68
if err != nil {
routers/web/repo/projects.go
@@ -74,7 +74,7 @@ func Projects(ctx *context.Context) {
74
RepoID: repo.ID,
75
76
77
78
Type: project_model.TypeRepository,
79
80
0 commit comments