Skip to content

Instantly share code, notes, and snippets.

@epishilpa
Created June 7, 2017 15:49
Show Gist options
  • Save epishilpa/94e49d0f927e149502fd738a1fd4bf2f to your computer and use it in GitHub Desktop.
Save epishilpa/94e49d0f927e149502fd738a1fd4bf2f to your computer and use it in GitHub Desktop.
public class ProductPageController : PageControllerBase<ProductPage>
{
private readonly IRatingService ratingService;
private readonly IRatingStatisticsService ratingStatisticsService;
public ProductPageController()
{
ratingService = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IRatingService>();
ratingStatisticsService = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IRatingStatisticsService>();
}
// Other controller code omitted for brevity ...
private Rating AddRatingForProduct(ProductPage currentPage, int ratingValue)
{
var productReference = Reference.Create(currentPage.ContentGuid.ToString());
Reference userReference = null;
if (this.User.Identity.IsAuthenticated)
{
userReference = Reference.Create(this.User.Identity.Name);
}
else
{
userReference = Reference.Create(System.Guid.NewGuid().ToString());
}
var rating = new Rating(userReference, productReference, new RatingValue(ratingValue));
return ratingService.Add(rating);
}
private Rating GetUserRatingForProduct(ProductPage currentPage)
{
Rating rating = null;
if (this.User.Identity.IsAuthenticated)
{
var productReference = Reference.Create(currentPage.ContentGuid.ToString());
var userReference = Reference.Create(this.User.Identity.Name);
var criteria = new Criteria<RatingFilter>
{
Filter = new RatingFilter
{
Targets = new List<Reference> { productReference },
Rater = userReference
},
PageInfo = new PageInfo { PageSize = 1 }
};
var ratingPage = ratingService.Get(criteria);
rating = ratingPage.Results.FirstOrDefault();
}
return rating;
}
private RatingStatistics GetAverageRatingForProduct(ProductPage currentPage)
{
var productReference = Reference.Create(currentPage.ContentGuid.ToString());
var criteria = new Criteria<RatingStatisticsFilter>
{
Filter = new RatingStatisticsFilter
{
Targets = new List<Reference> { productReference }
},
PageInfo = new PageInfo { PageSize = 1 }
};
var ratingStatisticsPage = ratingStatisticsService.Get(criteria);
return ratingStatisticsPage.Results.FirstOrDefault();
}
private ResultPage<RatingStatistics> GetPopularProducts()
{
var criteria = new Criteria<RatingStatisticsFilter>
{
PageInfo = new PageInfo { PageSize = 10, PageOffset = 0 },
OrderBy = new List<SortInfo>
{
new SortInfo(RatingStatisticsSortFields.Mean, false)
}
};
var ratingStatisticsPage = ratingStatisticsService.Get(criteria);
return ratingStatisticsPage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment