public string AddAuthor(AuthorDTO authorDTO) { if (string.IsNullOrEmpty(authorDTO.FirstName)) { throw new InvalidAuthorException("Invalid FirstName"); } if (string.IsNullOrEmpty(authorDTO.LastName)) { throw new InvalidAuthorException("Invalid LastName"); } Author author = new Author(); author.firstName = authorDTO.FirstName; author.lastName = authorDTO.LastName; author.AuthorID = authorDTO.generateID(); using (var context = new LibraryDBContext()) { context.Authors.Add(author); context.SaveChanges(); } return(author.AuthorID); }
/// <summary> /// This method reads the data of the book and sends the data to dto classes and call addbook method in bookmanager /// </summary> public static void AddBookOption() { BookDTO bookdto = new BookDTO(); AuthorDTO authorDTO = new AuthorDTO(); PublisherDTO publisherDTO = new PublisherDTO(); Console.Write("\nEnter the title of the book : "); bookdto.Title = Console.ReadLine(); Console.Write("\nEnter GenreType : "); bookdto.GenreType = Console.ReadLine(); while (true) { Console.Write("\nEnter Author firstName :"); authorDTO.FirstName = Console.ReadLine(); Console.Write("\nEnter Author lastName :"); authorDTO.LastName = Console.ReadLine(); Author author = authormanager.GetAuthorByName(authorDTO.FirstName); if (author == null) { bookdto.AuthorIDlist.Add(authormanager.AddAuthor(authorDTO)); } else { bookdto.AuthorIDlist.Add(author.AuthorID); } Console.Write("\n Do you want to enter more authors : "); if (string.Equals("No", Console.ReadLine(), StringComparison.CurrentCultureIgnoreCase)) { break; } } Console.Write("\n Enter name of the Publisher : "); publisherDTO.Name = Console.ReadLine(); Console.Write("\n Enter mobile number of the publisher : "); publisherDTO.ContactNumber = long.Parse(Console.ReadLine()); Publisher publisher = publishermanager.GetPublisher(publisherDTO.Name); if (publisher == null) { bookdto.publisherID = publishermanager.AddPublisher(publisherDTO); } else { bookdto.publisherID = publisher.PublisherID; } Console.Write("\n Enter edition of the Book : "); bookdto.Edition = Console.ReadLine(); Console.Write("\n Enter number of copies of the book : "); int numberofbooks = 0; int.TryParse(Console.ReadLine(), out numberofbooks); bookdto.NumberOfCopies = numberofbooks; if (bookmanager.AddBook(bookdto) != null) { Console.WriteLine("Book added successfully"); } else { Console.WriteLine("SOMETHING WENT WRONG IN ADDING BOOK"); } }