Example #1
0
 private PagedList <Comment> GetParentCommentsByEntityId(CommentSearchCondition commentSearchCondition)
 {
     using (var context = new CommentDbContext())
     {
         var parentCommentQuery = GetComments(context, commentSearchCondition);
         var parentComments     = parentCommentQuery.OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
         FetchAndIncludeReplies(context, parentComments, commentSearchCondition, false);
         return(parentComments);
     }
 }
Example #2
0
 private static IQueryable <Comment> GetComments(CommentDbContext context, CommentSearchCondition commentSearchCondition)
 {
     if (CommonHelpers.IsNotEmptyGuid(commentSearchCondition.ParentId))
     {
         return(context.Comments.Where(x => x.ParentId == commentSearchCondition.ParentId));
     }
     else
     {
         return(context.Comments.Where(x => x.PostId == commentSearchCondition.PostId && (x.ParentId == null || x.ParentId == "")));
     }
 }
Example #3
0
 private static IQueryable<Comment> GetComments(CommentDbContext context, CommentSearchCondition commentSearchCondition)
 {
     if (CommonHelpers.IsNotEmptyGuid(commentSearchCondition.ParentId))
     {
         return context.Comments.Where(x => x.ParentId == commentSearchCondition.ParentId);
     }
     else
     {
         return context.Comments.Where(x => x.PostId == commentSearchCondition.PostId && (x.ParentId == null || x.ParentId == ""));
     }
 }
Example #4
0
        public JsonResult Get(string postId, string parentId, int page, int pageSize)
        {
            CommentSearchCondition condition = new CommentSearchCondition()
            {
                PostId     = postId,
                ParentId   = parentId,
                PageNumber = page,
                PageSize   = pageSize
            };

            var replies = commentRepository.GetComments(condition);

            var result = GetExtractedComments(replies, replies.TotalCount);

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Example #5
0
        public JsonResult Get(string postId, string parentId, int page, int pageSize)
        {
            CommentSearchCondition condition = new CommentSearchCondition()
            {
                PostId = postId,
                ParentId = parentId,
                PageNumber = page,
                PageSize = pageSize
            };

            var replies = commentRepository.GetComments(condition);

            var result = GetExtractedComments(replies, replies.TotalCount);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
Example #6
0
        private void FetchAndIncludeReplies(CommentDbContext dbContext, PagedList <Comment> parentComments, CommentSearchCondition commentSearchCondition, bool fetchCountOnly)
        {
            if (parentComments == null || !parentComments.Any())
            {
                return;
            }
            var parentCommentIds = parentComments.Select(x => x.Id).ToArray();
            var childComments    = dbContext.Comments.Where(x => parentCommentIds.Contains(x.ParentId));

            foreach (var parent in parentComments)
            {
                if (fetchCountOnly)
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(true);
                }
                else
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
                    FetchAndIncludeReplies(dbContext, parent.Replies, commentSearchCondition, true);
                }
            }
        }
Example #7
0
 public PagedList <Comment> GetComments(CommentSearchCondition searchCondition)
 {
     return(GetParentCommentsByEntityId(searchCondition));
 }
Example #8
0
        private void FetchAndIncludeReplies(CommentDbContext dbContext, PagedList<Comment> parentComments, CommentSearchCondition commentSearchCondition, bool fetchCountOnly)
        {
            if (parentComments == null || !parentComments.Any()) return;
            var parentCommentIds = parentComments.Select(x => x.Id).ToArray();
            var childComments = dbContext.Comments.Where(x => parentCommentIds.Contains(x.ParentId));

            foreach (var parent in parentComments)
            {
                if (fetchCountOnly)
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(true);
                }
                else
                {
                    parent.Replies = childComments.Where(child => child.ParentId == parent.Id).OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
                    FetchAndIncludeReplies(dbContext, parent.Replies, commentSearchCondition, true);
                }
            }
        }
Example #9
0
 public PagedList<Comment> GetComments(CommentSearchCondition searchCondition)
 {
     return GetParentCommentsByEntityId(searchCondition);
 }
Example #10
0
 private PagedList<Comment> GetParentCommentsByEntityId(CommentSearchCondition commentSearchCondition)
 {
     using (var context = new CommentDbContext())
     {
         var parentCommentQuery = GetComments(context, commentSearchCondition);
         var parentComments = parentCommentQuery.OrderByDescending(p => p.CreatedOn).ToPagedList(commentSearchCondition.PageNumber, commentSearchCondition.PageSize);
         FetchAndIncludeReplies(context, parentComments, commentSearchCondition, false);
         return parentComments;
     }
 }