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

namespace Valg.EVA.Skanning.Commands.SetTransferFailed
{
    public class SetTransferFailedAsyncCommandHandler : IHandleAsyncCommands<SetTransferFailedCommand>
    {
        private readonly TransactionScopeFactory factory;
        private readonly ITransferWriteableRepository transferWriteableRepository;

        public SetTransferFailedAsyncCommandHandler(TransactionScopeFactory factory, ITransferWriteableRepository  transferWriteableRepository)
        {
            this.factory = factory;
            this.transferWriteableRepository = transferWriteableRepository;
        }

        public async Task HandleCommandAsync(SetTransferFailedCommand command)
        {
            using (var scope = factory.GetTransactionScope())
            {
                await transferWriteableRepository.SetTransferStateFailedAsync(command.TransferId, command.ErrorCode, command.UpdatedBy, command.OptionalConcurrencyToken);

                scope.Complete();
            }
        }
    }
}
