예제 #1
0
        /// <summary>
        /// Backs up the current environment to the datastore, excluding the environment variables added by Chami.
        /// </summary>
        /// <param name="repository">An <see cref="EnvironmentRepository"/> where to save the backup.</param>
        public static void Backup(EnvironmentRepository repository)
        {
            var environmentVariables = System.Environment.GetEnvironmentVariables();

            environmentVariables = RemoveCurrentChamiVariables(environmentVariables, repository);

            var backupEnvironment = new Environment();

            var environmentName = $"Backup of {DateTime.Now:s}";

            backupEnvironment.Name            = environmentName;
            backupEnvironment.EnvironmentType = EnvironmentType.BackupEnvironment;

            foreach (DictionaryEntry entry in environmentVariables)
            {
                var variableName        = (string)entry.Key;
                var variableValue       = (string)entry.Value;
                var environmentVariable = new EnvironmentVariable {
                    Name = variableName, Value = variableValue
                };
                backupEnvironment.EnvironmentVariables.Add(environmentVariable);
            }

            repository.InsertEnvironment(backupEnvironment);
        }
예제 #2
0
        /// <summary>
        /// Inserts an <see cref="Environment"/> object in the datastore and its associated <ses cref="EnvironmentVariable"/>s.
        /// </summary>
        /// <param name="environment">The <see cref="Environment"/> object to insert into the datastore.</param>
        /// <returns>The newly-inserted environment, with its EnvironmentId set. If the parameter is null, returns null.</returns>
        /// <exception cref="NotSupportedException">If the EnvironmentId is greater than 0 (i.e., if the <see cref="Environment"/> is already persisted to the datastore), a <see cref="NotSupportedException"/> is thrown.</exception>
        public Environment InsertEnvironment(Environment environment)
        {
            if (environment == null)
            {
                return(null);
            }

            if (environment.EnvironmentId > 0)
            {
                throw new NotSupportedException("Attempting to insert duplicate item in the database");
            }

            var queryString = @"INSERT INTO Environments(Name, AddedOn, EnvironmentType) VALUES (?, ?, ?)";

            using (var connection = GetConnection())
            {
                var transaction = connection.BeginTransaction();
                connection.Execute(queryString,
                                   new { environment.Name, environment.AddedOn, environment.EnvironmentType });
                var environmentVariableInsertQuery = @"
                INSERT INTO EnvironmentVariables(Name, Value, AddedOn, EnvironmentId)
                VALUES (?, ?, ?, ?)
";
                var selectQuery = @"
                    SELECT * 
                    FROM Environments e
                    WHERE e.AddedOn = ?";
                var results     = connection.Query <Environment>(selectQuery, new { environment.AddedOn });
                var result      = results.FirstOrDefault();
                environment.EnvironmentId = result.EnvironmentId;
                foreach (var environmentVariable in environment.EnvironmentVariables)
                {
                    environmentVariable.EnvironmentId = environment.EnvironmentId;
                    connection.Execute(environmentVariableInsertQuery,
                                       new
                    {
                        Name = environmentVariable.Name,
                        environmentVariable.Value,
                        environmentVariable.AddedOn,
                        environmentVariable.EnvironmentId
                    });
                }

                transaction.Commit();
            }

            return(environment);
        }
예제 #3
0
        /// <summary>
        /// Inserts an <see cref="Environment"/> in the datastore, or updates it if it's already present.
        /// </summary>
        /// <param name="environment">The <see cref="Environment"/> to persist in the datastore.</param>
        /// <returns>The newly-inserted or updated <see cref="Environment"/></returns>
        public Environment UpsertEnvironment(Environment environment)
        {
            if (environment.EnvironmentId == 0)
            {
                return(InsertEnvironment(environment));
            }

            UpdateEnvironment(environment);

            var newVariables = environment.EnvironmentVariables.Where(v => v.EnvironmentVariableId == 0);

            foreach (var environmentVariable in newVariables)
            {
                InsertVariable(environmentVariable, environment.EnvironmentId);
            }

            return(GetEnvironmentById(environment.EnvironmentId));
        }
예제 #4
0
 public void TestExcelExport()
 {
     using (var exporter = new EnvironmentExcelExporter())
     {
         var environment1 = new Environment()
         {
             Name = "Test", AddedOn = DateTime.Now, EnvironmentType = 0
         };
         environment1.EnvironmentVariables.Add(new EnvironmentVariable()
         {
             Name = "USER", Value = "MyValue", AddedOn = DateTime.Now, Environment = environment1
         });
         environment1.EnvironmentVariables.Add(new EnvironmentVariable()
         {
             Name = "USER", Value = "MyValue", AddedOn = DateTime.Now, Environment = environment1
         });
         environment1.EnvironmentVariables.Add(new EnvironmentVariable()
         {
             Name = "USER", Value = "MyValue", AddedOn = DateTime.Now, Environment = environment1
         });
         var environment2 = new Environment()
         {
             Name = "Test2", AddedOn = DateTime.Now, EnvironmentType = 0
         };
         environment2.EnvironmentVariables.Add(new EnvironmentVariable()
         {
             Name = "USER", Value = "MyValue", AddedOn = DateTime.Now, Environment = environment1
         });
         environment2.EnvironmentVariables.Add(new EnvironmentVariable()
         {
             Name = "USER", Value = "MyValue", AddedOn = DateTime.Now, Environment = environment1
         });
         environment2.EnvironmentVariables.Add(new EnvironmentVariable()
         {
             Name = "USER", Value = "MyValue", AddedOn = DateTime.Now, Environment = environment1
         });
         exporter.AddEnvironment(environment1);
         exporter.AddEnvironment(environment2);
         exporter.Export("d:\\test.xlsx");
     }
 }
예제 #5
0
        /// <summary>
        /// Updates the data associated with an <see cref="Environment"/> in the datastore.
        /// </summary>
        /// <param name="environment">The <see cref="Environment"/> to update.</param>
        /// <returns>The updated <see cref="Environment"/> entity.</returns>
        /// <exception cref="NotSupportedException">If the <see cref="Environment"/> is not yet persisted (i.e., its EnvironmentId is 0), a <see cref="NotSupportedException"/> is thrown.</exception>
        public Environment UpdateEnvironment(Environment environment)
        {
            if (environment.EnvironmentId == 0)
            {
                throw new NotSupportedException("Attempting to update an entity that has not been persisted.");
            }

            var updateQuery = @"
                UPDATE Environments
                SET Name = ?
                WHERE EnvironmentId = ?
";

            using (var connection = GetConnection())
            {
                connection.Execute(updateQuery, new { environment.Name, environment.EnvironmentId });
                foreach (var environmentVariable in environment.EnvironmentVariables)
                {
                    var envVarUpdateQuery = @"
                    UPDATE EnvironmentVariables 
                    SET Name = ?,
                        Value = ?
                    WHERE EnvironmentVariableId = ?
";
                    var updObj            = new
                    {
                        Name  = environmentVariable.Name,
                        Value = environmentVariable.Value,
                        EnvironmentVariableId = environmentVariable.EnvironmentVariableId
                    };

                    connection.Execute(envVarUpdateQuery, updObj);
                }
            }

            var updatedEnvironment = GetEnvironmentById(environment.EnvironmentId);

            return(updatedEnvironment);
        }
예제 #6
0
        public void InsertTest()
        {
            var repository = new EnvironmentRepository(connectionString);

            var environment = new Environment()
            {
                Name = "Example"
            };

            var environmentVariable1 = new EnvironmentVariable()
            {
                Name = "USER", Value = "TestUser"
            };
            var environmentVariable2 = new EnvironmentVariable()
            {
                Name = "PASSWORD", Value = "SECRET"
            };

            environment.EnvironmentVariables.Add(environmentVariable1);
            environment.EnvironmentVariables.Add(environmentVariable2);

            repository.InsertEnvironment(environment);
        }