public void ViewSpecificPlaylist() { ViewUserPlaylists(); Console.WriteLine(); Console.Write("Enter playlist id to view: "); int currentUserId = AuthenticationService.LoggedUser.Id; int playlistId; bool isIntPlaylistId = int.TryParse(Console.ReadLine(), out playlistId); while (isIntPlaylistId == false) { Console.WriteLine("Id can only be an integer number"); Console.Write("Enter playlist id to view: "); isIntPlaylistId = int.TryParse(Console.ReadLine(), out playlistId); } UsersPlaylistsRepository usersPlaylistsRepo = new UsersPlaylistsRepository(Constants.UsersPlaylistsPath); bool hasRightsToShare = usersPlaylistsRepo.EntityExists(upe => upe.PlaylistId == playlistId && upe.UserId == currentUserId); if (hasRightsToShare == false) { Console.WriteLine("Playlist does not exist or you have no rights to view!"); Console.ReadKey(true); return; } PlaylistsSongsRepository playlistsSongsRepo = new PlaylistsSongsRepository(Constants.PlaylistsSongsPath); List <PlaylistsSongs> playlistsSongsEntities = playlistsSongsRepo.GetAll(pse => pse.PlaylistId == playlistId); SongsRepository songsRepo = new SongsRepository(Constants.SongsPath); SongsArtistsRepository songsArtistsRepo = new SongsArtistsRepository(Constants.SongsArtistsPath); ArtistsRepository artistsRepo = new ArtistsRepository(Constants.ArtistsPath); List <Song> songs = new List <Song>(); foreach (PlaylistsSongs playlistsSongsEntity in playlistsSongsEntities) { Song song = songsRepo.GetAll(s => s.Id == playlistsSongsEntity.SongId).FirstOrDefault(); songs.Add(song); } if (songs.Count == 0) { Console.WriteLine("Playlist empty!"); Console.ReadKey(true); return; } Console.Clear(); foreach (Song song in songs) { Console.WriteLine("********************************"); Console.WriteLine("Id: {0}", song.Id); Console.WriteLine("Song title: {0}", song.Title); Console.WriteLine("Song release year: {0}", song.Year); List <SongsArtists> songsArtists = songsArtistsRepo.GetAll(sa => sa.SongId == song.Id); List <Artist> artists = new List <Artist>(); foreach (SongsArtists songArtistItem in songsArtists) { int artistId = songArtistItem.ArtistId; Artist artist = artistsRepo.GetAll(a => a.Id == songArtistItem.ArtistId).FirstOrDefault(); artists.Add(artist); } if (artists.Count == 1) { Console.WriteLine("Artist: {0}", artists[0].Name); } else { Console.Write("Artists: "); int end = artists.Count - 1; for (int i = 0; i < end; i++) { Console.WriteLine("{0}, ", artists[i].Name); } Console.WriteLine(artists[end]); } Console.WriteLine("********************************"); } Console.ReadKey(true); }
public void AddSongToPlaylist() { SongsView.ViewSongs(); Console.WriteLine(); Console.Write("Enter id of song to add: "); int songId = 0; bool isIntSongId = int.TryParse(Console.ReadLine(), out songId); while (isIntSongId == false) { Console.WriteLine("Song id can only be an integer number. Try again!"); Console.ReadKey(true); Console.Write("Enter id of song to add: "); isIntSongId = int.TryParse(Console.ReadLine(), out songId); } SongsRepository songsRepo = new SongsRepository(Constants.SongsPath); if (songsRepo.EntityExists(s => s.Id == songId) == false) { Console.WriteLine("Song with id {0} doesn't exist!", songId); Console.ReadKey(true); return; } ViewUserPlaylists(); Console.Write("Select id of playlist you would add to: "); int playlistId = 0; bool isIntPlaylistId = int.TryParse(Console.ReadLine(), out playlistId); while (isIntPlaylistId == false) { Console.WriteLine("Song id can only be an integer number. Try again!"); Console.ReadKey(true); Console.Write("Enter id of song to add: "); isIntPlaylistId = int.TryParse(Console.ReadLine(), out playlistId); } UsersPlaylistsRepository usersPlaylistsRepo = new UsersPlaylistsRepository(Constants.UsersPlaylistsPath); int currentUserId = AuthenticationService.LoggedUser.Id; if (usersPlaylistsRepo.EntityExists(upe => upe.PlaylistId == playlistId && upe.UserId == currentUserId) == false) { Console.WriteLine("Playlist does not exist exist or you have no rights to add songs to it!"); Console.ReadKey(true); return; } PlaylistsSongs playlistSongsEntity = new PlaylistsSongs(); playlistSongsEntity.SongId = songId; playlistSongsEntity.PlaylistId = playlistId; PlaylistsSongsRepository playlistsSongsRepo = new PlaylistsSongsRepository(Constants.PlaylistsSongsPath); if (playlistsSongsRepo.EntityExists(p => p.PlaylistId == playlistSongsEntity.PlaylistId && p.SongId == playlistSongsEntity.SongId)) { Console.WriteLine("The selected song is already in that playlist!"); Console.ReadKey(true); return; } playlistsSongsRepo.Save(playlistSongsEntity); Console.WriteLine("Song successfully added to playlist"); Console.ReadKey(true); }
public void RemoveSongFromPlaylist() { ViewUserPlaylists(); Console.WriteLine(); Console.Write("Enter id of playlist: "); int playlistId; bool isIntPlaylistId = int.TryParse(Console.ReadLine(), out playlistId); while (isIntPlaylistId == false) { Console.WriteLine("Id can only be an integer number. Try again!!"); Console.ReadKey(); Console.Write("Enter id of playlist: "); isIntPlaylistId = int.TryParse(Console.ReadLine(), out playlistId); } UsersPlaylistsRepository usersPlaylistsRepo = new UsersPlaylistsRepository(Constants.UsersPlaylistsPath); int currentUserId = AuthenticationService.LoggedUser.Id; if (usersPlaylistsRepo.EntityExists(upe => upe.PlaylistId == playlistId && upe.UserId == currentUserId) == false) { Console.WriteLine("Playlist does not exist exist or you have no rights to add songs to it!"); Console.ReadKey(true); return; } PlaylistsSongsRepository playlistsSongsRepo = new PlaylistsSongsRepository(Constants.PlaylistsSongsPath); List <PlaylistsSongs> playlistsSongsEntities = playlistsSongsRepo.GetAll(pse => pse.PlaylistId == playlistId); SongsRepository songsRepo = new SongsRepository(Constants.SongsPath); SongsArtistsRepository songsArtistsRepo = new SongsArtistsRepository(Constants.SongsArtistsPath); ArtistsRepository artistsRepo = new ArtistsRepository(Constants.ArtistsPath); List <Song> songs = new List <Song>(); foreach (PlaylistsSongs playlistSongsEntity in playlistsSongsEntities) { Song song = songsRepo.GetAll(s => s.Id == playlistSongsEntity.SongId).FirstOrDefault(); songs.Add(song); } Console.Clear(); foreach (Song song in songs) { Console.WriteLine("********************************"); Console.WriteLine("Id: {0}", song.Id); Console.WriteLine("Song title: {0}", song.Title); Console.WriteLine("Song release year: {0}", song.Year); List <SongsArtists> songsArtists = songsArtistsRepo.GetAll(sa => sa.SongId == song.Id); List <Artist> artists = new List <Artist>(); foreach (SongsArtists songArtistItem in songsArtists) { int artistId = songArtistItem.ArtistId; Artist artist = artistsRepo.GetAll(a => a.Id == songArtistItem.ArtistId).FirstOrDefault(); artists.Add(artist); } if (artists.Count == 1) { Console.WriteLine("Artist: {0}", artists[0].Name); } else { Console.Write("Artists: "); int end = artists.Count - 1; for (int i = 0; i < end; i++) { Console.WriteLine("{0}, ", artists[i].Name); } Console.WriteLine(artists[end]); } Console.WriteLine("********************************"); } Console.WriteLine(); Console.Write("Select song id to remove from the playlist: "); int songId; bool isIntSongId = int.TryParse(Console.ReadLine(), out songId); while (isIntSongId == false) { Console.WriteLine("Id can only be an integer number. Try again!!"); Console.ReadKey(); Console.Write("Select song id to remove from the playlist: "); isIntSongId = int.TryParse(Console.ReadLine(), out songId); } PlaylistsSongs playlistsSongsEntity = playlistsSongsRepo.GetAll(pse => pse.PlaylistId == playlistId && pse.SongId == songId).FirstOrDefault(); if (playlistsSongsEntity == null) { Console.WriteLine("Song does not exist in that playlist or you have no rights to remove!"); Console.ReadKey(true); return; } playlistsSongsRepo.Delete(playlistsSongsEntity); Console.WriteLine("Song successfully removed from playlist!"); Console.ReadKey(true); }
public void SharePlaylist() //TODO: Ensure there are checks if playlist is already shared. { UsersRepository usersRepo = new UsersRepository(Constants.UsersPath); int currentUserId = AuthenticationService.LoggedUser.Id; List <User> users = usersRepo.GetAll(u => u.IsAdministrator != true && u.Id != currentUserId); Console.Clear(); if (users.Count == 0) { Console.WriteLine("There are currently no users with whom you can share a playlist."); Console.ReadKey(true); return; } foreach (User user in users) { Console.WriteLine("**************************"); Console.WriteLine("Id: {0}", user.Id); Console.WriteLine("Name: {0}", user.DisplayName); Console.WriteLine("**************************"); } Console.WriteLine(); Console.Write("Enter Id of user with whom you will share playlist: "); int userId = 0; bool isIntId = int.TryParse(Console.ReadLine(), out userId); while (isIntId == false) { Console.WriteLine("User Id can only be an integer number. Try again!!"); Console.ReadKey(true); Console.Write("Enter Id of user with whom you will share playlist: "); isIntId = int.TryParse(Console.ReadLine(), out userId); } bool isValidUser = false; foreach (User user in users) { if (user.Id == userId) { isValidUser = true; break; } } if (isValidUser == false) { Console.WriteLine("You cannot share a playlist with that user!"); Console.ReadKey(true); return; } PlaylistsRepository playlistsRepo = new PlaylistsRepository(Constants.PlaylistsPath); UsersPlaylistsRepository usersPlaylistsRepo = new UsersPlaylistsRepository(Constants.UsersPlaylistsPath); List <UsersPlaylists> usersPlaylistsEntities = usersPlaylistsRepo.GetAll(upe => upe.UserId == currentUserId); List <Playlist> playlists = new List <Playlist>(); foreach (UsersPlaylists usersPlaylistsEntity in usersPlaylistsEntities) { Playlist playlist = playlistsRepo.GetAll(p => p.Id == usersPlaylistsEntity.PlaylistId).FirstOrDefault(); playlists.Add(playlist); } Console.Clear(); foreach (Playlist playlist in playlists) { Console.WriteLine("****************************"); Console.WriteLine("Id: {0}", playlist.Id); Console.WriteLine("Name: {0}", playlist.Name); Console.WriteLine("Description: {0}", playlist.Description); Console.WriteLine("****************************"); } Console.WriteLine(); Console.Write("Enter id of playlist to share: "); isIntId = false; int playlistShareId = 0; isIntId = int.TryParse(Console.ReadLine(), out playlistShareId); while (isIntId == false) { Console.WriteLine("Id can only be an integer number. Try again!!"); Console.ReadKey(true); Console.Write("Enter id of playlist to share: "); isIntId = int.TryParse(Console.ReadLine(), out playlistShareId); } bool isExistingPlaylist = usersPlaylistsRepo.EntityExists(upe => upe.PlaylistId == playlistShareId && upe.UserId == currentUserId); if (isExistingPlaylist == false) { Console.WriteLine("No playlist with id {0} exists or you have no rights to share it!"); Console.ReadKey(true); return; } UsersPlaylists newUsersPlaylistsEntity = new UsersPlaylists(); //TODO: Enforce sharing restrictions with checks. newUsersPlaylistsEntity.PlaylistId = playlistShareId; newUsersPlaylistsEntity.UserId = userId; usersPlaylistsRepo.Save(newUsersPlaylistsEntity); Console.WriteLine("Playlist shared successfully!"); Console.ReadKey(true); }