using Nancy; using Sony.Filtr.AdminAPI.ViewModels.BestOfTheWeek; using Sony.Filtr.AppleMusic.BestOfTheWeek; using Sony.Filtr.Contracts.Abstractions; using Sony.Filtr.Contracts.Entities; using Sony.Filtr.NewMusicFriday; using Sony.Filtr.Utility.Extensions; using System; using System.Linq; using System.Threading.Tasks; namespace Sony.Filtr.AdminAPI.Modules { public class BestOfTheWeekModule : BaseModule { private readonly BestOfTheWeekFactory _botwFactory; private readonly BestOfTheWeekManager _botwManager; private readonly NewMusicFridayManager _nmfManager; public BestOfTheWeekModule(IApplicationInstanceManager applicationInstanceManager, BestOfTheWeekFactory botwFactory, BestOfTheWeekManager botwManager, NewMusicFridayManager nmfManager) : base(applicationInstanceManager) { _botwFactory = botwFactory; _botwManager = botwManager; _nmfManager = nmfManager; Get["GetAppleMusicTracksForFriday", "/newMusicFriday/appleMusic/", runAsync: true] = async (parameters, cx) => { var date = DetermineDateToUse(Request); var limit = (int?)Request.Query.limit ?? 100; var offset = (int?)Request.Query.offset ?? 0; var viewModel = await GetTrackListResponseAsync(date, limit, offset); return Response.AsJson(viewModel); }; Get["GetAllAvailableAppleMusicNewMusicFridays", "/newMusicFriday/appleMusic/availableDates", runAsync: true] = async (parameters, cx) => { return Response.AsJson((await _botwManager.GetAvailableFridays()).Select(x => x.ToNeutralShortDateString())); }; Get["GetAppleMusicPlaylists", "/newMusicFriday/appleMusic/playlists", runAsync: true] = async (parameters, cx) => { return Response.AsJson((await _botwFactory.GetPlaylistsData()) .Select(x => new BestOfTheWeekPlaylistViewModel() { PlaylistId = x.PlaylistId, Name = x.Name, CountryCode = x.Storefront.ToLowerInvariant(), FridayLastUpdatedDate = x.FridayLastUpdatedDate.ToNeutralShortDateString() })); }; } private DateTime DetermineDateToUse(Request request) { if (DateTime.TryParse((string)request.Query.date, out var parsedDate)) return parsedDate; return _nmfManager.GetPrecedingOrCurrentFriday(DateTime.UtcNow); } private async Task> GetTrackListResponseAsync(DateTime date, int limit, int offset) { var playlistReferences = await _botwManager.GetPlaylistReferenceCacheItems(); var playlistCollections = await _botwManager.GetTracklistsAsync(playlistReferences, date); var trackViewModels = playlistCollections .SelectMany(p => p.Value) .GroupBy(p => p.Isrc).Select(p => { var track = p.FirstOrDefault(x => !string.IsNullOrEmpty(x.TrackId)); //to make sure all data is from the same track we just pick one even though they are grouped by ISRC return new BestOfTheWeekTrackViewModel() { Isrc = p.Key, TrackName = track?.TrackName, TrackId = track?.TrackId, Artists = track?.Artists, Playlists = p.Select(pi => new BestOfTheWeekTrackPlaylistReference() { PlaylistId = pi.PlaylistId, Position = pi.PlaylistIndex + 1, CountryCode = pi.Storefront, }).OrderBy(x => x.Position).ToList(), AlbumImageUrl = track?.AlbumImageUrl, TopTenFeatureCount = p.Count(pi => pi.PlaylistIndex < 10) }; }).ToList(); return trackViewModels.OrderByDescending(x => x.Playlists.Count) .ThenByDescending(x => x.TopTenFeatureCount) .ToList() .Paginate(limit, offset); } } }