/// <summary> /// Will ask for the name or id of the playlist /// Will then remove all the items in the playlist_track /// And then remove the playlist /// </summary> public static void RemovePlaylist() { Console.WriteLine("\nEnter the playlist you would like to remove (PlaylistId or Name)"); string PlaylistName = Console.ReadLine(); int playlistID; bool isNumber = Int32.TryParse(PlaylistName, out playlistID); Playlist playlist = new Playlist(); PlaylistTrack playListTrack = new PlaylistTrack(); using (var context = new MusicContext()) { if (isNumber) { playlist = context.Playlists.SingleOrDefault( x => x.PlaylistId == playlistID); } else { playlist = context.Playlists.SingleOrDefault( x => x.Name == PlaylistName); } if (playlist != null) { playListTrack = context.PlaylistTracks.Where(x => x.PlaylistId == playlist.PlaylistId).FirstOrDefault(); if (playListTrack != null) { context.Database.ExecuteSqlRaw("delete from music.playlist_track where PlaylistId={0}", playListTrack.PlaylistId); context.SaveChanges(); } context.Playlists.Remove(playlist); context.SaveChanges(); Console.WriteLine("They have been successfully deleted"); } else { Console.WriteLine("Could not find the playlist.\n"); } } }
/// <summary> /// Will ask the user for trackids to remove form the list /// </summary> /// <param name="playlistInfo">Information about the playlist (Could be name or id)</param> private static void ModifyRemove(string playlistInfo) { bool moreTracks = true; List <int> trackList = new List <int>(); while (moreTracks) { Console.WriteLine("\nEnter the TrackId that you would like to remove \n(-1 to continue without removing more)"); int trackID; bool isValid = true; string Input = Console.ReadLine(); if (Input == "-1") { moreTracks = false; } foreach (char c in Input) { if (!Char.IsDigit(c)) { isValid = false; } } if (isValid) { trackID = Int32.Parse(Input); trackList.Add(trackID); } } //Checks if the playlistInfo is the name or the id. int playlistID; bool isNumber = Int32.TryParse(playlistInfo, out playlistID); Playlist playlist = new Playlist(); PlaylistTrack playListTrack = new PlaylistTrack(); using (var context = new MusicContext()) { if (isNumber) { playlist = context.Playlists.SingleOrDefault( x => x.PlaylistId == playlistID); } else { playlist = context.Playlists.SingleOrDefault( x => x.Name == playlistInfo); } foreach (var item in trackList) { playListTrack = context.PlaylistTracks.Where(x => x.PlaylistId == playlist.PlaylistId && x.TrackId == item).SingleOrDefault(); if (playListTrack != null) { context.Database.ExecuteSqlRaw("delete from music.playlist_track where TrackId={0}", playListTrack.TrackId); context.SaveChanges(); } } Console.WriteLine("They have been successfully removed"); } }