public void AddSong() { string inputSongTitle; bool isEmptyName = false; do { Console.Clear(); Console.Write("Enter new song name: "); inputSongTitle = Console.ReadLine(); isEmptyName = (String.IsNullOrWhiteSpace(inputSongTitle)); if (isEmptyName == true) { Console.WriteLine("Song title cannot be empty. Try again!!"); Console.ReadKey(true); } }while (isEmptyName == true); short inputSongReleaseYear; bool isShortYear; bool isBeforeCurrentYear = false; do { Console.Write("Enter song's release year: "); isShortYear = short.TryParse(Console.ReadLine(), out inputSongReleaseYear); if (DateTime.Now.Year >= inputSongReleaseYear) { isBeforeCurrentYear = true; } if (isShortYear == false) { Console.WriteLine("Year can only be an integer number. Try again!!"); Console.ReadKey(true); } if (isBeforeCurrentYear == false) { Console.WriteLine("Year should not be after the current year."); } } while (isShortYear == false || isBeforeCurrentYear == false); Song newSong = new Song(); newSong.Title = inputSongTitle; newSong.Year = inputSongReleaseYear; SongsRepository songsRepo = new SongsRepository(Constants.SongsPath); if (songsRepo.EntityExists(s => s.Title == inputSongTitle && s.Year == inputSongReleaseYear)) { Console.WriteLine("The song already exists in the database!"); Console.ReadKey(true); } int songId = songsRepo.Save(newSong); ArtistsRepository artistsRepo = new ArtistsRepository(Constants.ArtistsPath); List <Artist> artists = artistsRepo.GetAll(); Console.Clear(); int artistId; if (artists.Count == 0) { artistId = ArtistsView.AddArtist(); } else { foreach (Artist artistEntity in artists) { Console.WriteLine("************************************"); Console.WriteLine("Id: {0}", artistEntity.Id); Console.WriteLine("Artist name: {0}", artistEntity.Name); Console.WriteLine("************************************"); } Console.WriteLine(); EnterArtistId: Console.Write("Enter new song's artist id (type \"0\" if not in the list): "); bool isInt = int.TryParse(Console.ReadLine(), out artistId); while (isInt == false) { Console.WriteLine("IDs can only be integer numbers. Try Again!!"); Console.ReadKey(true); goto EnterArtistId; } if (artistId == 0) { artistId = ArtistsView.AddArtist(); } } SongsArtists songArtistEntity = new SongsArtists(); songArtistEntity.SongId = songId; songArtistEntity.ArtistId = artistId; SongsArtistsRepository songsArtistsRepo = new SongsArtistsRepository(Constants.SongsArtistsPath); songsArtistsRepo.Save(songArtistEntity); Console.WriteLine("Song saved successfully!"); 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); }