From c5cb497722e2eb88e65c2925c2042b4cd93bcd21 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 18 Nov 2017 11:37:29 +0100 Subject: [PATCH] API Cleanup --- pages/anime/anime.go | 6 +----- pages/anime/tracks.go | 6 +----- pages/companies/companies.go | 31 +++++++++++++++++++++++++++++++ pages/companies/companies.pixy | 0 pages/dashboard/dashboard.go | 7 +------ pages/profile/tracks.go | 6 +----- pages/soundtracks/soundtracks.go | 12 ++---------- 7 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 pages/companies/companies.go create mode 100644 pages/companies/companies.pixy diff --git a/pages/anime/anime.go b/pages/anime/anime.go index 04dafdc5..e784e51b 100644 --- a/pages/anime/anime.go +++ b/pages/anime/anime.go @@ -72,7 +72,7 @@ func Get(ctx *aero.Context) string { } // Soundtracks - tracks, err := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 && arn.Contains(track.Tags, "anime:"+anime.ID) }) @@ -80,10 +80,6 @@ func Get(ctx *aero.Context) string { return tracks[i].Title < tracks[j].Title }) - if err != nil { - return ctx.Error(http.StatusNotFound, "Error fetching soundtracks", err) - } - // Open Graph description := anime.Summary diff --git a/pages/anime/tracks.go b/pages/anime/tracks.go index c82a1f95..48da67df 100644 --- a/pages/anime/tracks.go +++ b/pages/anime/tracks.go @@ -19,13 +19,9 @@ func Tracks(ctx *aero.Context) string { return ctx.Error(http.StatusNotFound, "Anime not found", err) } - tracks, err := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 && arn.Contains(track.Tags, "anime:"+anime.ID) }) - if err != nil { - return ctx.Error(http.StatusNotFound, "Error fetching soundtracks", err) - } - return ctx.HTML(components.AnimeTracks(anime, tracks)) } diff --git a/pages/companies/companies.go b/pages/companies/companies.go new file mode 100644 index 00000000..c6216223 --- /dev/null +++ b/pages/companies/companies.go @@ -0,0 +1,31 @@ +package companies + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" +) + +const maxEntries = 12 + +// Get renders the companies page. +func Get(ctx *aero.Context) string { + user := utils.GetUser(ctx) + + companies := arn.FilterCompanies(func(company *arn.Company) bool { + return !company.IsDraft + }) + + if err != nil { + return ctx.Error(http.StatusInternalServerError, "Error fetching companies", err) + } + + if len(companies) > maxEntries { + companies = companies[:maxEntries] + } + + return ctx.HTML(components.Companies(companies, user)) +} diff --git a/pages/companies/companies.pixy b/pages/companies/companies.pixy new file mode 100644 index 00000000..e69de29b diff --git a/pages/dashboard/dashboard.go b/pages/dashboard/dashboard.go index 4e6371ba..d0ec0cfd 100644 --- a/pages/dashboard/dashboard.go +++ b/pages/dashboard/dashboard.go @@ -72,15 +72,10 @@ func Get(ctx *aero.Context) string { upcomingEpisodes = upcomingEpisodes[:maxScheduleItems] } }, func() { - var err error - soundTracks, err = arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + soundTracks = arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 }) - if err != nil { - return - } - arn.SortSoundTracksLatestFirst(soundTracks) if len(soundTracks) > maxSoundTracks { diff --git a/pages/profile/tracks.go b/pages/profile/tracks.go index 083f3ae9..29e270d4 100644 --- a/pages/profile/tracks.go +++ b/pages/profile/tracks.go @@ -19,14 +19,10 @@ func GetSoundTracksByUser(ctx *aero.Context) string { return ctx.Error(http.StatusNotFound, "User not found", err) } - tracks, err := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 && track.CreatedBy == viewUser.ID }) - if err != nil { - return ctx.Error(http.StatusInternalServerError, "Error fetching soundtracks", err) - } - arn.SortSoundTracksLatestFirst(tracks) return ctx.HTML(components.TrackList(tracks, viewUser, user, ctx.URI())) diff --git a/pages/soundtracks/soundtracks.go b/pages/soundtracks/soundtracks.go index ce2052ec..2ff3913f 100644 --- a/pages/soundtracks/soundtracks.go +++ b/pages/soundtracks/soundtracks.go @@ -16,14 +16,10 @@ const maxTracks = 12 func Get(ctx *aero.Context) string { user := utils.GetUser(ctx) - tracks, err := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 }) - if err != nil { - return ctx.Error(http.StatusInternalServerError, "Error fetching soundtracks", err) - } - arn.SortSoundTracksLatestFirst(tracks) if len(tracks) > maxTracks { @@ -42,14 +38,10 @@ func From(ctx *aero.Context) string { return ctx.Error(http.StatusBadRequest, "Invalid start index", err) } - allTracks, err := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 }) - if err != nil { - return ctx.Error(http.StatusInternalServerError, "Error fetching soundtracks", err) - } - if index < 0 || index >= len(allTracks) { return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allTracks))+")", nil) }