using MySql.Data.MySqlClient; using Sony.Filtr.AppleMusic.BestOfTheWeek.Caching; using Sony.Filtr.AppleMusic.BestOfTheWeek.Caching.Models; using Sony.Filtr.AppleMusic.BestOfTheWeek.Entities; using Sony.Filtr.Database; using Sony.Filtr.Utility.Extensions; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Sony.Filtr.AppleMusic.BestOfTheWeek { public class BestOfTheWeekManager { private readonly BestOfTheWeekCacheProvider _cacheProvider; private readonly BestOfTheWeekFactory _botwFactory; public BestOfTheWeekManager(BestOfTheWeekCacheProvider cacheProvider, BestOfTheWeekFactory botwFactory) { _cacheProvider = cacheProvider; _botwFactory = botwFactory; } public bool IsTrackListChanged(IList existingTracklist, IList newTracks) { if (existingTracklist.Count != newTracks.Count) return true; existingTracklist = existingTracklist.OrderBy(x => x.PlaylistIndex).ToList(); newTracks = newTracks.OrderBy(x => x.PlaylistIndex).ToList(); for (int playlistIndex = 0; playlistIndex < existingTracklist.Count; playlistIndex++) { var oldTrack = existingTracklist[playlistIndex]; var newTrack = newTracks[playlistIndex]; if (oldTrack.SongId != newTrack.SongId) return true; } return false; } public async Task>> GetTracklistsAsync(IList playlists, DateTime date) { var playlistCollections = new ConcurrentDictionary>(); await playlists.ForEachAsync(20, async playlist => { var tracks = await GetPlaylistTracksAsync(playlist, date); playlistCollections.TryAdd(playlist, tracks); }); return playlistCollections; } private async Task> GetPlaylistTracksAsync(BestOfTheWeekPlaylistReference playlist, DateTime date) { var playlistCollection = _cacheProvider.GetPlaylistCollection(playlist, date); if (playlistCollection == null) { playlistCollection = await _botwFactory.GetPlaylistTracksAsync(playlist, date); _cacheProvider.SetPlaylistCollection(playlist, date, playlistCollection); } return playlistCollection; } public async Task> GetPlaylistReferenceCacheItems() { var playlistReferences = _cacheProvider.GetPlaylistReferences(); if (playlistReferences == null) { playlistReferences = new List(); const string sql = "SELECT DISTINCT pl.Id, botwpl.Storefront, pl.Name, pl.Artwork FROM tblAppleMusicBestOfTheWeekPlaylist AS botwpl " + "INNER JOIN tblAppleMusicPlaylist AS pl ON pl.Id = botwpl.PlaylistId"; using (var conn = await DatabaseHandler.GetOpenReadOnlyConnectionAsync()) using (var command = new MySqlCommand(sql, conn)) using (var reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { playlistReferences.Add(new BestOfTheWeekPlaylistReference() { PlaylistId = reader.GetString("Id"), Storefront = reader.GetString("Storefront") }); } } _cacheProvider.SetPlaylistReferences(playlistReferences); } return playlistReferences; } public async Task> GetAvailableFridays() { var dates = new List(); const string sql = "SELECT DISTINCT Date FROM tblAppleMusicBestOfTheWeekPlaylistTrack ORDER BY Date DESC"; using (var conn = await DatabaseHandler.GetOpenReadOnlyConnectionAsync()) using (var command = new MySqlCommand(sql, conn)) using (var reader = await command.ExecuteReaderAsync()) { while (reader.Read()) { dates.Add(reader.GetDateTime("Date")); } } return dates; } } }