Пример #1
0
        public async Task <IActionResult> Create(BookDTO book)
        {
            if (ValidateProperties(book))
            {
                return(View());
            }

            if (bookServices.FindBook(book.Title) != null)
            {
                ViewData.Add("RepeatingTitle", "Book already exists!");

                return(View());
            }

            string fullName = NameRefactorer
                              .GetFullName(book.FirstName, null, book.LastName);

            Author author    = authorServices.FindAuthor(fullName);
            Book   bookToAdd = null;

            try
            {
                if (author == null)
                {
                    author = new Author
                    {
                        FirstName = book.FirstName,
                        LastName  = book.LastName
                    };

                    authorServices.AddAuthor(author);
                }

                bookToAdd = new Book
                {
                    Title    = book.Title,
                    Genre    = book.Genre,
                    AuthorId = author.Id
                };
            }
            catch (ArgumentException ae)
            {
                ViewData.Add("ShortName", ae.Message);

                return(View());
            }

            bookServices.AddBook(bookToAdd);
            await AddBookToUser(bookToAdd.Id);

            return(RedirectToAction(nameof(Books)));
        }
Пример #2
0
        public string Execute(IEnumerable <string> parameters)
        {
            IList <string> args = parameters.ToList();

            if (args.Count != 4)
            {
                throw new InvalidBookServiceParametersExeption("Invalid numbers of parameters");
            }

            string title       = args[0];
            string genre       = args[1];
            string author      = args[2];
            string bookInStore = args[3];

            var newGenre  = genreServices.AddGenre(genre);
            var newAuthor = authorServices.AddAuthor(author);

            var addedBook = booksServices.AddBook(title, newGenre, newAuthor, bookInStore);

            return($"New book {addedBook.Title} was added.");
        }