hn-dotnet/Application/CommentLink/CommentLinkCommandHandler.cs
2020-12-10 18:37:46 +01:00

30 lines
862 B
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;
public CommentLinkCommandHandler(ILinkRepository linkRepository, ICommentRepository commentRepository)
{
_linkRepository = linkRepository;
_commentRepository = commentRepository;
}
public async Task<Guid> Handle(CommentLinkCommand request, CancellationToken cancellationToken)
{
var link = await _linkRepository.GetByIdAsync(request.LinkId);
var comment = link.AddComment(request.Content);
await _commentRepository.AddAsync(comment);
return comment.Id;
}
}
}