using System; using System.Linq; using System.Text; using MoreLinq; using Nancy; using Nancy.ModelBinding; using Sony.Filtr.AdminAPI.BusinessLogic; using Sony.Filtr.AdminAPI.Utility; using Sony.Filtr.AdminAPI.ViewModels; using Sony.Filtr.Applications; using Sony.Filtr.Contracts.Entities; using Sony.Filtr.Core.EditorialPlaylists; using Sony.Filtr.Playlists.Spotify; using Sony.Filtr.SpotifyWebAPI; namespace Sony.Filtr.AdminAPI.Modules.EditorialPlaylist { public class EditorialPlaylistModule : BaseModule { public EditorialPlaylistModule(EditorialPlaylistBusinessLogic editorialPlaylistBusinessLogic, EditorialPlaylistStatsManager editorialPlaylistStatsManager, EditorialPlaylistManager editorialPlaylistManager, ApplicationInstanceManager applicationInstanceManager, SpotifyWebApi spotifyWebApi, SpotifyPlaylistManager spotifyPlaylistManager, EditorialPlaylistViewModelHelper editorialPlaylistViewModelHelper) : base(applicationInstanceManager) { Get["GetEditorialPlaylistByCountry", "/EditorialPlaylistByCountry", true] = async (parameters, cx) => { var getDetails = (bool) Request.Query.getDetails; var playlists = await editorialPlaylistManager.GetEditorialPlaylistsAsync(null, onlyActive: false, lightWeight: !getDetails); var viewModels = await editorialPlaylistViewModelHelper.GetExtendedPlaylistInfoAsync(playlists); var groupedPlaylists = viewModels.DistinctBy(p=> p.SpotifyLink).Where(p=> !string.IsNullOrWhiteSpace(p.CountryCode)).GroupBy(p => p.CountryCode.ToLowerInvariant()).OrderBy(m => m.Key) .ToDictionary(playlistCountry => playlistCountry.Key, playlistCountry => playlistCountry.ToList()); return Response.AsJson(groupedPlaylists); }; Get["GetAllEditorialPlaylists", "/EditorialPlaylist", true] = async (parameters, cx) => { var getDetails = (bool) Request.Query.getDetails; var playlists = await editorialPlaylistManager.GetEditorialPlaylistsAsync(null, onlyActive: false, lightWeight: !getDetails); var viewModels = await editorialPlaylistViewModelHelper.GetExtendedPlaylistInfoAsync(playlists); return Response.AsJson(viewModels); }; Get["GetSpecifiedEditorialPlaylistsExcel", "/EditorialPlaylist/excel", true] = async (parameters, cx) => { var playlistIds = ((string)Request.Query.playlistId).Split(',').Select(p => int.Parse(p)); var playlists = playlistIds.Select(p => editorialPlaylistManager.GetEditorialPlaylistAsync(p).Result).ToList(); var playlistStats = await editorialPlaylistStatsManager.GetPlaylistStatsAsync(playlists); var sb = new StringBuilder(); sb.AppendLine("Name;SpotifyUri;Subscribers;Active;Priority;"); foreach (var playlistStat in playlistStats) { var playlist = playlistStat.Playlist; var columnValues = new[] { playlist.Name, playlist.SpotifyLink.Uri, playlistStat.LatestSubscribers.ToString(), playlist.Active.ToString(), playlist.Priority.ToString() }; sb.AppendLine(string.Join(";", columnValues)); } var playlistExportFileName = string.Format("PlaylistExport-{0}-{1}.csv", "Selected-playlists", DateTime.Today.ToShortDateString()); return Response.AsText(sb.ToString(), "text/csv").WithHeader("Content-disposition", "attachment;filename=\"" + playlistExportFileName + "\""); }; Get["GetEditorialPlaylistInMarket", "{countryCode}/EditorialPlaylist", true] = async (parameters, cx) => { var getDetails = (bool)Request.Query.getDetails; var playlists = await editorialPlaylistManager.GetEditorialPlaylistsAsync(CurrentApplication, onlyActive: false, lightWeight: !getDetails); var viewModels = await editorialPlaylistViewModelHelper.GetExtendedPlaylistInfoAsync(playlists); return Response.AsJson(viewModels); }; Get["GetEditorialPlaylistInMarketExcel", "{countryCode}/EditorialPlaylist/excel", true] = async (parameters, cx) => { var app = CurrentApplication; var playlists = await editorialPlaylistManager.GetLightweightEditorialPlaylistsAsync(app, onlyActive: false); //var playlistStats = editorialPlaylistStatsManager.GetPlaylistStats(playlists); var playlistAttributes = await editorialPlaylistManager.GetPlaylistAttributesAsync(playlists); var playlistExportFileName = $"PlaylistExport-{app.Name}-{ExcelUtility.GetFilenameDatePart()}.xlsx"; return ExcelUtility.BuildExcelResponse(playlistAttributes, playlistExportFileName); }; Post["AddEditorialPlaylist", "{countryCode}/EditorialPlaylist", true] = async (parameters, token) => { var editorialPlaylist = this.Bind(); if (CurrentApplication == null || editorialPlaylist == null) return HttpStatusCode.BadRequest; var spotifyLink = new SpotifyLink(editorialPlaylist.SpotifyLink); var existingPlaylist = await editorialPlaylistManager.GetEditorialPlaylistAsync(CurrentApplication, spotifyLink); if (existingPlaylist != null) return HttpStatusCode.BadRequest; try { const string fields = "name,description,owner"; var spotifyPlaylist = await spotifyWebApi.GetPlaylistByIdAsync(spotifyLink.ExtractPlaylistID(), fields); if (spotifyPlaylist != null) { if (string.IsNullOrWhiteSpace(editorialPlaylist.DisplayName)) editorialPlaylist.DisplayName = spotifyPlaylist.name; editorialPlaylist.Name = spotifyPlaylist.name; editorialPlaylist.SpotifyDescription = spotifyPlaylist.description; editorialPlaylist.OwnerUsername = spotifyPlaylist.owner?.id; } } catch (Exception) { return HttpStatusCode.BadRequest; } var addedPlaylist = await editorialPlaylistBusinessLogic.SavePlaylistAsync(CurrentApplication, null, editorialPlaylist); var viewModel = await editorialPlaylistViewModelHelper.GetExtendedPlaylistInfoAsync(addedPlaylist); return Response.AsJson(viewModel, HttpStatusCode.Created); }; Get["GetSingleEditorialPlaylist", "{countryCode}/EditorialPlaylist/{id}", true] = async (parameters, cx) => { int playlistId; if (!int.TryParse((string) parameters.id, out playlistId)) return HttpStatusCode.BadRequest; var existingPlaylist = await editorialPlaylistManager.GetEditorialPlaylistAsync(playlistId); if (existingPlaylist == null) return HttpStatusCode.NotFound; var viewModels = await editorialPlaylistViewModelHelper.GetExtendedPlaylistInfoAsync(existingPlaylist); return Response.AsJson(viewModels); }; Put["UpdateEditorialPlaylist", "{countryCode}/EditorialPlaylist/{id}", true] = async (parameters, cx) => { var editorialPlaylist = this.Bind(); if (CurrentApplication == null || editorialPlaylist == null) return HttpStatusCode.BadRequest; int playlistId; if (!int.TryParse((string)parameters.id, out playlistId)) return HttpStatusCode.BadRequest; var existingPlaylist = await editorialPlaylistManager.GetEditorialPlaylistAsync(playlistId); if (existingPlaylist == null) return HttpStatusCode.NotFound; //TODO: Check access rights for playlist and app. var addedPlaylist = await editorialPlaylistBusinessLogic.SavePlaylistAsync(CurrentApplication, playlistId, editorialPlaylist); var viewModel = await editorialPlaylistViewModelHelper.GetExtendedPlaylistInfoAsync(addedPlaylist); return Response.AsJson(viewModel, HttpStatusCode.Created); }; Delete["DeleteEditorialPlaylist", "{countryCode}/EditorialPlaylist/{id}", true] = async (parameters,cx ) => { int id; if (!int.TryParse((string) parameters.id, out id)) return HttpStatusCode.BadRequest; //TODO: Check access rights var editorialPlaylist = await editorialPlaylistManager.GetEditorialPlaylistAsync(id); if (editorialPlaylist == null) return HttpStatusCode.NotFound; await editorialPlaylistManager.DeleteEditorialPlaylistAsync(editorialPlaylist); return HttpStatusCode.OK; }; } } }