Exemple #1
0
        private IDictionary <Guid, ResourceRatingSummary> GetRatings <TResource>(Guid userId, IEnumerable <Guid> ids, ResourceModel model)
            where TResource : Resource
        {
            // Only need ratings for articles.

            var resourceIds = typeof(TResource) == typeof(Article)
                ? ids
                : new Guid[0];

            if (model.RelatedItems != null)
            {
                resourceIds = resourceIds.Concat(model.RelatedItems.OfType <Article>().Select(i => i.Id));
            }

            if (typeof(TResource) == typeof(Article) && model.RecentItems != null)
            {
                resourceIds = resourceIds.Concat(model.RecentItems.Select(i => i.Id));
            }

            if (model.TopRatedArticle != null)
            {
                resourceIds = resourceIds.Concat(new[] { model.TopRatedArticle.Id });
            }

            var distinctResourceIds = resourceIds.Distinct().ToList();
            var ratings             = _resourcesQuery.GetRatingSummaries(userId, distinctResourceIds);

            // Ensure that all ids are repesented.

            return((from i in distinctResourceIds
                    let r = ratings.ContainsKey(i) ? ratings[i] : new ResourceRatingSummary()
                            select new { i, r }).ToDictionary(x => x.i, x => x.r));
        }