private void AddGroup() { string groupName; Console.WriteLine("Enter group name: "); groupName = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(groupName)) { Band newBand = new Band(); newBand.Name = groupName; using (var context = new MusicContext()) { bands.Add(newBand); context.Bands.Add(newBand); context.SaveChanges(); } } else { Console.WriteLine("Empty group name!"); } }
private void AddSong() { string songName, songWordsString, songBandName; long songDuration; int songRating; List <string> songWords = new List <string>(); Console.WriteLine("Enter the name of the song: "); songName = Console.ReadLine(); if (!string.IsNullOrWhiteSpace(songName)) { if (bands.Count > 0) { Song newSong = new Song(); newSong.Name = songName; Console.WriteLine("Enter the length of the song(in seconds): "); if (long.TryParse(Console.ReadLine(), out songDuration)) { if (songDuration > 0) { newSong.DurationSeconds = songDuration; Console.WriteLine("Enter song rating: "); if (int.TryParse(Console.ReadLine(), out songRating)) { if (songRating >= MIN_RATING_SONG && songRating <= MAX_RATING_SONG) { newSong.Rating = songRating; Console.WriteLine("Add words: "); songWordsString = Console.ReadLine(); string[] words = songWordsString.Split(' '); foreach (var word in words) { songWords.Add(word); } newSong.Words = songWords; Console.WriteLine("Enter group name: "); foreach (var band in bands) { Console.WriteLine($"{band.Name}"); } songBandName = Console.ReadLine(); if (bands.Contains(bands.Where(band => band.Name.ToUpper() == songBandName.ToUpper()).FirstOrDefault())) { using (var context = new MusicContext()) { newSong.Band = context.Bands.Where(band => band.Name.ToUpper() == songBandName.ToUpper()).FirstOrDefault(); context.Songs.Add(newSong); context.SaveChanges(); } } else { Console.WriteLine("No such group!"); } } else { Console.WriteLine($"Rating must be between {MIN_RATING_SONG} and {MAX_RATING_SONG} inclusive"); } } else { Console.WriteLine("Wrong format!"); } } else { Console.WriteLine("The song can not last less than 1 second!"); } } else { Console.WriteLine("Wrong format!"); } } else { Console.WriteLine("First you need to add groups!"); } } else { Console.WriteLine("Empty song name!"); } }