public static void CreateNewBook(BookManagerDbContext data) { Console.Write("Title: "); string title = Console.ReadLine(); Console.WriteLine("Author:"); Console.Write("-First Name: "); string firstName = Console.ReadLine(); Console.Write("-Last Name: "); string lastName = Console.ReadLine(); Console.Write("-Year Born: "); DateTime born = DateTime.Parse(Console.ReadLine()); Console.Write("-Year Died: "); DateTime died = DateTime.Parse(Console.ReadLine()); Console.Write("Description: "); string description = Console.ReadLine(); Console.Write("Date Published: "); DateTime published = DateTime.Parse(Console.ReadLine()); Console.Write("Book Publisher: "); string publisher = Console.ReadLine(); List<Genre> genres = new List<Genre>(); Console.WriteLine("Select Book Genres:"); Console.WriteLine("1. Comedy"); Console.WriteLine("2. Action"); Console.WriteLine("3. Fantasy"); Console.Write("Enter your choises: "); string numbers = Console.ReadLine(); string[] numbersSplited = numbers.Split(' '); foreach (var number in numbersSplited) { SelectGenres(genres, int.Parse(number), data); } Console.Write("Pages: "); int pages = int.Parse(Console.ReadLine()); Console.Write("ISBN: "); string isbn = Console.ReadLine(); var hasAuthor = data.Authors.Any(a => a.FirstName == firstName && a.LastName == lastName && a.YearBorn == born && a.YearDied == died); if (!hasAuthor) { var newAuthor = new Author() { FirstName = firstName, LastName = lastName, YearBorn = born, YearDied = died }; var book = new Book() { Author = newAuthor, DatePublished = published, Description = description, Genres = genres, ISBN = isbn, Pages = pages, Title = title, Publisher = publisher, isLoan = false }; newAuthor.Books.Add(book); data.Authors.Add(newAuthor); data.SaveChanges(); } else { var author = data.Authors.Where(a => a.FirstName == firstName && a.LastName == lastName && a.YearBorn == born && a.YearDied == died).FirstOrDefault(); var book = new Book() { Author = author, DatePublished = published, Description = description, Genres = genres, ISBN = isbn, Pages = pages, Title = title, Publisher = publisher, isLoan = false }; author.Books.Add(book); data.SaveChanges(); } }
public static void CreateNewAuthor(BookManagerDbContext data) { Console.Write("First Name: "); string firstName = Console.ReadLine(); Console.Write("Last Name: "); string lastName = Console.ReadLine(); Console.Write("Year Born: "); DateTime born = DateTime.Parse(Console.ReadLine()); Console.Write("Year Died: "); DateTime died = died = DateTime.Parse(Console.ReadLine()); var hasAuthor = data.Authors.Any(a => a.FirstName == firstName && a.LastName == lastName && a.YearBorn == born && a.YearDied == died); if (!hasAuthor) { var newAuthor = new Author() { FirstName = firstName, LastName = lastName, YearBorn = born, YearDied = died }; data.Authors.Add(newAuthor); data.SaveChanges(); } else { Console.WriteLine("Author already exist!"); } }
public static void SelectGenres(List<Genre> genres, int number, BookManagerDbContext data) { switch (number) { case 1: { var genre = data.Genres.Where(g => g.Id == 1).FirstOrDefault(); genres.Add(genre); }; break; case 2: { var genre = data.Genres.Where(g => g.Id == 2).FirstOrDefault(); genres.Add(genre); }; break; case 3: { var genre = data.Genres.Where(g => g.Id == 3).FirstOrDefault(); genres.Add(genre); }; break; } }