myhn/Application/GetCommentsByLink/GetCommentsByLinkQueryHandler.cs
2021-01-08 16:26:19 +01:00

35 lines
1.1 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using MediatR;
using MyHN.Domain;
namespace MyHN.Application
{
public class GetCommentsByLinkQueryHandler : IRequestHandler<GetCommentsByLinkQuery, CommentDto[]>
{
private readonly IContext _context;
public GetCommentsByLinkQueryHandler(IContext context)
{
_context = context;
}
public Task<CommentDto[]> Handle(GetCommentsByLinkQuery request, CancellationToken cancellationToken)
{
var result = from comment in _context.Comments
where comment.LinkId == request.LinkId
orderby comment.CreatedAt descending
select new CommentDto
{
Id = comment.Id,
CreatedAt = comment.CreatedAt,
Content = comment.Content,
UpvotesCount = comment.Votes.Count(v => v.Direction == VoteType.Up),
DownvotesCount = comment.Votes.Count(v => v.Direction == VoteType.Down)
};
return Task.FromResult(result.ToArray());
}
}
}