Beispiel #1
0
        public IActionResult GetAuthorById(int authorId)
        {
            var author = _authorRepository.GetAuthorById(authorId);

            if (author == null)
            {
                ModelState.AddModelError("", "Some kind of error getting author");
                ViewBag.Message = $"There was a problem retrieving author with id {authorId} " +
                                  $"From the database or no author with that id exists";
                author = new AuthorDto();
            }

            var country = _countryRepository.GetCountryOfAnAuthor(authorId);

            if (country == null)
            {
                ModelState.AddModelError("", "Some kind of error getting country");
                ViewBag.Message += $"There was a problem retrieving country with id {authorId} " +
                                   $"From the database or no country with that id exists";
                country = new CountryDto();
            }

            var bookCategories = new Dictionary <BookDto, IEnumerable <CategoryDto> >();
            var books          = _authorRepository.GetBooksByAuthor(authorId);

            if (books.Count() <= 0)
            {
                ViewBag.BookMessage = $"No books for {author.FirstName} {author.LastName} exists";
            }

            foreach (var book in books)
            {
                var categories = _categoryRepository.GetAllCategoriesOfABook(book.Id);
                bookCategories.Add(book, categories);
            }

            var authorCountryBooksCategoriesViewModel = new AuthorCountryBooksCategoriesViewModel
            {
                Author         = author,
                Country        = country,
                BookCategories = bookCategories
            };

            ViewBag.SuccessMessage = TempData["SuccessMessage"];
            return(View(authorCountryBooksCategoriesViewModel));
        }
        public IActionResult GetAuthorById(int authorId)
        {
            var author = _authorRespository.GetAuthorById(authorId);

            if (author == null)
            {
                ModelState.AddModelError("", $"There is no author with id:{authorId}");
                ViewBag.Message = $"There was a problem retrieving author with id {authorId} " +
                                  $"from the database or no author with this id exists";
                author = new AuthorDto();
            }

            var country = _countryRepository.GetCountryOfAnAuthor(authorId);

            if (country == null)
            {
                ModelState.AddModelError("", "Error getting a country");
                ViewBag.Message += $"There was a problem retrieving country of an author with Id={authorId} " +
                                   $"from the database or no country exists for this author";
                country = new CountryDto();
            }

            var books = _authorRespository.GetBooksByAuthor(authorId);
            IDictionary <BookDto, IEnumerable <CategoryDto> > bookCategories = new Dictionary <BookDto, IEnumerable <CategoryDto> >();

            if (books.Count() <= 0)
            {
                ViewBag.BookMessage = $"No books for {author.FirstName} {author.LastName} exists.";
            }
            foreach (var book in books)
            {
                var categories = _categoryRepository.GetAllCategoriesOfABook(book.Id);
                bookCategories.Add(book, categories);
            }

            var ACBCVM = new AuthorCountryBookCategoriesViewModel
            {
                Author         = author,
                Country        = country,
                BookCategories = bookCategories
            };

            return(View(ACBCVM));
        }