public async Task <ActionResult> GetBlogDetails(BlogFilterPagination blogFilter) { try { var res = await this._blog.GetUserBlogDetails(blogFilter); return(Ok(res)); } catch (Exception ex) { throw ex; } }
public async Task <object> GetUserBlogDetails(BlogFilterPagination blogFilter) { PaginationResponse paginationResponse = new PaginationResponse(); try { List <BlogDataVM> lstVMGroupData = new List <BlogDataVM>(); BlogDataVM vMGroupData = new BlogDataVM(); if (string.IsNullOrEmpty(blogFilter.blogFilter.User)) { var res = await _aspContext.Blog.Select(b => new Blog() { Comments = b.Comments, NumberOfComments = b.Comments.Count, CreatedBy = b.CreatedBy, CreatedTime = DateTime.UtcNow, BlogContent = b.BlogContent, BlogID = b.BlogID, }).ToListAsync(); foreach (var item in res) { foreach (Comments items in item.Comments) { var vote = await _aspContext.LikeOrDisLikes.Where(p => p.CommentsID == items.CommentsID).ToListAsync(); if (vote != null) { items.NumberOfLike = vote.Count(p => p.CommentsID == items.CommentsID && p.LikeORDislike == true); items.NumberOfLike = vote.Count(p => p.CommentsID == items.CommentsID && p.LikeORDislike == false); } } } blogFilter.Pagination.totalItems = res.Count; paginationResponse.Data = res.Skip((blogFilter.Pagination.currentPage - 1) * blogFilter.Pagination.itemsPerPage) .Take(blogFilter.Pagination.itemsPerPage).GroupBy(p => p.BlogID).ToList(); paginationResponse.Pagination = blogFilter.Pagination; } else { var res = await _aspContext.Blog.Where(b => b.BlogContent.Equals(blogFilter.blogFilter.User)).Select(b => new Blog() { Comments = b.Comments, NumberOfComments = b.Comments.Count, CreatedBy = b.CreatedBy, CreatedTime = DateTime.UtcNow, BlogContent = b.BlogContent, BlogID = b.BlogID, }).ToListAsync(); foreach (var item in res) { foreach (Comments items in item.Comments) { var vote = _aspContext.LikeOrDisLikes.Where(p => p.CommentsID == items.CommentsID).ToList(); if (vote != null) { items.NumberOfLike = vote.Count(p => p.CommentsID == items.CommentsID && p.LikeORDislike == true); items.NumberOfLike = vote.Count(p => p.CommentsID == items.CommentsID && p.LikeORDislike == false); } } } blogFilter.Pagination.totalItems = res.Count; paginationResponse.Data = res.Skip((blogFilter.Pagination.currentPage - 1) * blogFilter.Pagination.itemsPerPage) .Take(blogFilter.Pagination.itemsPerPage).GroupBy(p => p.BlogID).ToList(); paginationResponse.Pagination = blogFilter.Pagination; } } catch (Exception ex) { throw; } return(paginationResponse); }