hn-dotnet/Application/GetLinkComments/GetLinkCommentsQueryHandler.cs
2020-12-22 10:47:22 +01:00

36 lines
1.2 KiB
C#

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using HN.Domain;
using MediatR;
namespace HN.Application
{
public sealed class GetLinkCommentsQueryHandler : IRequestHandler<GetLinkCommentsQuery, CommentDto[]>
{
private readonly IHNContext _context;
public GetLinkCommentsQueryHandler(IHNContext context)
{
_context = context;
}
public Task<CommentDto[]> 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());
}
}
}