myhn/Application/GetCommentById/GetCommentByIdQueryHandler.cs
2021-01-15 10:10:24 +01:00

34 lines
1.0 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using MediatR;
using System.Linq;
using MyHN.Domain;
namespace MyHN.Application
{
public class GetCommentByIdQueryHandler : IRequestHandler<GetCommentByIdQuery, CommentDto>
{
private readonly IContext _context;
public GetCommentByIdQueryHandler(IContext context)
{
_context = context;
}
public Task<CommentDto> Handle(GetCommentByIdQuery request, CancellationToken cancellationToken)
{
var result = from comment in _context.Comments
where comment.Id == request.Id
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.Single());
}
}
}