Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, EditReviewInputModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest("Invalid input data."));
            }

            var reviewExists = await this.reviewService.Exists(id);

            if (!reviewExists)
            {
                return(this.BadRequest("Review does not exist."));
            }

            //var bookExists = await this.bookService.Exists(model.BookId);
            //if (!bookExists)
            //{
            //    ModelState.AddModelError(nameof(ReviewListingModel.BookId), "Book does not exist.");
            //    return View(model);
            //}

            await this.reviewService.Update(
                id,
                model.Title.Trim(),
                model.Description.Trim(),
                model.AuthorId,
                model.Author,
                model.BookId,
                model.BookName);

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task EditReview(EditReviewInputModel model)
        {
            var currentReview = this.reviewRepository.AllAsNoTracking().FirstOrDefault(x => x.Id == model.Id);

            currentReview.ReviewText = model.ReviewText;
            await this.reviewRepository.SaveChangesAsync();
        }
        public async Task <ActionResult <bool> > EditReview(EditReviewInputModel input)
        {
            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            await this.reviewsService.EditReviewAsync(input.ReviewNumber, userId, input.ReviewInfo);

            return(true);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> EditReview(EditReviewInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            await this.reviewService.EditReview(model);

            return(this.Redirect("/Reviews/MyReviews"));
        }
        public async Task <IActionResult> EditReview(EditReviewInputModel inputModel, string pageFragment, string commentsPage, string commentsOrderingOption)
        {
            var productId = inputModel.ProductId;

            var commentId = inputModel.CommentId;

            //Sanitize pageFragment
            pageFragment = this.javaScriptEncoder.Encode(pageFragment);

            //Sanitize commentsPage
            if (commentsPage != null)
            {
                commentsPage = this.htmlEncoder.Encode(commentsPage);
            }


            //Store input model for passing in get action
            TempData["InputModelFromPOSTRequest"]     = JsonSerializer.Serialize(inputModel);
            TempData["InputModelFromPOSTRequestType"] = nameof(EditReviewInputModel);

            //Check if data is valid without looking into the database
            if (this.ModelState.IsValid == false)
            {
                //Add suitable model state error for UI validation
                var newModelState = new ModelStateDictionary(this.ModelState);
                foreach (var modelStateEntry in this.ModelState.Values)
                {
                    foreach (var modelStateError in modelStateEntry.Errors)
                    {
                        newModelState.AddModelError($"CommentId_{inputModel.CommentCounter}", modelStateError.ErrorMessage);
                    }
                }

                //Store needed info for get request in TempData
                TempData["ErrorsFromPOSTRequest"] = ModelStateHelper.SerialiseModelState(newModelState);

                return(this.RedirectToAction("ProductPage", "Products", new { productId, commentsPage, commentsOrderingOption }, pageFragment));
            }

            var userId = this.userService.GetUserId(this.User.Identity.Name);

            var oldProductRating = new ProductRating();

            if (inputModel?.ProductRatingViewModel?.AverageRating > 0)
            {
                //Check if rating from this user for this product already exists
                if (this.productService.ProductRatingExists(userId, productId) == true)
                {
                    oldProductRating = this.productService.GetProductRating(userId, productId);
                }
                else
                {
                    this.ModelState.AddModelError($"Rating_{inputModel.CommentCounter}", "You have not given a review for this product!!!");
                }
            }
            else
            {
                oldProductRating = null;
            }


            var oldComment = new ProductComment();

            //Check if the comment exists and if it belongs to the current user and is for this product TODO
            if (this.productCommentService.CommentMatchesUserAndProduct(commentId, userId, productId) == false)
            {
                oldComment = null;
                this.ModelState.AddModelError($"CommentId_{inputModel.CommentCounter}", "You have not given a review with a comment for this product!!!");
            }
            else
            {
                oldComment = this.productCommentService.GetProductComment(commentId);
            }

            //Check if model state is valid after checking into the database
            if (this.ModelState.IsValid == false)
            {
                //Store needed info for get request in TempData only if the model state is invalid after doing the complex checks
                TempData["ErrorsFromPOSTRequest"] = ModelStateHelper.SerialiseModelState(this.ModelState);

                //Reload same page with the TempData
                return(this.RedirectToAction("ProductPage", "Products", new { productId, commentsPage, commentsOrderingOption }, pageFragment));
            }

            if (oldComment != null)
            {
                await this.productCommentService.EditCommentTextAsync(oldComment, inputModel.Text);
            }
            if (oldProductRating != null)
            {
                await this.productService.EditProductRating(oldProductRating, (double)inputModel.ProductRatingViewModel.AverageRating);
            }

            return(this.RedirectToAction("ProductPage", "Products", new { productId, toReplyComment = oldComment.Id, commentsPage, commentsOrderingOption }, pageFragment));
        }