예제 #1
0
        public UserReviewDTO AddReview(int userId, int bookId, BookReviewViewModel newReview)
        {
            var user = _userRepo.getUserById(userId);
            var book = _bookRepo.getBookById(bookId);

            if (user == null)
            {
                throw new UserNotFoundException();
            }
            if (book == null)
            {
                throw new BookNotFoundException();
            }
            try
            {
                GetUserReview(userId, bookId);
                throw new AlreadyReviewedByUserException();
            }
            catch (ReviewNotFoundException e)
            {
                // Good thing, there is no review :p!
            }

            var review = new BookReview
            {
                UserId       = userId,
                BookId       = bookId,
                DateOfReview = DateTime.Now,
                Rating       = newReview.Rating,
                Review       = newReview.Review
            };

            _bookRepo.SetBookRating(review.Rating, bookId);

            var result = _repo.AddUserReview(review);

            return(new UserReviewDTO
            {
                BookReviewId = result.BookReviewId,
                BookTitle = book.Title,
                DateOfReview = result.DateOfReview,
                Rating = result.Rating,
                Review = result.Review
            });
        }