hn-dotnet/Application/VoteForLink/VoteForLinkCommandHandler.cs
Julien Leicher 3cd5133f66 add-aspnet-identity (#26)
add exception filter when user not connected
default to needing authentication and apply anonymous to some actions
add user in get requests
add user relation in link, comment and vote
signup and in are ok now!
2020-12-11 17:59:35 +01:00

39 lines
1.0 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using HN.Domain;
using MediatR;
namespace HN.Application
{
public sealed class VoteForLinkCommandHandler : IRequestHandler<VoteForLinkCommand>
{
private readonly ILinkRepository _linkRepository;
private readonly IExecutingUserProvider _executingUserProvider;
public VoteForLinkCommandHandler(ILinkRepository linkRepository, IExecutingUserProvider executingUserProvider)
{
_linkRepository = linkRepository;
_executingUserProvider = executingUserProvider;
}
public async Task<Unit> Handle(VoteForLinkCommand request, CancellationToken cancellationToken)
{
var link = await _linkRepository.GetByIdAsync(request.LinkId);
var userId = _executingUserProvider.GetCurrentUserId();
switch (request.Type)
{
case VoteType.Up:
link.Upvote(userId);
break;
case VoteType.Down:
link.Downvote(userId);
break;
}
await _linkRepository.UpdateAsync(link);
return Unit.Value;
}
}
}