using System.Threading; using System.Threading.Tasks; using HN.Domain; using MediatR; namespace HN.Application { public sealed class VoteForCommentCommandHandler : IRequestHandler { private readonly ICommentRepository _commentRepository; private readonly IExecutingUserProvider _executingUserProvider; public VoteForCommentCommandHandler(ICommentRepository commentRepository, IExecutingUserProvider executingUserProvider) { _commentRepository = commentRepository; _executingUserProvider = executingUserProvider; } public async Task 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; } await _commentRepository.UpdateAsync(comment); return Unit.Value; } } }