using System.Threading.Tasks;
using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Repositories.Transfers;

namespace Valg.EVA.Skanning.Commands.SetTransferSuccessful
{
    public class SetTransferSuccessfulAsyncCommandHandler : IHandleAsyncCommands<SetTransferSuccessfulCommand>
    {
        private readonly TransactionScopeFactory transactionScopeFactory;
        private readonly ITransferWriteableRepository transferWriteableRepository;

        public SetTransferSuccessfulAsyncCommandHandler(TransactionScopeFactory transactionScopeFactory, ITransferWriteableRepository transferWriteableRepository)
        {
            this.transactionScopeFactory = transactionScopeFactory;
            this.transferWriteableRepository = transferWriteableRepository;
        }

        public async Task HandleCommandAsync(SetTransferSuccessfulCommand command)
        {
            using (var scope = transactionScopeFactory.GetTransactionScope())
            {
                await transferWriteableRepository.SetTransferSuccessfulAsync(command.TransferId, command.UserName, command.FileId).ConfigureAwait(false);

                scope.Complete();
            }
        }
    }
}
