using System.Linq; using System.Threading; using System.Threading.Tasks; using HN.Domain; using MediatR; namespace HN.Application { public sealed class GetLinkCommentsQueryHandler : IRequestHandler { private readonly IHNContext _context; public GetLinkCommentsQueryHandler(IHNContext context) { _context = context; } public Task Handle(GetLinkCommentsQuery request, CancellationToken cancellationToken) { var comments = from comment in _context.Comments join user in _context.Users on comment.CreatedBy equals user.Id where comment.LinkId == request.LinkId select new CommentDto { Id = comment.Id, CreatedAt = comment.CreatedAt, CreatedByName = user.UserName, Content = comment.Content, UpVotes = comment.Votes.Count(c => c.Type == VoteType.Up), DownVotes = comment.Votes.Count(c => c.Type == VoteType.Down) }; return Task.FromResult(comments.OrderBy(c => c.CreatedAt).ToArray()); } } }