using System.Threading; using System.Linq; using System.Threading.Tasks; using MediatR; using HN.Domain; namespace HN.Application { public sealed class GetLinkQueryHandler : IRequestHandler { private readonly IDbContext _context; public GetLinkQueryHandler(IDbContext context) { _context = context; } public Task Handle(GetLinkQuery request, CancellationToken cancellationToken) { var result = from link in _context.Links where link.Id == request.Id select new LinkDto { Id = link.Id, Url = link.Url, CreatedAt = link.CreatedAt, UpVotes = link.Votes.Count(v => v.Type == VoteType.Up), DownVotes = link.Votes.Count(v => v.Type == VoteType.Down) }; return Task.FromResult(result.SingleOrDefault()); } } }