hn-dotnet/Application/VoteForComment/VoteForCommentCommandHandler.cs

36 lines
877 B
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;
public VoteForCommentCommandHandler(ICommentRepository commentRepository)
{
_commentRepository = commentRepository;
}
public async Task<Unit> Handle(VoteForCommentCommand request, CancellationToken cancellationToken)
{
var comment = await _commentRepository.GetByIdAsync(request.CommentId);
switch (request.Type)
{
case VoteType.Up:
comment.Upvote();
break;
case VoteType.Down:
comment.Downvote();
break;
}
await _commentRepository.UpdateAsync(comment);
return Unit.Value;
}
}
}