Example #1
0
        private Paged <BlogDetails> ToPaged(X.PagedList.IPagedList <BlogDetailDisplay> blogs)
        {
            var names       = blogs.Select(b => b.blog.Author).Distinct();
            var authors     = udb_.Users.Where(u => names.Contains(u.UserName)).ToDictionary(u => u.UserName);
            var authorInfos = names.Select(n =>
            {
                if (authors.TryGetValue(n, out UserProfile u))
                {
                    var user = Models.App.User.FromUserProfile(u, Url.Action("Show", "Avatar", new { name = u.UserName }, Request.Scheme));
                    return(user);
                }
                return(new User {
                    UserName = n, NickName = n
                });
            }).ToDictionary(u => u.UserName);

            ratingUtil_.PrepareRatings(blogs.Select(b => b.blog.BlogID));
            var ids   = blogs.Select(b => b.blog.BlogID);
            var posts = db_.Posts.Where(p => p.IdType == ItemType.Blog && ids.Contains(p.ItemId))
                        .GroupJoin(db_.BlogRatings, p => p.PostId, r => r.PostId, (p, r) => new { post = p, ratings = r })
                        .SelectMany(a => a.ratings.DefaultIfEmpty(), (p, r) => new {
                p.post,
                blograting = r,
                uc         = p.post.Ratings.Count(ra => ra.Value == 1),
                dc         = p.post.Ratings.Count(ra => ra.Value == -1),
                uv         = User.Identity.IsAuthenticated ? p.post.Ratings.FirstOrDefault(ra => ra.Rater == User.Identity.Name) : null
            })
                        .GroupBy(p => p.post.ItemId, (key, ps) => new
            {
                Key   = key,
                Posts = ps
                        .OrderByDescending(p => p.post.Rating)
                        .ThenByDescending(p => p.post.PostDate)
                        .Take(5)
            })
                        .ToDictionary(p => p.Key, p => p.Posts);
            var userRatings = ratingUtil_.GetUsersRatingValues(ids);
            var items       = new X.PagedList.StaticPagedList <BlogDetails>(blogs.Select(bd =>
            {
                blogUtil_.ProcessBlogDetails(bd);
                return(GetBlogDetails(bd,
                                      authorInfos[bd.blog.Author],
                                      posts.ContainsKey(bd.blog.BlogID) ? posts[bd.blog.BlogID].Select(c => new Comment
                {
                    Author = c.post.Author,
                    CommentId = c.post.PostId,
                    Content = c.post.Content,
                    ItemId = c.post.ItemId,
                    UpvoteCount = c.uc,
                    DownvoteCount = c.dc,
                    IsUpvoted = c.uv == null ? new bool?() : c.uv.Value == 1,
                    IsDownvoted = c.uv == null ? new bool?() : c.uv.Value == -1,
                    CreateDate = c.post.PostDate,
                    Type = Comment.CommentType.Blog,
                    Rating = c.blograting == null ? new int?() : c.blograting.value,
                }).ToArray() : new Comment[0],
                                      userRatings.ContainsKey(bd.blog.BlogID) ? userRatings[bd.blog.BlogID] : new int?()));
            }), blogs.GetMetaData());

            return(new Paged <BlogDetails>(items));
        }