public async Task <int> RateMovieAsync(int movieId, int userId, int rating) { if (rating < 1 || rating > 5) { throw new InvalidRatingException(rating); } var movieGetter = movieRepository.Get(movieId); var userGetter = userRepository.Get(userId); if (await movieGetter == null) { throw new MovieNotFoundException(movieId); } if (await userGetter == null) { throw new UserNotFoundException(userId); } var ratingSavedResult = await movieRatingRepository.UpsertAsync(new MovieRating { MovieId = movieId, UserId = userId, Rating = rating }); var ratings = await movieRatingRepository.GetByMovieId(movieId); var roundedAverageRating = Rounder.Round(ratings.Average(x => x.Rating)); return(await movieRepository.UpdateAverageRating(movieId, roundedAverageRating)); }