using System;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using MyHN.Domain;

namespace MyHN.Application
{
  public 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 = _linkRepository.GetById(request.LinkId);
      var comment = link.AddComment(request.Content);

      await _commentRepository.AddAsync(comment);

      return comment.Id;
    }
  }
}