private void UpdateProductWithAverageReview(EntryContentBase product, string language) { EntryContentBase writableProduct = product.CreateWritableClone() as EntryContentBase; if (writableProduct != null && writableProduct.Property["AverageRating"] != null) { ReviewResult reviewResult = Get(product.ContentLink.ID); if (reviewResult != null) { writableProduct.Property["AverageRating"].Value = reviewResult.AverageReview; _contentRepository.Save(writableProduct, SaveAction.ForceCurrentVersion, AccessLevel.NoAccess); UpdateFindProductWithAverageReview(writableProduct.ContentLink.ID, language, reviewResult.AverageReview); } } }
//TODO: This should take in id, and type. Then it can be used on both commerce products and pagedata public ReviewResult Get(int id) { ReviewResult reviewResult = new ReviewResult(); reviewResult.AverageReview = 0; reviewResult.TotalNumberOfReviews = 0; reviewResult.Reviews = new List <ReviewData>(); ContentReference contentLink = _referenceConverter.GetContentLink(id, CatalogContentType.CatalogEntry, 0); var contentAssetFolder = _contentAssetHelper.GetAssetFolder(contentLink); if (contentAssetFolder == null) { return(reviewResult); } List <Review> reviews = new List <Review>(); if (Language == null) { reviews = _contentRepository.GetChildren <Review>(contentAssetFolder.ContentLink).ToList(); } else { reviews = _contentRepository.GetChildren <Review>(contentAssetFolder.ContentLink, new CultureInfo(Language)).ToList(); } if (reviews.Any()) { reviewResult.Reviews = reviews.Select(x => new ReviewData { ContentId = x.ContentId, Rating = x.Rating, Heading = x.Heading, Text = x.Text, UserName = x.UserDisplayName, ReviewDate = x.ReviewDate }).OrderByDescending(y => y.ReviewDate).ToList(); reviewResult.TotalNumberOfReviews = reviewResult.Reviews.Count; var average = reviewResult.Reviews.Average(x => x.Rating); average = average * 2; average = Math.Round(average, MidpointRounding.AwayFromZero); average = average / 2; reviewResult.AverageReview = average; } return(reviewResult); }