From d0e518bedf9a97623622e351c8eecc16d4a7731b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 10 Apr 2018 22:12:32 +0200 Subject: [PATCH] Generic handler for object history --- pages/character/history.go | 23 ++++------------------- pages/company/history.go | 23 ++++------------------- pages/editanime/history.go | 23 ++++------------------- pages/quote/history.go | 23 ++++------------------- pages/soundtrack/history.go | 23 ++++------------------- utils/history/history.go | 30 ++++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 95 deletions(-) create mode 100644 utils/history/history.go diff --git a/pages/character/history.go b/pages/character/history.go index 11be7709..723d63b1 100644 --- a/pages/character/history.go +++ b/pages/character/history.go @@ -1,29 +1,14 @@ package character import ( - "net/http" - - "github.com/aerogo/aero" "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" - "github.com/animenotifier/notify.moe/utils" + "github.com/animenotifier/notify.moe/utils/history" ) // History of the edits. -func History(ctx *aero.Context) string { - id := ctx.Get("id") - user := utils.GetUser(ctx) - character, err := arn.GetCharacter(id) +var History = history.Handler(renderHistory, "Character") - if err != nil { - return ctx.Error(http.StatusNotFound, "Character not found", err) - } - - entries := arn.FilterEditLogEntries(func(entry *arn.EditLogEntry) bool { - return entry.ObjectType == "Character" && entry.ObjectID == id - }) - - arn.SortEditLogEntriesLatestFirst(entries) - - return ctx.HTML(components.CharacterTabs(character, user) + components.EditLog(entries, user)) +func renderHistory(obj interface{}, entries []*arn.EditLogEntry, user *arn.User) string { + return components.CharacterTabs(obj.(*arn.Character), user) + components.EditLog(entries, user) } diff --git a/pages/company/history.go b/pages/company/history.go index b2d9b14c..9b054c0f 100644 --- a/pages/company/history.go +++ b/pages/company/history.go @@ -1,29 +1,14 @@ package company import ( - "net/http" - - "github.com/aerogo/aero" "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" - "github.com/animenotifier/notify.moe/utils" + "github.com/animenotifier/notify.moe/utils/history" ) // History of the edits. -func History(ctx *aero.Context) string { - id := ctx.Get("id") - user := utils.GetUser(ctx) - company, err := arn.GetCompany(id) +var History = history.Handler(renderHistory, "Company") - if err != nil { - return ctx.Error(http.StatusNotFound, "Company not found", err) - } - - entries := arn.FilterEditLogEntries(func(entry *arn.EditLogEntry) bool { - return entry.ObjectType == "Company" && entry.ObjectID == id - }) - - arn.SortEditLogEntriesLatestFirst(entries) - - return ctx.HTML(components.CompanyTabs(company, user) + components.EditLog(entries, user)) +func renderHistory(obj interface{}, entries []*arn.EditLogEntry, user *arn.User) string { + return components.CompanyTabs(obj.(*arn.Company), user) + components.EditLog(entries, user) } diff --git a/pages/editanime/history.go b/pages/editanime/history.go index 95033368..3b2f8866 100644 --- a/pages/editanime/history.go +++ b/pages/editanime/history.go @@ -1,29 +1,14 @@ package editanime import ( - "net/http" - - "github.com/aerogo/aero" "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" - "github.com/animenotifier/notify.moe/utils" + "github.com/animenotifier/notify.moe/utils/history" ) // History of the edits. -func History(ctx *aero.Context) string { - id := ctx.Get("id") - user := utils.GetUser(ctx) - anime, err := arn.GetAnime(id) +var History = history.Handler(renderHistory, "Anime", "AnimeCharacters", "AnimeRelations", "AnimeEpisodes") - if err != nil { - return ctx.Error(http.StatusNotFound, "Anime not found", err) - } - - entries := arn.FilterEditLogEntries(func(entry *arn.EditLogEntry) bool { - return entry.ObjectID == id && (entry.ObjectType == "Anime" || entry.ObjectType == "AnimeCharacters" || entry.ObjectType == "AnimeRelations" || entry.ObjectType == "AnimeEpisodes") - }) - - arn.SortEditLogEntriesLatestFirst(entries) - - return ctx.HTML(components.EditAnimeTabs(anime) + components.EditLog(entries, user)) +func renderHistory(obj interface{}, entries []*arn.EditLogEntry, user *arn.User) string { + return components.EditAnimeTabs(obj.(*arn.Anime)) + components.EditLog(entries, user) } diff --git a/pages/quote/history.go b/pages/quote/history.go index 1cb51039..5fe2f7e4 100644 --- a/pages/quote/history.go +++ b/pages/quote/history.go @@ -1,29 +1,14 @@ package quote import ( - "net/http" - - "github.com/aerogo/aero" "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" - "github.com/animenotifier/notify.moe/utils" + "github.com/animenotifier/notify.moe/utils/history" ) // History of the edits. -func History(ctx *aero.Context) string { - id := ctx.Get("id") - user := utils.GetUser(ctx) - quote, err := arn.GetQuote(id) +var History = history.Handler(renderHistory, "Quote") - if err != nil { - return ctx.Error(http.StatusNotFound, "Quote not found", err) - } - - entries := arn.FilterEditLogEntries(func(entry *arn.EditLogEntry) bool { - return entry.ObjectType == "Quote" && entry.ObjectID == id - }) - - arn.SortEditLogEntriesLatestFirst(entries) - - return ctx.HTML(components.QuoteTabs(quote, user) + components.EditLog(entries, user)) +func renderHistory(obj interface{}, entries []*arn.EditLogEntry, user *arn.User) string { + return components.QuoteTabs(obj.(*arn.Quote), user) + components.EditLog(entries, user) } diff --git a/pages/soundtrack/history.go b/pages/soundtrack/history.go index 40213a1e..9f747f5e 100644 --- a/pages/soundtrack/history.go +++ b/pages/soundtrack/history.go @@ -1,30 +1,15 @@ package soundtrack import ( - "net/http" - "github.com/animenotifier/notify.moe/components" - "github.com/animenotifier/notify.moe/utils" + "github.com/animenotifier/notify.moe/utils/history" - "github.com/aerogo/aero" "github.com/animenotifier/arn" ) // History of the edits. -func History(ctx *aero.Context) string { - id := ctx.Get("id") - user := utils.GetUser(ctx) - track, err := arn.GetSoundTrack(id) +var History = history.Handler(renderHistory, "SoundTrack") - if err != nil { - return ctx.Error(http.StatusNotFound, "Track not found", err) - } - - entries := arn.FilterEditLogEntries(func(entry *arn.EditLogEntry) bool { - return entry.ObjectType == "SoundTrack" && entry.ObjectID == id - }) - - arn.SortEditLogEntriesLatestFirst(entries) - - return ctx.HTML(components.SoundTrackTabs(track, user) + components.EditLog(entries, user)) +func renderHistory(obj interface{}, entries []*arn.EditLogEntry, user *arn.User) string { + return components.SoundTrackTabs(obj.(*arn.SoundTrack), user) + components.EditLog(entries, user) } diff --git a/utils/history/history.go b/utils/history/history.go new file mode 100644 index 00000000..d74b32d5 --- /dev/null +++ b/utils/history/history.go @@ -0,0 +1,30 @@ +package history + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/utils" +) + +// Handler returns a function that renders the history of any object. +func Handler(render func(interface{}, []*arn.EditLogEntry, *arn.User) string, typeNames ...string) func(ctx *aero.Context) string { + return func(ctx *aero.Context) string { + id := ctx.Get("id") + user := utils.GetUser(ctx) + obj, err := arn.DB.Get(typeNames[0], id) + + if err != nil { + return ctx.Error(http.StatusNotFound, typeNames[0]+" not found", err) + } + + entries := arn.FilterEditLogEntries(func(entry *arn.EditLogEntry) bool { + return entry.ObjectID == id && arn.Contains(typeNames, entry.ObjectType) + }) + + arn.SortEditLogEntriesLatestFirst(entries) + + return ctx.HTML(render(obj, entries, user)) + } +}