public async Task <bool> SetRating(BookRatingQueryParams ratingQueryParams)
        {
            var book = await _bookRepository.FindByIdAsync(ratingQueryParams.BookId);

            if (book == null)
            {
                return(false);
            }

            var bookRating = new BookRating(ratingQueryParams.BookId, ratingQueryParams.UserId, ratingQueryParams.Rating);

            _bookRatingRepository.Add(bookRating);
            await _bookRatingRepository.SaveChangesAsync();

            var avgRating = _bookRatingRepository.GetAll()
                            .Where(b => b.BookId == ratingQueryParams.BookId)
                            .Average(b => b.Rating);

            book.Rating = avgRating;
            _bookRepository.Update(book);
            await _bookRepository.SaveChangesAsync();

            return(true);
        }
Esempio n. 2
0
 public async Task <ActionResult <bool> > SetRating([FromQuery] BookRatingQueryParams ratingQueryParams)
 {
     return(await _bookService.SetRating(ratingQueryParams));
 }