Generic handler for object history

This commit is contained in:
Eduard Urbach 2018-04-10 22:12:32 +02:00
parent 1d658a9e76
commit d0e518bedf
6 changed files with 50 additions and 95 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

30
utils/history/history.go Normal file
View File

@ -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))
}
}