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

namespace Valg.EVA.Skanning.Commands.RefreshUpdatedTimestampOnTransfer
{
    public class RefreshUpdatedTimestampOnTransferAsyncCommandHandler : IHandleAsyncCommands<RefreshUpdatedTimestampOnTransferCommand>
    {
        private readonly TransactionScopeFactory factory;
        private readonly ITransferWriteableRepository transferWriteableRepository;

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

        public async Task HandleCommandAsync(RefreshUpdatedTimestampOnTransferCommand command)
        {
            using (var scope = factory.GetTransactionScope())
            {
                await transferWriteableRepository.RefreshUpdatedTimestampOnTransfer(command.TransferId, command.UserName, command.LastUpdated).ConfigureAwait(false);

                scope.Complete();
            }
        }
    }
}
