public string Add(AuthorDTO dto)
        {
            if (string.IsNullOrEmpty(dto.FirstName))
            {
                throw new InvalidAuthorException("INVALID AUTHOR FIRSTNAME");
            }
            if (string.IsNullOrEmpty(dto.LastName))
            {
                throw new InvalidAuthorException(" INVALID AUTHOR LASTNAME");
            }

            Author author;
            var    context = new LibraryDBContext();

            author = context.Authors.SingleOrDefault(authorL => (authorL.FirstName + authorL.LastName) == (dto.FirstName + dto.LastName));
            if (author == null)
            {
                author           = new Author();
                author.FirstName = dto.FirstName;
                author.LastName  = dto.LastName;
                author.ID        = dto.generateID();
                context.Authors.Add(author);
                context.SaveChanges();
                return(author.ID);
            }
            else
            {
                return(author.ID);
            }
        }
        public bool Update(AuthorDTO dto)
        {
            var    context = new LibraryDBContext();
            Author author  = context.Authors.SingleOrDefault(authorL => (authorL.FirstName + authorL.LastName).Contains(dto.FirstName));

            if (author == null)
            {
                throw new InvalidPublisherException("AUTHOR NOT FOUND");
            }
            if (dto.FirstName != null)
            {
                author.FirstName = dto.FirstName;
            }
            if (dto.LastName != null)
            {
                author.LastName = dto.LastName;
            }
            context.SaveChanges();

            return(true);
        }
Esempio n. 3
0
        /// <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();
            BookRepositoryDTO bookRepoDTO  = new BookRepositoryDTO();

            Console.Write("\nEnter Title : ");
            bookDTO.Title = Console.ReadLine();
            while (true)
            {
                Console.WriteLine("1.Science&Technology  2.Romance  3.Literature  4.Biography  5.History");
                Console.Write("\nSelect GenreType : ");
                int genre = 0;
                int.TryParse(Console.ReadLine(), out genre);
                if (genre == 0 || genre < 0 || genre > 5)
                {
                    Console.WriteLine("Please enter correct value");
                    continue;
                }
                else
                {
                    switch (genre)
                    {
                    case 1: bookDTO.GenreType = "Science&Technology";
                        break;

                    case 2: bookDTO.GenreType = "Romance";
                        break;

                    case 3: bookDTO.GenreType = "Literature";
                        break;

                    case 4: bookDTO.GenreType = "Biography";
                        break;

                    case 5: bookDTO.GenreType = "History";
                        break;

                    default: Console.WriteLine("Enter correct value");
                        break;
                    }
                    break;
                }
            }
            while (true)
            {
                Console.Write("\nEnter Author FirstName :");
                authorDTO.FirstName = Console.ReadLine();
                Console.Write("\nEnter Author LastName :");
                authorDTO.LastName = Console.ReadLine();
                bookDTO.AuthorDTOlist.Add(authorDTO);
                //Author author = authormanager.Get(authorDTO.FirstName);
                //if (author == null)
                //{
                //    bookDTO.AuthorIDlist.Add(authormanager.Add(authorDTO));
                //}
                //else
                //    bookDTO.AuthorIDlist.Add(author.ID);
                Console.Write("\n Do you want to enter more authors Yes/No : ");
                if (string.Equals("No", Console.ReadLine(), StringComparison.CurrentCultureIgnoreCase))
                {
                    break;
                }
            }

            Console.Write("\n Enter Publisher Name : ");
            publisherDTO.Name = Console.ReadLine();
            Console.Write("\n Enter Publisher MoblieNumber : ");
            publisherDTO.ContactNumber = long.Parse(Console.ReadLine());
            bookDTO.publisherDTO       = publisherDTO;
            //Publisher publisher = publishermanager.Get(publisherDTO.Name);
            //if (publisher == null)
            //{
            //    bookDTO.publisherID = publishermanager.Add(publisherDTO);
            //}
            //else
            //    bookDTO.publisherID = publisher.ID;
            Console.Write("\n Enter Edition : ");
            bookRepoDTO.Edition = Console.ReadLine();
            Console.Write("\n Enter Price : ");
            bookRepoDTO.Price = Double.Parse(Console.ReadLine());
            Console.Write("\n Enter Number of Copies : ");
            int numberofbooks = 0;

            int.TryParse(Console.ReadLine(), out numberofbooks);
            bookRepoDTO.NumberOfCopies = numberofbooks;
            bookDTO.BookRepositoryDTO  = bookRepoDTO;

            if (bookmanager.AddBook(bookDTO) != null)
            {
                Console.WriteLine("BOOK ADDED SUCCESSFULLY");
            }
            else
            {
                Console.WriteLine("SOMETHING WENT WRONG IN ADDING BOOK");
            }
        }