public static bool AddPlayingSongToCollection() { using (var s_Db = Database.GetConnection()) { var s_Song = s_Db.SingleById<SongEntry>(Application.Library.Broadcast.PlayingSongID); if (s_Song != null) return false; s_Song = new SongEntry() { AlbumID = Application.Library.Broadcast.PlayingAlbumID, AlbumName = Application.Library.Broadcast.PlayingSongAlbum, ArtistID = Application.Library.Broadcast.PlayingArtistID, ArtistName = Application.Library.Broadcast.PlayingSongArtist, SongID = Application.Library.Broadcast.PlayingSongID, SongName = Application.Library.Broadcast.PlayingSongName }; s_Db.Insert(s_Song); CollectionSongs.Add(s_Song.SongID); return true; } }
public SongsModule() { this.RequiresAuthentication(); Get["/songs"] = p_Parameters => { return View["Songs", new { SuperUser = Context.CurrentUser.Claims.Contains("super") }]; }; Get["/songs/all.json"] = p_Parameters => { using (var s_Db = Database.GetConnection()) { var s_Serialized = JsonConvert.SerializeObject(s_Db.Select<SongEntry>()); var s_Encoded = Encoding.UTF8.GetBytes(s_Serialized); return new Response() { ContentType = "application/json", Contents = p_Writer => p_Writer.Write(s_Encoded, 0, s_Encoded.Length) }; } }; Get["/songs/add"] = p_Parameters => { return View["AddSong", new { SuperUser = Context.CurrentUser.Claims.Contains("super"), Error = "" }]; }; Get["/songs/autocomplete/{query}.json"] = p_Parameters => { String s_Query = p_Parameters.query; var s_Results = Application.Library.Search.GetAutocomplete(s_Query, "song"); if (s_Results.ContainsKey("song")) return s_Results["song"]; return new List<ResultData>(); }; Post["/songs/add"] = p_Parameters => { var s_Request = this.Bind<AddSongRequest>(); if (s_Request.SongID <= 0 || s_Request.AlbumID <= 0 || s_Request.ArtistID <= 0 || String.IsNullOrWhiteSpace(s_Request.Song) || String.IsNullOrWhiteSpace(s_Request.Album) || String.IsNullOrWhiteSpace(s_Request.Artist)) { return View["AddSong", new { SuperUser = Context.CurrentUser.Claims.Contains("super"), Error = "Please fill in all the required fields." }]; } var s_PreviousSongCount = QueueManager.CollectionSongs.Count; using (var s_Db = Database.GetConnection()) { var s_Song = s_Db.SingleById<SongEntry>(s_Request.SongID); if (s_Song != null) return View["AddSong", new { SuperUser = Context.CurrentUser.Claims.Contains("super"), Error = "The specified song already exists in your collection." }]; s_Song = new SongEntry() { AlbumName = s_Request.Album, AlbumID = s_Request.AlbumID, ArtistID = s_Request.ArtistID, ArtistName = s_Request.Artist, SongID = s_Request.SongID, SongName = s_Request.Song }; s_Db.Insert(s_Song); QueueManager.CollectionSongs.Add(s_Song.SongID); } if (s_PreviousSongCount < 2 && QueueManager.CollectionSongs.Count >= 2) BroadcastManager.CreateBroadcast(); return new RedirectResponse("/songs"); }; Get["/songs/delete/{song:long}"] = p_Parameters => { Int64 s_SongID = p_Parameters.song; // Don't allow the user to delete the last two songs. if (QueueManager.CollectionSongs.Count <= 2) return new RedirectResponse("/songs"); using (var s_Db = Database.GetConnection()) { var s_Song = s_Db.SingleById<SongEntry>(s_SongID); if (s_Song == null) return new RedirectResponse("/songs"); QueueManager.CollectionSongs.Remove(s_SongID); s_Db.Delete<PlaylistEntry>(p_Entry => p_Entry.SongID == s_Song.SongID); s_Db.Delete(s_Song); } return new RedirectResponse("/songs"); }; Get["/songs/import"] = p_Parameters => { return View["ImportSongs", new { SuperUser = Context.CurrentUser.Claims.Contains("super"), User = Application.Library.User.Data.UserID }]; }; Get["/songs/import/autocomplete/{query}.json"] = p_Parameters => { String s_Query = p_Parameters.query; var s_Results = Application.Library.Search.GetAutocomplete(s_Query, "user"); if (s_Results.ContainsKey("user")) return s_Results["user"]; return new List<ResultData>(); }; Post["/songs/import"] = p_Parameters => { var s_Request = this.Bind<ImportSongsFromUserRequest>(); var s_PreviousSongCount = QueueManager.CollectionSongs.Count; var s_SongEntries = new List<SongEntry>(); // Fetch collection songs. if (!s_Request.Only) { var s_Songs = Application.Library.User.GetSongsInLibrary(s_Request.User); foreach (var s_Song in s_Songs) { try { var s_SongEntry = new SongEntry() { SongID = Int64.Parse(s_Song.SongID), SongName = s_Song.Name, ArtistID = Int64.Parse(s_Song.ArtistID), ArtistName = s_Song.ArtistName, AlbumID = Int64.Parse(s_Song.AlbumID), AlbumName = s_Song.AlbumName }; s_SongEntries.Add(s_SongEntry); } catch { continue; } } } // Fetch favorite songs. if (s_Request.Favorites || s_Request.Only) { var s_Favorites = Application.Library.User.GetFavorites("Songs", s_Request.User); foreach (var s_Song in s_Favorites) { try { var s_SongEntry = new SongEntry() { SongID = Int64.Parse(s_Song.SongID), SongName = s_Song.Name, ArtistID = Int64.Parse(s_Song.ArtistID), ArtistName = s_Song.ArtistName, AlbumID = Int64.Parse(s_Song.AlbumID), AlbumName = s_Song.AlbumName }; s_SongEntries.Add(s_SongEntry); } catch { continue; } } } // Remove duplicates. s_SongEntries = s_SongEntries.DistinctBy(p_Entry => p_Entry.SongID).ToList(); // Store using (var s_Db = Database.GetConnection()) s_Db.SaveAll(s_SongEntries); // Update loaded collection QueueManager.FetchCollectionSongs(); // Create the broadcast (if needed). if (s_PreviousSongCount < 2 && QueueManager.CollectionSongs.Count >= 2) BroadcastManager.CreateBroadcast(); return new RedirectResponse("/songs"); }; Get["/songs/wipe"] = p_Parameters => { using (var s_Db = Database.GetConnection()) { // Get all songs. var s_Songs = s_Db.Select<SongEntry>(); if (s_Songs.Count <= 2) return new RedirectResponse("/songs"); // Drop all playlists. PlaylistManager.DeleteAllPlaylists(); var s_IDs = s_Songs.Select(p_Song => p_Song.SongID).ToList(); // Keep the first two songs. s_IDs.RemoveRange(0, 2); // Delete all songs. s_Db.DeleteByIds<SongEntry>(s_IDs); } return new RedirectResponse("/songs"); }; }
public static void ImportPlaylist(Int64 p_ExternalID) { var s_PlaylistData = Application.Library.User.GetPlaylistData(p_ExternalID); if (s_PlaylistData == null) return; using (var s_Db = Database.GetConnection()) { var s_Playlist = s_Db.Single<Playlist>(p_Playlist => p_Playlist.GrooveSharkID == s_PlaylistData.PlaylistID); if (s_Playlist == null) { s_Playlist = new Playlist() { Description = s_PlaylistData.About, Name = s_PlaylistData.Name, GrooveSharkID = s_PlaylistData.PlaylistID }; } // Insert/update the playlist. s_Db.Save(s_Playlist); var s_Songs = new List<PlaylistEntry>(); var s_NewSongs = new List<SongEntry>(); foreach (var s_Song in s_PlaylistData.Songs) { s_Songs.Add(new PlaylistEntry() { PlaylistID = s_Playlist.ID, SongID = s_Song.SongID, Index = 0 }); var s_LocalSong = s_Db.SingleById<SongEntry>(s_Song.SongID); if (s_LocalSong != null) continue; s_LocalSong = new SongEntry() { SongID = s_Song.SongID, SongName = s_Song.Name, ArtistID = s_Song.ArtistID, ArtistName = s_Song.ArtistName, AlbumID = s_Song.AlbumID, AlbumName = s_Song.AlbumName, }; s_NewSongs.Add(s_LocalSong); } s_Songs = s_Songs.DistinctBy(p_Entry => p_Entry.SongID).ToList(); s_NewSongs = s_NewSongs.DistinctBy(p_Song => p_Song.SongID).ToList(); // Calculate indexes. for (var i = 0; i < s_Songs.Count; ++i) s_Songs[i].Index = i; // Add songs that don't exist to the collection. s_Db.SaveAll(s_NewSongs); // Delete old song entries. s_Db.Delete<PlaylistEntry>(p_Entry => p_Entry.PlaylistID == s_Playlist.ID); // Import the new ones. s_Db.SaveAll(s_Songs); } }