Exemple #1
0
        public async Task <IActionResult> New(NewReviewModel model)
        {
            var movie = _movies.GetDetails(model.MovieId);

            if (movie == null)
            {
                return(RedirectToAction("Index", "Movies"));
            }

            // TODO: authorize the user to create reviews for the movie

            if (ModelState.IsValid)
            {
                var result = _reviews.Create(model.MovieId, model.Stars, model.Comment, "test");
                if (result.Succeeded)
                {
                    return(View("Success", new ReviewSuccessViewModel {
                        MovieId = model.MovieId, Action = "Created"
                    }));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }

            model.MovieTitle = movie.Title;

            return(View(model));
        }
        public async Task <IActionResult> CreateReview([FromBody] NewReviewModel review)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var reviewCreated = await _reviewService.CreateReviewAsync(review.TermId, review.Grade, review.Description);

            return(CreatedAtAction("GetReview", new { id = reviewCreated.ReviewId }, review));
        }
        public async Task <IActionResult> New(NewReviewModel model)
        {
            var result = await _movies.GetDetailsAsync(model.MovieId);

            if (result.IsAccessDenied)
            {
                return(Forbid());
            }
            if (!result.Succeeded)
            {
                return(Error(result.Errors));
            }

            var movie = result.Value;

            if (movie == null)
            {
                return(RedirectToAction("Index", "Movies"));
            }

            if (!movie.CanReview)
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                var create = await _reviews.CreateAsync(model.MovieId, model.Stars, model.Comment);

                if (create.IsAccessDenied)
                {
                    return(new ForbidResult());
                }

                if (create.Succeeded)
                {
                    return(View("Success", new ReviewSuccessViewModel {
                        MovieId = model.MovieId, Action = "Created"
                    }));
                }
                else
                {
                    foreach (var error in create.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }

            model.MovieTitle = movie.Title;

            return(View(model));
        }
Exemple #4
0
        public async Task <IActionResult> New(NewReviewModel model)
        {
            var movie = _movies.GetDetails(model.MovieId);

            if (movie == null)
            {
                return(RedirectToAction("Index", "Movies"));
            }

            // TODO: authorize the user to create reviews for the movie
            var authz = await _authorization.AuthorizeAsync(User, movie, Authorization.MovieOperations.Review);

            if (!authz.Succeeded)
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                var result = _reviews.Create(model.MovieId, model.Stars, model.Comment, User.FindFirst("sub").Value);
                if (result.Succeeded)
                {
                    return(View("Success", new ReviewSuccessViewModel {
                        MovieId = model.MovieId, Action = "Created"
                    }));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
            }

            model.MovieTitle = movie.Title;

            return(View(model));
        }