public IEnumerable <ReviewStatisticsViewModel> GetRatings(IEnumerable <string> productCodes) { //ResultPage<Composite<RatingStatistics, Review>> statistics = null; ResultPage <RatingStatistics> statistics = null; var statisticsCriteria = new Criteria <RatingStatisticsFilter> { Filter = new RatingStatisticsFilter { Targets = productCodes.Select(x => Reference.Create($"product://{x}")) }, PageInfo = new PageInfo { PageSize = productCodes.Count() } }; try { statistics = _ratingStatisticsService.Get(statisticsCriteria); if (!statistics.Results.Any()) { return(new List <ReviewStatisticsViewModel>()); } } catch (SocialAuthenticationException ex) { throw new SocialRepositoryException("The application failed to authenticate with Episerver Social.", ex); } catch (MaximumDataSizeExceededException ex) { throw new SocialRepositoryException( "The application request was deemed too large for Episerver Social.", ex); } catch (SocialCommunicationException ex) { throw new SocialRepositoryException("The application failed to communicate with Episerver Social.", ex); } catch (SocialException ex) { throw new SocialRepositoryException("Episerver Social failed to process the application request.", ex); } return(statistics.Results.Select(x => ViewModelAdapter.Adapt(x))); }
/// <summary> /// Gets the rating statistics, if any, from the Episerver Social rating /// repository for the specified target reference. /// </summary> /// <param name="target">The target reference by which to filter ratings statistics</param> /// <returns>The rating statistics if any exist, null otherwise.</returns> /// <exception cref="SocialRepositoryException"> /// Thrown when errors occur communicating with /// the Social cloud services. /// </exception> public PageRatingStatistics GetRatingStatistics(string target) { PageRatingStatistics result = null; try { var ratingStatisticsPage = _ratingStatisticsService.Get(new Criteria <RatingStatisticsFilter> { Filter = new RatingStatisticsFilter { Targets = new List <Reference> { Reference.Create(target) } }, PageInfo = new PageInfo { PageSize = 1 } }); if (ratingStatisticsPage.Results.Any()) { var statistics = ratingStatisticsPage.Results.ToList().FirstOrDefault(); if (statistics.TotalCount > 0) { result = new PageRatingStatistics { Average = (double)statistics.Sum / statistics.TotalCount, TotalCount = statistics.TotalCount }; } } } catch (SocialAuthenticationException ex) { throw new SocialRepositoryException("The application failed to authenticate with Episerver Social.", ex); } catch (MaximumDataSizeExceededException ex) { throw new SocialRepositoryException( "The application request was deemed too large for Episerver Social.", ex); } catch (SocialCommunicationException ex) { throw new SocialRepositoryException("The application failed to communicate with Episerver Social.", ex); } catch (SocialException ex) { throw new SocialRepositoryException("Episerver Social failed to process the application request.", ex); } return(result); }
/// <summary> /// Render the Like button block frontend view. /// </summary> /// <param name="currentBlock">The current block instance.</param> /// <returns>Result of the redirect to the current page.</returns> public override ActionResult Index(LikeButtonBlock currentBlock) { var pageLink = _pageRouteHelper.PageLink; var targetPageRef = Reference.Create(PermanentLinkUtility.FindGuid(pageLink).ToString()); var anonymousUser = User.Identity.GetUserId() == null ? true : false; // Create a rating block view model to fill the frontend block view var blockModel = new LikeButtonBlockViewModel(currentBlock) { Link = pageLink }; try { // Using the Episerver Social Rating service, get the existing rating for the current // user (rater) and page (target). This is done only if there's a user identity. Anonymous // users will never match a previously submitted anonymous Like rating as they are always // uniquely generated. if (!anonymousUser) { var raterUserRef = GetRaterRef(); var ratingPage = _ratingService.Get( new Criteria <RatingFilter> { Filter = new RatingFilter { Rater = raterUserRef, Targets = new List <Reference> { targetPageRef } }, PageInfo = new PageInfo { PageSize = 1 } } ); // Add the current Like rating, if any, to the block view model. If the user is logged // into the site and had previously liked the current page then the CurrentRating value // should be 1 (LIKED_RATING). Anonymous user Likes are generated with unique random users // and thus the current anonymous user will never see a current rating value as he/she // can Like the page indefinitely. if (ratingPage.Results.Count() > 0) { blockModel.CurrentRating = ratingPage.Results.ToList().FirstOrDefault().Value.Value; } } // Using the Episerver Social Rating service, get the existing Like statistics for the page (target) var ratingStatisticsPage = _ratingStatisticsService.Get( new Criteria <RatingStatisticsFilter> { Filter = new RatingStatisticsFilter { Targets = new List <Reference> { targetPageRef } }, PageInfo = new PageInfo { PageSize = 1 } } ); // Add the page Like statistics to the block view model if (ratingStatisticsPage.Results.Count() > 0) { var statistics = ratingStatisticsPage.Results.ToList().FirstOrDefault(); if (statistics.TotalCount > 0) { blockModel.TotalCount = statistics.TotalCount; } } } catch (Exception) { // The rating service may throw a number of possible exceptions // should handle each one accordingly -- see rating service documentation } return(PartialView("~/Features/Blocks/Views/LikeButtonBlock.cshtml", blockModel)); }