Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateFeedback([FromBody] MovieReviewViewModel review)
        {
            var hasSeen = _context.Watchhistory.Where(r => r.CustomerMailAddress == User.Identity.Name).FirstOrDefault(r => r.MovieId == review.MovieId);

            if (!ModelState.IsValid && hasSeen == null)
            {
                return(BadRequest(ModelState));
            }
            if (review.Review.Length < 10 || review.Review.Length > 500)
            {
                return(BadRequest("The length of your review is too long or too short (min 10 & max 500 characters"));
            }
            if (review.Rating < 1 || review.Rating > 10)
            {
                return(BadRequest("Rating must be between 1 and 10"));
            }

            var newReview = Mapper.Map <MovieReview>(review);

            newReview.CustomerMailAddress = User.Identity.Name;
            try{
                _repository.UpdateMovieReview(newReview);
                if (await _repository.SaveChangesAsync())
                {
                    return(Json($"/api/movies/{review.MovieId}/feedback"));
                }
            } catch (Exception e) {
                return(BadRequest(e.Message));
            }

            return(BadRequest("Failed to save changes to the database"));
        }