예제 #1
0
        public ActionResult SetReviewHelpfulness(int productReviewId, bool washelpful)
        {
            var productReview = _customerContentService.GetCustomerContentById(productReviewId) as ProductReview;

            if (productReview == null)
            {
                throw new ArgumentException(T("Reviews.NotFound", productReviewId));
            }

            if (_services.WorkContext.CurrentCustomer.IsGuest() && !_catalogSettings.AllowAnonymousUsersToReviewProduct)
            {
                return(Json(new
                {
                    Success = false,
                    Result = T("Reviews.Helpfulness.OnlyRegistered").Text,
                    TotalYes = productReview.HelpfulYesTotal,
                    TotalNo = productReview.HelpfulNoTotal
                }));
            }

            //customers aren't allowed to vote for their own reviews
            if (productReview.CustomerId == _services.WorkContext.CurrentCustomer.Id)
            {
                return(Json(new
                {
                    Success = false,
                    Result = T("Reviews.Helpfulness.YourOwnReview").Text,
                    TotalYes = productReview.HelpfulYesTotal,
                    TotalNo = productReview.HelpfulNoTotal
                }));
            }

            // delete previous helpfulness
            var oldPrh = (from prh in productReview.ProductReviewHelpfulnessEntries
                          where prh.CustomerId == _services.WorkContext.CurrentCustomer.Id
                          select prh).FirstOrDefault();

            if (oldPrh != null)
            {
                _customerContentService.DeleteCustomerContent(oldPrh);
            }

            // insert new helpfulness
            var newPrh = new ProductReviewHelpfulness
            {
                ProductReviewId = productReview.Id,
                CustomerId      = _services.WorkContext.CurrentCustomer.Id,
                IpAddress       = _services.WebHelper.GetCurrentIpAddress(),
                WasHelpful      = washelpful,
                IsApproved      = true,            //always approved
            };

            _customerContentService.InsertCustomerContent(newPrh);

            // new totals
            int helpfulYesTotal = (from prh in productReview.ProductReviewHelpfulnessEntries
                                   where prh.WasHelpful
                                   select prh).Count();
            int helpfulNoTotal = (from prh in productReview.ProductReviewHelpfulnessEntries
                                  where !prh.WasHelpful
                                  select prh).Count();

            productReview.HelpfulYesTotal = helpfulYesTotal;
            productReview.HelpfulNoTotal  = helpfulNoTotal;
            _customerContentService.UpdateCustomerContent(productReview);

            return(Json(new
            {
                Success = true,
                Result = T("Reviews.Helpfulness.SuccessfullyVoted").Text,
                TotalYes = productReview.HelpfulYesTotal,
                TotalNo = productReview.HelpfulNoTotal
            }));
        }