Add configuration get and set endpoint
This commit is contained in:
parent
27f1fab7db
commit
8bd9887463
@ -34,17 +34,25 @@ namespace ChaosBot.Database.Repository
|
|||||||
public static T GetValue<T>(string configurationFlag, ulong guildId)
|
public static T GetValue<T>(string configurationFlag, ulong guildId)
|
||||||
{
|
{
|
||||||
if (Program._logger == null) return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
if (Program._logger == null) return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
||||||
|
|
||||||
Program._logger.Info($"===> called for {configurationFlag} in {guildId}");
|
string valueSerialized = GetValueRaw(configurationFlag, Convert.ToInt64(guildId));
|
||||||
|
if (valueSerialized != null)
|
||||||
|
return ServerConfigurationFlag<T>.Deserialize(valueSerialized);
|
||||||
|
return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetValueRaw(string configurationFlag, long guildId)
|
||||||
|
{
|
||||||
Dictionary<string, object> filterColumns = new Dictionary<string, object>();
|
Dictionary<string, object> filterColumns = new Dictionary<string, object>();
|
||||||
filterColumns.Add("key", configurationFlag);
|
filterColumns.Add("key", configurationFlag);
|
||||||
filterColumns.Add("guildId", Convert.ToInt64(guildId));
|
filterColumns.Add("guildId", guildId);
|
||||||
DataTable valueTable = Controller.SelectQuery(ServersTable, filterColumns: filterColumns);
|
DataTable valueTable = Controller.SelectQuery(ServersTable, filterColumns: filterColumns);
|
||||||
if (valueTable.Rows.Count == 1)
|
if (valueTable.Rows.Count == 1)
|
||||||
{
|
{
|
||||||
return ServerConfigurationFlag<T>.Deserialize(valueTable.Rows[0]["serializedValue"].ToString());
|
return valueTable.Rows[0]["serializedValue"].ToString();
|
||||||
}
|
}
|
||||||
return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -56,5 +64,45 @@ namespace ChaosBot.Database.Repository
|
|||||||
{
|
{
|
||||||
return AppSettingsHandler.GetSection(configurationFlag);
|
return AppSettingsHandler.GetSection(configurationFlag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get guild specific configuration option
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="configurationFlag"></param>
|
||||||
|
/// <param name="guildId"></param>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static void SetValue<T>(string configurationFlag, ulong guildId, T value)
|
||||||
|
{
|
||||||
|
SetValueRaw(configurationFlag, Convert.ToInt64(guildId), ServerConfigurationFlag<T>.Serialize(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void SetValueRaw(string configurationFlag, long guildId, string serializedValue)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Dictionary<string, object> filterColumns = new Dictionary<string, object>();
|
||||||
|
filterColumns.Add("key", configurationFlag);
|
||||||
|
filterColumns.Add("guildId", guildId);
|
||||||
|
|
||||||
|
Dictionary<string, object> newValue = new Dictionary<string, object>();
|
||||||
|
newValue.Add("key", configurationFlag);
|
||||||
|
newValue.Add("guildId", guildId);
|
||||||
|
newValue.Add("serializedValue", serializedValue);
|
||||||
|
|
||||||
|
DataTable valueTable = Controller.SelectQuery(ServersTable, "COUNT(*)", filterColumns);
|
||||||
|
if ((long) valueTable.Rows[0]["COUNT(*)"] == 1)
|
||||||
|
{
|
||||||
|
Controller.UpdateQuery(ServersTable, newValue, filterColumns);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Controller.InsertQuery(ServersTable, newValue);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Program._logger.Fatal($"ConfigurationRepository.SetValueRaw: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
26
ChaosBot/WebServer/App/Controller/ConfigurationController.cs
Normal file
26
ChaosBot/WebServer/App/Controller/ConfigurationController.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using ChaosBot.Database.Repository;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using NLog;
|
||||||
|
|
||||||
|
namespace ChaosBot.WebServer.App.Controller
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("/config/{guildId}/{flag}")]
|
||||||
|
public class ConfigurationController
|
||||||
|
{
|
||||||
|
private readonly Logger _logger = Program._logger;
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public string GetConfigurationFlag(long guildId, string flag)
|
||||||
|
{
|
||||||
|
return ConfigurationRepository.GetValueRaw(flag, guildId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public void SetConfigurationFlag(long guildId, string flag, [FromBody] string serializedValue)
|
||||||
|
{
|
||||||
|
ConfigurationRepository.SetValueRaw(flag, guildId, serializedValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user