public ActionResult SetReviewHelpfulness(int articleReviewId, bool washelpful) { var article = _articleService.GetArticleById(articleReviewId); if (article == null || article.Deleted || article.StatusFormat != StatusFormat.Norma || !article.AllowUserReviews) { return(HttpNotFound()); } var articleReview = article.ArticleReviews.Where(pr => pr.IsApproved).OrderBy(pr => pr.CreatedOnUtc).FirstOrDefault() as ArticleReview; if (articleReview == null) { bool isApproved = !_catalogSettings.ArticleReviewsMustBeApproved; var user = _services.WorkContext.CurrentUser; //添加新的评论赞 articleReview = new ArticleReview() { ArticleId = article.Id, UserId = user.Id, IpAddress = _services.WebHelper.GetCurrentIpAddress(), Rating = 0, HelpfulYesTotal = 0, HelpfulNoTotal = 0, IsApproved = isApproved, CreatedOnUtc = DateTime.UtcNow, ModifiedOnUtc = DateTime.UtcNow, }; _userContentService.InsertUserContent(articleReview); } if (_services.WorkContext.CurrentUser.IsGuest() && !_catalogSettings.AllowAnonymousUsersToReviewArticle) { return(Json(new { Success = false, Result = T("Reviews.Helpfulness.OnlyRegistered").Text, TotalYes = articleReview.HelpfulYesTotal, TotalNo = articleReview.HelpfulNoTotal })); } //users aren't allowed to vote for their own reviews if (articleReview.UserId == _services.WorkContext.CurrentUser.Id) { return(Json(new { Success = false, Result = T("Reviews.Helpfulness.YourOwnReview").Text, TotalYes = articleReview.HelpfulYesTotal, TotalNo = articleReview.HelpfulNoTotal })); } //delete previous helpfulness var oldPrh = (from prh in articleReview.ArticleReviewHelpfulnessEntries where prh.UserId == _services.WorkContext.CurrentUser.Id select prh).FirstOrDefault(); if (oldPrh != null) { _userContentService.DeleteUserContent(oldPrh); } //insert new helpfulness var newPrh = new ArticleReviewHelpfulness() { ArticleReviewId = articleReview.Id, UserId = _services.WorkContext.CurrentUser.Id, IpAddress = _services.WebHelper.GetCurrentIpAddress(), WasHelpful = washelpful, IsApproved = true, //always approved CreatedOnUtc = DateTime.UtcNow, ModifiedOnUtc = DateTime.UtcNow, }; _userContentService.InsertUserContent(newPrh); //new totals int helpfulYesTotal = (from prh in articleReview.ArticleReviewHelpfulnessEntries where prh.WasHelpful select prh).Count(); int helpfulNoTotal = (from prh in articleReview.ArticleReviewHelpfulnessEntries where !prh.WasHelpful select prh).Count(); articleReview.HelpfulYesTotal = helpfulYesTotal; articleReview.HelpfulNoTotal = helpfulNoTotal; _userContentService.UpdateUserContent(articleReview); return(Json(new { Success = true, Result = T("Reviews.Helpfulness.SuccessfullyVoted").Text, TotalYes = articleReview.HelpfulYesTotal, TotalNo = articleReview.HelpfulNoTotal })); }
public virtual ActionResult SetArticleReviewHelpfulness(int articleReviewId, bool washelpful) { var articleReview = _articleService.GetArticleReviewById(articleReviewId); if (articleReview == null) { throw new ArgumentException("No article review found with the specified id"); } if (_workContext.CurrentCustomer.IsGuest() && !_catalogSettings.AllowAnonymousUsersToReviewArticle) { return(Json(new { Result = _localizationService.GetResource("Reviews.Helpfulness.OnlyRegistered"), TotalYes = articleReview.HelpfulYesTotal, TotalNo = articleReview.HelpfulNoTotal })); } //customers aren't allowed to vote for their own reviews if (articleReview.CustomerId == _workContext.CurrentCustomer.Id) { return(Json(new { Result = _localizationService.GetResource("Reviews.Helpfulness.YourOwnReview"), TotalYes = articleReview.HelpfulYesTotal, TotalNo = articleReview.HelpfulNoTotal })); } //delete previous helpfulness var prh = articleReview.ArticleReviewHelpfulnessEntries .FirstOrDefault(x => x.CustomerId == _workContext.CurrentCustomer.Id); if (prh != null) { //existing one prh.WasHelpful = washelpful; } else { //insert new helpfulness prh = new ArticleReviewHelpfulness { ArticleReviewId = articleReview.Id, CustomerId = _workContext.CurrentCustomer.Id, WasHelpful = washelpful, }; articleReview.ArticleReviewHelpfulnessEntries.Add(prh); } _articleService.UpdateArticle(articleReview.Article); //new totals articleReview.HelpfulYesTotal = articleReview.ArticleReviewHelpfulnessEntries.Count(x => x.WasHelpful); articleReview.HelpfulNoTotal = articleReview.ArticleReviewHelpfulnessEntries.Count(x => !x.WasHelpful); _articleService.UpdateArticle(articleReview.Article); return(Json(new { Result = _localizationService.GetResource("Reviews.Helpfulness.SuccessfullyVoted"), TotalYes = articleReview.HelpfulYesTotal, TotalNo = articleReview.HelpfulNoTotal })); }