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

namespace MyHN.Application
{
  public class CreateLinkCommandHandler : IRequestHandler<CreateLinkCommand, Guid>
  {
    private readonly ILinkRepository _repository;
    private readonly IUserProvider _userProvider;

    public CreateLinkCommandHandler(
      ILinkRepository repository
    , IUserProvider userProvider)
    {
      _repository = repository;
      _userProvider = userProvider;
    }

    public async Task<Guid> Handle(CreateLinkCommand request, CancellationToken cancellationToken)
    {
      var link = new Link(request.Url, _userProvider.GetCurrentUserId());

      await _repository.AddAsync(link);

      return link.Id;
    }
  }
}