]> source.dussan.org Git - gitea.git/commitdiff
Fix incorrect oldest sort in project list (#25806)
authoryp05327 <576951401@qq.com>
Tue, 11 Jul 2023 18:47:50 +0000 (03:47 +0900)
committerGitHub <noreply@github.com>
Tue, 11 Jul 2023 18:47:50 +0000 (20:47 +0200)
sort type `oldest` should be `Asc`.
Added a test for this.

models/fixtures/project.yml
models/project/project.go
models/project/project_test.go
routers/web/org/projects.go
routers/web/repo/projects.go

index 3fa4286c53a0cbe9b6d784a6fb78fdc5cb21f5a2..1bf8030f6aa57e3c09f9be3e837824acd016c1c9 100644 (file)
@@ -7,6 +7,8 @@
   creator_id: 2
   board_type: 1
   type: 2
+  created_unix: 1688973030
+  updated_unix: 1688973030
 
 -
   id: 2
@@ -17,6 +19,8 @@
   creator_id: 3
   board_type: 1
   type: 2
+  created_unix: 1688973010
+  updated_unix: 1688973010
 
 -
   id: 3
@@ -27,6 +31,8 @@
   creator_id: 5
   board_type: 1
   type: 2
+  created_unix: 1688973020
+  updated_unix: 1688973020
 
 -
   id: 4
@@ -37,3 +43,5 @@
   creator_id: 2
   board_type: 1
   type: 2
+  created_unix: 1688973000
+  updated_unix: 1688973000
index 44609e60b2ea098dcdd1338707e9dcba5fd4a6c8..6fa2adf604dc4a3cf2de0e19c6ad534a2a67d18b 100644 (file)
@@ -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
 }
index 71ceda7aa5ad28f72286c787bae2e30ee3d6fefa..d1a471565367ce7347da37f42c78b8b3e53315c1 100644 (file)
@@ -82,3 +82,42 @@ func TestProject(t *testing.T) {
 
        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)
+                       }
+               }
+       }
+}
index 21cb23000d2e9ca7ce2d81006e980a1e294295ff..60032c777d0508780709a61621e12437ce4220d1 100644 (file)
@@ -62,7 +62,7 @@ func Projects(ctx *context.Context) {
                OwnerID:  ctx.ContextUser.ID,
                Page:     page,
                IsClosed: util.OptionalBoolOf(isShowClosed),
-               SortType: sortType,
+               OrderBy:  project_model.GetSearchOrderByBySortType(sortType),
                Type:     projectType,
        })
        if err != nil {
index 066cdbc5fd88908b1a786e40372e821b099e3f42..6bfb21c134cbddcdf7a638764811b8d1a214e03b 100644 (file)
@@ -74,7 +74,7 @@ func Projects(ctx *context.Context) {
                RepoID:   repo.ID,
                Page:     page,
                IsClosed: util.OptionalBoolOf(isShowClosed),
-               SortType: sortType,
+               OrderBy:  project_model.GetSearchOrderByBySortType(sortType),
                Type:     project_model.TypeRepository,
        })
        if err != nil {