using Valg.EVA.Skanning.Entity.SqlConnection;
using Valg.EVA.Skanning.Repositories.EmergencyMode;

namespace Valg.EVA.Skanning.Commands.EmergencyMode
{
    public class CreateEmergencyModeCommandHandler : IHandleCommands<CreateEmergencyModeCommand>
    {
        private readonly IEmergencyModeWriteableRepository emergencyModeWriteableRepository;
        private readonly TransactionScopeFactory transactionScopeFactory;

        public CreateEmergencyModeCommandHandler(IEmergencyModeWriteableRepository emergencyModeWriteableRepository, TransactionScopeFactory transactionScopeFactory)
        {
            this.emergencyModeWriteableRepository = emergencyModeWriteableRepository;
            this.transactionScopeFactory = transactionScopeFactory;
        }

        public void HandleCommand(CreateEmergencyModeCommand command)
        {
            using (var scope = transactionScopeFactory.GetTransactionScope())
            {
                emergencyModeWriteableRepository.SaveToken(command.Token, command.CreateBy);
                scope.Complete();
            }
        }
    }
}
