Пример #1
0
        public ActionResult Submit(LikeButtonBlockViewModel likeButtonBlock)
        {
            var targetPageRef = Reference.Create(PermanentLinkUtility.FindGuid(likeButtonBlock.Link).ToString());
            var raterUserRef  = GetRaterRef();

            try
            {
                // Add the rating using the Episerver Social Rating service
                var addedRating = _ratingService.Add(
                    new Rating(
                        raterUserRef,
                        targetPageRef,
                        new RatingValue(LikedRating)
                        )
                    );
            }
            catch (Exception)
            {
                // The rating service may throw a number of possible exceptions
                // should handle each one accordingly -- see rating service documentation
            }

            return(Redirect(UrlResolver.Current.GetUrl(likeButtonBlock.Link)));
        }
Пример #2
0
        /// <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));
        }