public async Task AddStartForMovie(int stars, Movie movie, CancellationToken cancellationToken = default)
        {
            var rating = new Rating(stars);

            await _coreDbContext.Ratings.AddAsync(rating, cancellationToken);

            var ratingMovie = new RatingMovie(movie.Id, rating.Id);

            await _coreDbContext.RatingMovies.AddAsync(ratingMovie, cancellationToken);

            await _coreDbContext.SaveChangesAsync(cancellationToken);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a rating to a movie for a certain user
        /// The rating must be an integer between 1 and 5
        /// If the user already had a rating for that movie, the old rating should be updated to the new value
        /// </summary>
        /// <param name="userId">User Id</param>
        /// <param name="model">RatingMovie Model</param>
        /// <returns></returns>
        public bool PostRating(int userId, RatingMovie model)
        {
            var rating = ctx.Ratings.FirstOrDefault(x => x.MovieId == model.MovieId && x.UserId == userId);

            if (rating == null)
            {
                ctx.Ratings.Add(new Rating {
                    MovieId = model.MovieId, UserId = userId, Value = (int)model.Rating
                });
            }
            else
            {
                rating.Value = (int)model.Rating;
            }
            ctx.SaveChanges();

            return(true);
        }
Esempio n. 3
0
        public async Task <HttpResponseMessage> PostRating(int userId, RatingMovie model)
        {
            log.Debug(string.Format("PostRating({0}) - {1}", userId, JsonConvert.SerializeObject(model, FWA.Api.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None)));

            if (model == null)
            {
                return(this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "SearchCriteria Cannot be null"));
            }

            try
            {
                bool userExists  = movieRepository.UserExists(userId);
                bool movieExists = movieRepository.MovieExists(model.MovieId);

                if (!userExists || !movieExists)
                {
                    log.Debug(string.Format("PostRating()= User or Movie Not Exists {0}, {1}", userId, JsonConvert.SerializeObject(model, FWA.Api.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None)));
                    return(this.Request.CreateResponse(HttpStatusCode.NotFound, "User or Movie not found"));
                }

                bool posted = movieRepository.PostRating(userId, model);

                if (!posted)
                {
                    log.Debug(string.Format("PostRating()= NotFound{0}", JsonConvert.SerializeObject(model, FWA.Api.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None)));
                    return(this.Request.CreateResponse(HttpStatusCode.NotFound, "No movie is found based on the criteria"));
                }

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception ex)
            {
                log.Debug(string.Format("PostRating()= Exception{0}", JsonConvert.SerializeObject(ex, FWA.Api.Properties.Settings.Default.Tracing ? Formatting.Indented : Formatting.None)));
                return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }