refactor: made the read and set settings functions more efficient
This commit is contained in:
@@ -8,6 +8,9 @@ Logging.Log(Logging.LogType.Information, "Startup", "Starting Gaseous Server");
|
||||
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
|
||||
db.InitDB();
|
||||
|
||||
// load app settings
|
||||
Config.InitSettings();
|
||||
|
||||
// set initial values
|
||||
Guid APIKey = Guid.NewGuid();
|
||||
if (Config.ReadSetting("API Key", "Test API Key") == "Test API Key")
|
||||
|
@@ -138,7 +138,34 @@ namespace gaseous_tools
|
||||
File.WriteAllText(ConfigurationFilePath, configRaw);
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> AppSettings = new Dictionary<string, string>();
|
||||
|
||||
public static void InitSettings()
|
||||
{
|
||||
Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
|
||||
string sql = "SELECT * FROM settings";
|
||||
|
||||
DataTable dbResponse = db.ExecuteCMD(sql);
|
||||
foreach (DataRow dataRow in dbResponse.Rows)
|
||||
{
|
||||
if (AppSettings.ContainsKey((string)dataRow["setting"]))
|
||||
{
|
||||
AppSettings[(string)dataRow["setting"]] = (string)dataRow["value"];
|
||||
}
|
||||
else
|
||||
{
|
||||
AppSettings.Add((string)dataRow["setting"], (string)dataRow["value"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string ReadSetting(string SettingName, string DefaultValue)
|
||||
{
|
||||
if (AppSettings.ContainsKey(SettingName))
|
||||
{
|
||||
return AppSettings[SettingName];
|
||||
}
|
||||
else
|
||||
{
|
||||
Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
|
||||
string sql = "SELECT * FROM settings WHERE setting = @settingname";
|
||||
@@ -153,10 +180,12 @@ namespace gaseous_tools
|
||||
if (dbResponse.Rows.Count == 0)
|
||||
{
|
||||
// no value with that name stored - respond with the default value
|
||||
SetSetting(SettingName, DefaultValue);
|
||||
return DefaultValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppSettings.Add(SettingName, (string)dbResponse.Rows[0][0]);
|
||||
return (string)dbResponse.Rows[0][0];
|
||||
}
|
||||
}
|
||||
@@ -166,6 +195,7 @@ namespace gaseous_tools
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetSetting(string SettingName, string Value)
|
||||
{
|
||||
@@ -179,6 +209,15 @@ namespace gaseous_tools
|
||||
try
|
||||
{
|
||||
db.ExecuteCMD(sql, dbDict);
|
||||
|
||||
if (AppSettings.ContainsKey(SettingName))
|
||||
{
|
||||
AppSettings[SettingName] = Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
AppSettings.Add(SettingName, Value);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
Reference in New Issue
Block a user