hn-dotnet/Application/CommentLink/CommentLinkCommandHandler.cs
2020-12-11 16:00:29 +01:00

32 lines
1.0 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using HN.Domain;
using MediatR;
namespace HN.Application
{
public sealed class CommentLinkCommandHandler : IRequestHandler<CommentLinkCommand, Guid>
{
private readonly ILinkRepository _linkRepository;
private readonly ICommentRepository _commentRepository;
private readonly IExecutingUserProvider _executingUserProvider;
public CommentLinkCommandHandler(ILinkRepository linkRepository, ICommentRepository commentRepository, IExecutingUserProvider executingUserProvider)
{
_linkRepository = linkRepository;
_commentRepository = commentRepository;
_executingUserProvider = executingUserProvider;
}
public async Task<Guid> Handle(CommentLinkCommand request, CancellationToken cancellationToken)
{
var link = await _linkRepository.GetByIdAsync(request.LinkId);
var comment = link.AddComment(_executingUserProvider.GetCurrentUserId(), request.Content);
await _commentRepository.AddAsync(comment);
return comment.Id;
}
}
}