From 69d4b618256fae82de6483c2dc6732e1c1b4d9a5 Mon Sep 17 00:00:00 2001 From: Humaid AlQassimi Date: Fri, 14 Aug 2020 20:13:02 +0400 Subject: [PATCH] Sort moderation logs. --- models/moderation_sort.go | 20 ++++++++++++++++++++ routes/main.go | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 models/moderation_sort.go diff --git a/models/moderation_sort.go b/models/moderation_sort.go new file mode 100644 index 0000000..bf6cd36 --- /dev/null +++ b/models/moderation_sort.go @@ -0,0 +1,20 @@ +package models + +import ( + "time" +) + +// ModerationSort implements sort.Interface for []Moderation depending on date. +type ModerationSort []Moderation + +func (p ModerationSort) Len() int { + return len(p) +} + +func (p ModerationSort) Swap(i, j int) { + p[i], p[j] = p[j], p[i] +} + +func (p ModerationSort) Less(i, j int) bool { + return time.Unix(p[i].CreatedUnix, 0).After(time.Unix(p[j].CreatedUnix, 0)) +} diff --git a/routes/main.go b/routes/main.go index 0b02b94..edff31b 100644 --- a/routes/main.go +++ b/routes/main.go @@ -3,6 +3,7 @@ package routes import ( "bytes" "log" + "sort" "github.com/hw-cs-reps/platform/config" "github.com/hw-cs-reps/platform/models" @@ -25,7 +26,9 @@ func HomepageHandler(ctx *macaron.Context, sess session.Store, f *session.Flash) // ModLogsHandler response for the moderation log page. func ModLogsHandler(ctx *macaron.Context, sess session.Store, f *session.Flash) { ctx.Data["Title"] = "Moderation Log" - ctx.Data["Logs"] = models.GetModerations() + logs := models.GetModerations() + sort.Sort(models.ModerationSort(logs)) + ctx.Data["Logs"] = logs ctx.HTML(200, "moderations") }