From 57273481a67025959c979a7103ed97e533769160 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 19 Nov 2017 17:02:20 +0100 Subject: [PATCH] Latest and best soundtrack categories --- pages/index.go | 6 ++- pages/soundtracks/best.go | 66 ++++++++++++++++++++++++++++++ pages/soundtracks/soundtracks.go | 8 ++-- pages/soundtracks/soundtracks.pixy | 11 ++++- 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 pages/soundtracks/best.go diff --git a/pages/index.go b/pages/index.go index d4c7f5dc..5d8c7e6b 100644 --- a/pages/index.go +++ b/pages/index.go @@ -108,8 +108,10 @@ func Configure(app *aero.Application) { l.Page("/settings/pro", settings.Get(components.SettingsPro)) // Soundtracks - l.Page("/soundtracks", soundtracks.Get) - l.Page("/soundtracks/from/:index", soundtracks.From) + l.Page("/soundtracks", soundtracks.Latest) + l.Page("/soundtracks/from/:index", soundtracks.LatestFrom) + l.Page("/soundtracks/best", soundtracks.Best) + l.Page("/soundtracks/best/from/:index", soundtracks.BestFrom) l.Page("/soundtrack/:id", soundtrack.Get) l.Page("/soundtrack/:id/edit", soundtrack.Edit) diff --git a/pages/soundtracks/best.go b/pages/soundtracks/best.go new file mode 100644 index 00000000..6197d84a --- /dev/null +++ b/pages/soundtracks/best.go @@ -0,0 +1,66 @@ +package soundtracks + +import ( + "net/http" + "strconv" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" +) + +// Best renders the soundtracks page. +func Best(ctx *aero.Context) string { + user := utils.GetUser(ctx) + + tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + return !track.IsDraft && len(track.Media) > 0 + }) + + arn.SortSoundTracksPopularFirst(tracks) + + if len(tracks) > maxTracks { + tracks = tracks[:maxTracks] + } + + return ctx.HTML(components.SoundTracks(tracks, maxTracks, user)) +} + +// BestFrom renders the soundtracks from the given index. +func BestFrom(ctx *aero.Context) string { + user := utils.GetUser(ctx) + index, err := ctx.GetInt("index") + + if err != nil { + return ctx.Error(http.StatusBadRequest, "Invalid start index", err) + } + + allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { + return !track.IsDraft && len(track.Media) > 0 + }) + + if index < 0 || index >= len(allTracks) { + return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allTracks))+")", nil) + } + + arn.SortSoundTracksPopularFirst(allTracks) + + tracks := allTracks[index:] + + if len(tracks) > maxTracks { + tracks = tracks[:maxTracks] + } + + nextIndex := index + maxTracks + + if nextIndex >= len(allTracks) { + // End of data - no more scrolling + ctx.Response().Header().Set("X-LoadMore-Index", "-1") + } else { + // Send the index for the next request + ctx.Response().Header().Set("X-LoadMore-Index", strconv.Itoa(nextIndex)) + } + + return ctx.HTML(components.SoundTracksScrollable(tracks, user)) +} diff --git a/pages/soundtracks/soundtracks.go b/pages/soundtracks/soundtracks.go index 2ff3913f..f881d2a5 100644 --- a/pages/soundtracks/soundtracks.go +++ b/pages/soundtracks/soundtracks.go @@ -12,8 +12,8 @@ import ( const maxTracks = 12 -// Get renders the soundtracks page. -func Get(ctx *aero.Context) string { +// Latest renders the soundtracks page. +func Latest(ctx *aero.Context) string { user := utils.GetUser(ctx) tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { @@ -29,8 +29,8 @@ func Get(ctx *aero.Context) string { return ctx.HTML(components.SoundTracks(tracks, maxTracks, user)) } -// From renders the soundtracks from the given index. -func From(ctx *aero.Context) string { +// LatestFrom renders the soundtracks from the given index. +func LatestFrom(ctx *aero.Context) string { user := utils.GetUser(ctx) index, err := ctx.GetInt("index") diff --git a/pages/soundtracks/soundtracks.pixy b/pages/soundtracks/soundtracks.pixy index 3e998a59..a4e41ad8 100644 --- a/pages/soundtracks/soundtracks.pixy +++ b/pages/soundtracks/soundtracks.pixy @@ -1,5 +1,7 @@ component SoundTracks(tracks []*arn.SoundTrack, tracksPerPage int, user *arn.User) - h1 Soundtracks + h1.page-title Soundtracks + + SoundTracksTabs .corner-buttons if user != nil @@ -21,4 +23,9 @@ component SoundTracks(tracks []*arn.SoundTrack, tracksPerPage int, user *arn.Use component SoundTracksScrollable(tracks []*arn.SoundTrack, user *arn.User) each track in tracks - SoundTrack(track) \ No newline at end of file + SoundTrack(track) + +component SoundTracksTabs + .tabs + Tab("Latest", "music", "/soundtracks") + Tab("Best", "heart", "/soundtracks/best") \ No newline at end of file