using System; using System.Threading; using System.Threading.Tasks; using HN.Domain; using MediatR; namespace HN.Application { public sealed class CommentLinkCommandHandler : IRequestHandler { 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 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; } } }