Example #1
0
        public async Task <int> SaveMovieRatingAsync(MovieRatingDTO movieRatingDTO)
        {
            var rating = await _dbContext.MoviesRating
                         .FirstOrDefaultAsync(mr => mr.MovieId == movieRatingDTO.MovieId && mr.UserId == movieRatingDTO.UserId);

            if (rating != null)
            {
                rating.Rate = movieRatingDTO.Rate;
            }
            else
            {
                rating = new MoviesRating
                {
                    MovieId = movieRatingDTO.MovieId,
                    UserId  = movieRatingDTO.UserId,
                    Rate    = movieRatingDTO.Rate
                };

                await _dbContext.MoviesRating.AddAsync(rating);
            }

            try
            {
                await _dbContext.SaveChangesAsync();

                _logger.LogInfo($"Successfully saved MoviesRating with rating {movieRatingDTO.Rate} from user {movieRatingDTO.UserId} to movie {movieRatingDTO.MovieId}");
                return(1);
            }
            catch (Exception ex)
            {
                _logger.LogWarn($"Failed to save MoviesRating with rating {movieRatingDTO.Rate} from user {movieRatingDTO.UserId} to movie {movieRatingDTO.MovieId}. Exception: {ex}");
                return(-1);
            }
        }
Example #2
0
        private void AddRottenDetails(ref MovieRatingDTO rating)
        {
            var url = string.Format("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=9txsnh3qkb5ufnphhqv5tv5z&q={0}&page_limit=1", HttpUtility.UrlEncode(rating.MovieName));

            using (var client = new WebClient())
            {
                var json = client.DownloadString(url);
            }
        }
Example #3
0
        public async Task <ActionResult> SaveUserMovieRating([FromBody] MovieRatingDTO movieRatingDTO)
        {
            var result = await _moviesService.SaveUserMovieRatingAsync(movieRatingDTO);

            if (result == -1)
            {
                return(BadRequest(ModelState));
            }

            return(NoContent());
        }
Example #4
0
        // POST api/ratings
        public HttpResponseMessage Post([FromBody] MovieRatingDTO rating)
        {
            if (rating.UseRottenRatingOverride.HasValue)
            {
                return(PersistMovieRating(new MovieRating()
                {
                    MovieName = rating.MovieName,
                    RottenRating = rating.RottenRating.Value,
                    RottenSynopsis = rating.RottenSynopsis,
                    User = db.Users.FirstOrDefault(x => x.Username == rating.Username), // <= Odd implementation. Assuming the UI isn't connected to "Users" even though it normally would be.
                    UserRating = rating.UseRottenRatingOverride.Value ? rating.RottenRating.Value : rating.UserRating,
                    UserSummary = rating.UserSummary
                }));
            }
            else
            {
                if (string.IsNullOrWhiteSpace(rating.MovieName))
                {
                    return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                    {
                        ReasonPhrase = "Invalid movie name."
                    });
                }

                AddRottenDetails(ref rating);

                /* Modify one of the rating systems to conform to the other. RT uses scores but doesn't provide it in their API. They
                 * provide access to their "tomatometer" percentages though. Research will need to be done to see if they are comparable. */

                //if (rating.UserRating < rating.RottenRating.Value - 0.3 || ratin.UserRating > rottenRating.Value + 0.3)

                //Return the rating object back to the user in the response with the new Rotten Tomatoes data.
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = "Would user like to replace their rating with the available Rotten Tomatoes rating?"
                });
            }

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }
Example #5
0
        public async Task <int> SaveUserMovieRatingAsync(MovieRatingDTO movieRatingDTO)
        {
            movieRatingDTO.UserId = await _accountsService.GetCurrentUserIdAsync();

            return(await _ratingService.SaveMovieRatingAsync(movieRatingDTO));
        }