public async Task <List <CommentDto> > GetListOfPostAsync(GetCommentListOfPostAsync input) { var comments = await _commentRepository.GetListOfPostAsync(input.PostId); return(new List <CommentDto>( ObjectMapper.Map <List <Comment>, List <CommentDto> >(comments))); }
public async Task <List <CommentWithRepliesDto> > GetHierarchicalListOfPostAsync(GetCommentListOfPostAsync input) { var comments = await GetListOfPostAsync(input); var hierarchicalComments = new List <CommentWithRepliesDto>(); foreach (var commentDto in comments) { var parent = hierarchicalComments.Find(c => c.Comment.Id == commentDto.RepliedCommentId); if (parent != null) { parent.Replies.Add(commentDto); } else { hierarchicalComments.Add(new CommentWithRepliesDto() { Comment = commentDto }); } } return(hierarchicalComments); }
public async Task <List <CommentWithRepliesDto> > GetHierarchicalListOfPostAsync(GetCommentListOfPostAsync input) { var comments = await GetListOfPostAsync(input); var userDictionary = new Dictionary <Guid, BlogUserDto>(); foreach (var commentDto in comments) { if (commentDto.CreatorId.HasValue) { var creatorUser = await UserLookupService.FindByIdAsync(commentDto.CreatorId.Value); if (creatorUser != null && !userDictionary.ContainsKey(creatorUser.Id)) { userDictionary.Add(creatorUser.Id, ObjectMapper.Map <BlogUser, BlogUserDto>(creatorUser)); } } } foreach (var commentDto in comments) { if (commentDto.CreatorId.HasValue && userDictionary.ContainsKey((Guid)commentDto.CreatorId)) { commentDto.Writer = userDictionary[(Guid)commentDto.CreatorId]; } } var hierarchicalComments = new List <CommentWithRepliesDto>(); foreach (var commentDto in comments) { var parent = hierarchicalComments.Find(c => c.Comment.Id == commentDto.RepliedCommentId); if (parent != null) { parent.Replies.Add(commentDto); } else { hierarchicalComments.Add(new CommentWithRepliesDto() { Comment = commentDto }); } } hierarchicalComments = hierarchicalComments.OrderByDescending(c => c.Comment.CreationTime).ToList(); return(hierarchicalComments); }