hn-dotnet/Application/VoteForComment/VoteForCommentCommandHandler.cs
Julien Leicher 7a9aefbefc tiny-refactors (#27)
add UnitWorkBehavior and some files moving
add Docker stuff to prepare heroku deployment
rename (Up/Down)vote
add sample for msbuild tasks
2020-12-17 12:01:11 +01:00

37 lines
1.0 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using HN.Domain;
using MediatR;
namespace HN.Application
{
public sealed class VoteForCommentCommandHandler : IRequestHandler<VoteForCommentCommand>
{
private readonly ICommentRepository _commentRepository;
private readonly IExecutingUserProvider _executingUserProvider;
public VoteForCommentCommandHandler(ICommentRepository commentRepository, IExecutingUserProvider executingUserProvider)
{
_commentRepository = commentRepository;
_executingUserProvider = executingUserProvider;
}
public async Task<Unit> Handle(VoteForCommentCommand request, CancellationToken cancellationToken)
{
var comment = await _commentRepository.GetByIdAsync(request.CommentId);
var userId = _executingUserProvider.GetCurrentUserId();
switch (request.Type)
{
case VoteType.Up:
comment.UpvoteBy(userId);
break;
case VoteType.Down:
comment.DownvoteBy(userId);
break;
}
return Unit.Value;
}
}
}