public IActionResult CreateReview(int bookId, int ReviewerId, Review review)
        {
            using (var client = new HttpClient())
            {
                var reviewerDto = _reviewerRepository.GetReviewerById(ReviewerId);
                var bookDto     = _bookRepository.GetBookById(bookId);

                if (reviewerDto == null || bookDto == null || review == null)
                {
                    ModelState.AddModelError("", "Invalid book, reviewer, or review. Cannot create review!");
                    ViewBag.BookId    = bookId;
                    ViewBag.BookTitle = bookDto == null ? "" : bookDto.Title;
                    return(View(review));
                }

                review.Reviewer = new Reviewer
                {
                    Id        = reviewerDto.Id,
                    FirstName = reviewerDto.FirstName,
                    LastName  = reviewerDto.LastName
                };

                review.Book = new Book
                {
                    Id    = bookDto.Id,
                    Isbn  = bookDto.Isbn,
                    Title = bookDto.Title
                };

                client.BaseAddress = new Uri("http://localhost:44373/api/");
                var responseTask = client.PostAsJsonAsync("reviews", review);
                responseTask.Wait();

                var result = responseTask.Result;

                if (result.IsSuccessStatusCode)
                {
                    var newReviewTask = result.Content.ReadAsAsync <Review>();
                    newReviewTask.Wait();

                    var newReview = newReviewTask.Result;
                    TempData["SuccessMessage"] = $"Review for {review.Book.Title} was successfully created.";
                    return(RedirectToAction("GetReviewById", new { reviewId = newReview.Id }));
                }

                ModelState.AddModelError("", "Review not created");
                ViewBag.BookId    = bookId;
                ViewBag.BookTitle = bookDto == null ? "" : bookDto.Title;
            }

            return(View(review));
        }
        //顯示該"評論者"詳細狀況,評論的"書籍"及"內容"
        public async Task <ActionResult> GetReviewerById(int reviewerId)
        {
            //欲顯示reviewer 以及其每則review跟所評論的Book
            var reviewer = await _reviewerRepository.GetReviewerById(reviewerId);

            if (reviewer == null)
            {
                ViewBag.Message = $"Did not have {reviewerId}'s Id reviewer";
                ModelState.AddModelError("", "Some Error getting reviewer");
                reviewer = new ReviewerDto();
            }
            IDictionary <ReviewDto, BookDto> reviewAndBook = new Dictionary <ReviewDto, BookDto>();

            var reviews = await _reviewerRepository.GetReviewsByAReviewer(reviewer.Id);

            foreach (var review in reviews)
            {
                var book = await _reviewRepository.GetBookForAReview(review.Id);

                reviewAndBook.Add(review, book);
            }

            var reviewerDetail = new ReviewerDetailViewModel
            {
                Reviewer      = reviewer,
                ReviewAndBook = reviewAndBook
            };

            return(View(reviewerDetail));
        }
        public IActionResult GetReviewerById(int reviewerId)
        {
            var reviewer = _reviewerRepository.GetReviewerById(reviewerId);

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

            var reviews = _reviewerRepository.GetReviewsByReviewer(reviewerId);

            if (reviews.Count() <= 0)
            {
                ViewBag.ReviewMessage = $"Reviewer {reviewer.FirstName} {reviewer.LastName} has no reviews";
            }

            IDictionary <ReviewDto, BookDto> reviewAndBook = new Dictionary <ReviewDto, BookDto>();

            foreach (var review in reviews)
            {
                var book = _reviewRepository.GetBookOfAReview(review.Id);
                reviewAndBook.Add(review, book);
            }

            var reviewerReviewsBooksViewModel = new ReviewerReviewsBooksViewModel()
            {
                Reviewer   = reviewer,
                ReviewBook = reviewAndBook
            };

            ViewBag.SuccessMessage = TempData["SuccessMessage"];
            return(View(reviewerReviewsBooksViewModel));
        }
        public IActionResult GetReviewerById(int reviewerId)
        {
            var reviewer = _reviewerRepository.GetReviewerById(reviewerId);

            if (reviewer == null)
            {
                ModelState.AddModelError("", "Error getting a reviewer");
                ViewBag.Reviewer = $"There was a problem retrieving reviewer with Id={reviewerId} " +
                                   $"from the database or no reviewer with this id exists";
                reviewer = new ReviewerDto();
            }

            var reviews = _reviewerRepository.GetReviewsByAReviewer(reviewerId);

            if (reviews.Count() <= 0)
            {
                ViewBag.ReviewsMessage = $"Reviewer {reviewer.FirstName} {reviewer.LastName} " +
                                         $"has no any reviews at this time, please come back later";
            }

            IDictionary <ReviewDto, BookDto> reviewAndBook = new Dictionary <ReviewDto, BookDto>();

            foreach (var review in reviews)
            {
                var book = _reviewRepository.GetBookOfAReview(review.Id);
                reviewAndBook.Add(review, book);
            }

            var reviewerReviewsBooksViewModel = new ReviewerReviewsBooksViewModel()
            {
                Reviewer   = reviewer,
                ReviewBook = reviewAndBook
            };

            return(View(reviewerReviewsBooksViewModel));
        }