Ejemplo n.º 1
0
        public async Task <UpdateRatingResponse> RateMovie(UserRatingUpdateRequest userRatingUpdateRequest)
        {
            var updateRatingResponse = new UpdateRatingResponse
            {
                MovieId = userRatingUpdateRequest.MovieId
            };

            var movieToUpdate = await _movieRepository.GetMovie(userRatingUpdateRequest.MovieId);

            if (movieToUpdate == null)
            {
                updateRatingResponse.RatingUpdateStatus = RatingUpdateStatus.NotFound;
                return(updateRatingResponse);
            }

            var userRating =
                await _movieRepository.GetUserRating(userRatingUpdateRequest.MovieId, userRatingUpdateRequest.UserId);

            if (userRating != null)
            {
                userRating.Rating = userRatingUpdateRequest.Rating;
                await _movieRepository.UpdateMovieRating(userRating);

                updateRatingResponse.RatingUpdateStatus = RatingUpdateStatus.Updated;
            }
            else
            {
                await _movieRepository.AddMovieRating(userRatingUpdateRequest.MovieId, userRatingUpdateRequest.UserId,
                                                      userRatingUpdateRequest.Rating);

                updateRatingResponse.RatingUpdateStatus = RatingUpdateStatus.Added;
            }

            return(updateRatingResponse);
        }