/// <summary> /// Will run the method "AddTracks" which returns a list of trackids /// and then addd them to the database. /// </summary> /// <param name="playlistInfo">Information about the playlist (Could be name or id)</param> private static void ModifyAdd(string playlistInfo) { List <int> trackList = new List <int>(); trackList = AddTracks(); //Checks if the playlistInfo is the name or the id. int playlistID; bool isNumber = Int32.TryParse(playlistInfo, out playlistID); Track track = new Track(); Playlist playlist = new Playlist(); using (var context = new MusicContext()) { if (isNumber) { playlist = context.Playlists.SingleOrDefault( x => x.PlaylistId == playlistID); } else { playlist = context.Playlists.SingleOrDefault( x => x.Name == playlistInfo); } if (playlist != null) { foreach (var item in trackList) { track = context.Tracks.SingleOrDefault(x => x.TrackId == item); if (track != null && playlist != null) { context.Database.ExecuteSqlRaw("insert into music.playlist_track values({0}, {1})", playlist.PlaylistId, track.TrackId); context.SaveChanges(); } } Console.WriteLine("They have been successfully added"); } else { Console.WriteLine("\nCould not find 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"); } }