36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System.Threading;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using MediatR;
|
|
using HN.Domain;
|
|
|
|
namespace HN.Application
|
|
{
|
|
public sealed class GetLinkQueryHandler : IRequestHandler<GetLinkQuery, LinkDto>
|
|
{
|
|
private readonly IHNContext _context;
|
|
|
|
public GetLinkQueryHandler(IHNContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public Task<LinkDto> Handle(GetLinkQuery request, CancellationToken cancellationToken)
|
|
{
|
|
var result = from link in _context.Links
|
|
join user in _context.Users on link.CreatedBy equals user.Id
|
|
where link.Id == request.Id
|
|
select new LinkDto
|
|
{
|
|
Id = link.Id,
|
|
Url = link.Url,
|
|
CreatedByName = user.UserName,
|
|
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());
|
|
}
|
|
}
|
|
} |