34 lines
1.1 KiB
C#
34 lines
1.1 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
|
|
where comment.LinkId == request.LinkId
|
|
select new CommentDto
|
|
{
|
|
Id = comment.Id,
|
|
CreatedAt = comment.CreatedAt,
|
|
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());
|
|
}
|
|
}
|
|
} |