From 149de3cdbe62c10722c6e44af2fb4d2bac523031 Mon Sep 17 00:00:00 2001 From: yan Date: Mon, 1 Jun 2020 19:21:20 +0800 Subject: [PATCH] add attachments sort method --- routers/repo/dataset.go | 39 ++++++++++++----------- templates/repo/datasets/dataset_list.tmpl | 4 +-- templates/repo/datasets/index.tmpl | 2 -- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 8be5fa878..cef47ef70 100644 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -1,6 +1,8 @@ package repo import ( + "sort" + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/base" @@ -35,31 +37,30 @@ func DatasetIndex(ctx *context.Context) { ctx.ServerError("GetDatasetAttachments", err) return } + attachments := dataset.Attachments - // var orderBy models.SearchOrderBy - // page := ctx.QueryInt("page") - // if page <= 0 { - // page = 1 - // } - // ctx.Data["SortType"] = ctx.Query("sort") - // switch ctx.Query("sort") { - // case "newest": - // orderBy = models.SearchOrderByNewest - // case "oldest": - // orderBy = models.SearchOrderByOldest - // case "recentupdate": - // orderBy = models.SearchOrderByRecentUpdated - // case "leastupdate": - // orderBy = models.SearchOrderByLeastUpdated - // default: - // ctx.Data["SortType"] = "recentupdate" - // orderBy = models.SearchOrderByRecentUpdated - // } + ctx.Data["SortType"] = ctx.Query("sort") + switch ctx.Query("sort") { + case "newest": + sort.Slice(attachments, func(i, j int) bool { + return attachments[i].CreatedUnix > attachments[j].CreatedUnix + }) + case "oldest": + sort.Slice(attachments, func(i, j int) bool { + return attachments[i].CreatedUnix < attachments[j].CreatedUnix + }) + default: + ctx.Data["SortType"] = "newest" + sort.Slice(attachments, func(i, j int) bool { + return attachments[i].CreatedUnix > attachments[j].CreatedUnix + }) + } ctx.Data["PageIsDataset"] = true ctx.Data["Title"] = ctx.Tr("dataset.show_dataset") ctx.Data["Link"] = ctx.Repo.RepoLink + "/datasets" ctx.Data["dataset"] = dataset + ctx.Data["Attachments"] = attachments ctx.Data["IsOwner"] = true ctx.HTML(200, tplIndex) diff --git a/templates/repo/datasets/dataset_list.tmpl b/templates/repo/datasets/dataset_list.tmpl index df626c985..775e2d89a 100644 --- a/templates/repo/datasets/dataset_list.tmpl +++ b/templates/repo/datasets/dataset_list.tmpl @@ -1,5 +1,5 @@ -{{if .dataset.Attachments}} - {{range .dataset.Attachments}} +{{if .Attachments}} + {{range .Attachments}}
diff --git a/templates/repo/datasets/index.tmpl b/templates/repo/datasets/index.tmpl index afa33263b..344e093a9 100644 --- a/templates/repo/datasets/index.tmpl +++ b/templates/repo/datasets/index.tmpl @@ -53,8 +53,6 @@