Exemple #1
0
        protected virtual object CreateModelPart(ProductReviewHelpfulness part, MessageContext messageContext)
        {
            Guard.NotNull(messageContext, nameof(messageContext));
            Guard.NotNull(part, nameof(part));

            var m = new Dictionary <string, object>
            {
                { "ProductReviewId", part.ProductReviewId },
                { "ReviewTitle", part.ProductReview.Title },
                { "WasHelpful", part.WasHelpful }
            };

            ApplyCustomerContentPart(m, part, messageContext);

            PublishModelPartCreatedEvent <ProductReviewHelpfulness>(part, m);

            return(m);
        }
Exemple #2
0
        public void Can_get_customer_content_by_type()
        {
            var customer = SaveAndLoadEntity <Customer>(GetTestCustomer(), false);
            var product  = SaveAndLoadEntity <Product>(GetTestProduct(), false);

            var productReview = new ProductReview
            {
                Customer     = customer,
                Product      = product,
                Title        = "Test",
                ReviewText   = "A review",
                IpAddress    = "192.168.1.1",
                IsApproved   = true,
                CreatedOnUtc = new DateTime(2010, 01, 01),
                UpdatedOnUtc = new DateTime(2010, 01, 02),
            };

            var productReviewHelpfulness = new ProductReviewHelpfulness
            {
                Customer      = customer,
                ProductReview = productReview,
                WasHelpful    = true,
                IpAddress     = "192.168.1.1",
                IsApproved    = true,
                CreatedOnUtc  = new DateTime(2010, 01, 03),
                UpdatedOnUtc  = new DateTime(2010, 01, 04)
            };

            var blogComment = new BlogComment
            {
                Customer     = customer,
                IpAddress    = "192.168.1.1",
                IsApproved   = true,
                CreatedOnUtc = new DateTime(2010, 01, 03),
                UpdatedOnUtc = new DateTime(2010, 01, 04),
                BlogPost     = new BlogPost()
                {
                    Title         = "Title 1",
                    Body          = "Body 1",
                    AllowComments = true,
                    CreatedOnUtc  = new DateTime(2010, 01, 01),
                    Language      = new Language()
                    {
                        Name            = "English",
                        LanguageCulture = "en-Us",
                    }
                }
            };

            context.Set <CustomerContent>().Add(productReview);
            context.Set <CustomerContent>().Add(productReviewHelpfulness);
            context.Set <CustomerContent>().Add(blogComment);

            context.SaveChanges();

            context.Dispose();
            context = new SmartObjectContext(GetTestDbName());

            var query = context.Set <CustomerContent>();

            query.ToList().Count.ShouldEqual(3);

            var dbReviews = query.OfType <ProductReview>().ToList();

            dbReviews.Count().ShouldEqual(1);
            dbReviews.First().ReviewText.ShouldEqual("A review");

            var dbHelpfulnessRecords = query.OfType <ProductReviewHelpfulness>().ToList();

            dbHelpfulnessRecords.Count().ShouldEqual(1);
            dbHelpfulnessRecords.First().WasHelpful.ShouldEqual(true);

            var dbBlogCommentRecords = query.OfType <BlogComment>().ToList();

            dbBlogCommentRecords.Count().ShouldEqual(1);
        }
Exemple #3
0
        public virtual async Task <IActionResult> SetProductReviewHelpfulness(string productReviewId, string productId, bool washelpful,
                                                                              [FromServices] ICustomerService customerService, [FromServices] IProductReviewService productReviewService)
        {
            var product = await _productService.GetProductById(productId);

            var productReview = await productReviewService.GetProductReviewById(productReviewId);

            if (productReview == null)
            {
                throw new ArgumentException("No product review found with the specified id");
            }

            if (_workContext.CurrentCustomer.IsGuest() && !_catalogSettings.AllowAnonymousUsersToReviewProduct)
            {
                return(Json(new
                {
                    Result = _localizationService.GetResource("Reviews.Helpfulness.OnlyRegistered"),
                    TotalYes = productReview.HelpfulYesTotal,
                    TotalNo = productReview.HelpfulNoTotal
                }));
            }

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

            //delete previous helpfulness
            var prh = productReview.ProductReviewHelpfulnessEntries
                      .FirstOrDefault(x => x.CustomerId == _workContext.CurrentCustomer.Id);

            if (prh != null)
            {
                //existing one
                prh.WasHelpful = washelpful;
            }
            else
            {
                //insert new helpfulness
                prh = new ProductReviewHelpfulness {
                    ProductReviewId = productReview.Id,
                    CustomerId      = _workContext.CurrentCustomer.Id,
                    WasHelpful      = washelpful,
                };
                productReview.ProductReviewHelpfulnessEntries.Add(prh);
                await productReviewService.UpdateProductReview(productReview);

                if (!_workContext.CurrentCustomer.HasContributions)
                {
                    await customerService.UpdateContributions(_workContext.CurrentCustomer);
                }
            }

            //new totals
            productReview.HelpfulYesTotal = productReview.ProductReviewHelpfulnessEntries.Count(x => x.WasHelpful);
            productReview.HelpfulNoTotal  = productReview.ProductReviewHelpfulnessEntries.Count(x => !x.WasHelpful);
            await productReviewService.UpdateProductReview(productReview);

            return(Json(new
            {
                Result = _localizationService.GetResource("Reviews.Helpfulness.SuccessfullyVoted"),
                TotalYes = productReview.HelpfulYesTotal,
                TotalNo = productReview.HelpfulNoTotal
            }));
        }
Exemple #4
0
        public async Task <IActionResult> SetReviewHelpfulness(int productReviewId, bool washelpful)
        {
            // INFO: Entitity is being loaded tracked because it must be saved later.
            var productReview = await _db.ProductReviews.FindByIdAsync(productReviewId);

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

            var customer = Services.WorkContext.CurrentCustomer;

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

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

            var entriesQuery = _db.CustomerContent
                               .AsQueryable()
                               .OfType <ProductReviewHelpfulness>()
                               .Where(x => x.ProductReviewId == productReview.Id);

            // Delete previous helpfulness.
            var oldEntry = await entriesQuery.Where(x => x.CustomerId == customer.Id).FirstOrDefaultAsync();

            if (oldEntry != null)
            {
                _db.CustomerContent.Remove(oldEntry);
            }

            // Insert new helpfulness.
            var newEntry = new ProductReviewHelpfulness
            {
                ProductReviewId = productReview.Id,
                CustomerId      = customer.Id,
                IpAddress       = _webHelper.GetClientIpAddress().ToString(),
                WasHelpful      = washelpful,
                IsApproved      = true // Always approved
            };

            _db.CustomerContent.Add(newEntry);

            await _db.SaveChangesAsync();

            // New totals.
            int helpfulYesTotal = await entriesQuery.Where(x => x.WasHelpful).CountAsync();

            int helpfulNoTotal = await entriesQuery.Where(x => !x.WasHelpful).CountAsync();

            productReview.HelpfulYesTotal = helpfulYesTotal;
            productReview.HelpfulNoTotal  = helpfulNoTotal;

            await _db.SaveChangesAsync();

            return(Json(new
            {
                Success = true,
                Result = T("Reviews.Helpfulness.SuccessfullyVoted").Value,
                TotalYes = productReview.HelpfulYesTotal,
                TotalNo = productReview.HelpfulNoTotal
            }));
        }
        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
            }));
        }