示例#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)));
        }
        public IActionResult Details(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction(nameof(Authors)));
            }

            Author author = authorServices.FindAuthor(id);

            FullAuthorView result = GetDetails(author);

            if (result == null)
            {
                return(RedirectToAction(nameof(Authors)));
            }

            return(View(result));
        }