Allow config for servers to be read from database
This commit is contained in:
parent
5ae3d7599d
commit
33b70bf884
43
ChaosBot/Database/Entity/ServerConfigurationFlag.cs
Normal file
43
ChaosBot/Database/Entity/ServerConfigurationFlag.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Xml.Serialization;
|
||||
using ChaosBot.Attribute;
|
||||
|
||||
namespace ChaosBot.Database.Entity
|
||||
{
|
||||
[DBEntity("ServerConfigurationFlags")]
|
||||
public class ServerConfigurationFlag<T>
|
||||
{
|
||||
[DBUnique]
|
||||
public string key { get; }
|
||||
|
||||
public string serializedValue { get; }
|
||||
|
||||
[DBUnique]
|
||||
public long guildId { get; }
|
||||
|
||||
public ServerConfigurationFlag(string key, T value, ulong guildId)
|
||||
{
|
||||
this.serializedValue = Serialize(value);
|
||||
this.key = key;
|
||||
this.guildId = Convert.ToInt64(guildId);
|
||||
}
|
||||
|
||||
public T GetValue()
|
||||
{
|
||||
return Deserialize(serializedValue);
|
||||
}
|
||||
|
||||
public static string Serialize(T value)
|
||||
{
|
||||
return JsonSerializer.Serialize(value);
|
||||
}
|
||||
|
||||
public static T Deserialize(string serializedValue)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(serializedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using ChaosBot.Database.Entity;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using NLog;
|
||||
|
||||
@ -5,24 +9,49 @@ namespace ChaosBot.Database.Repository
|
||||
{
|
||||
public static class ConfigurationRepository
|
||||
{
|
||||
private static Logger _logger = Program._logger;
|
||||
public static IConfiguration AppSettingsHandler { get; set; }
|
||||
|
||||
private static readonly string ServersTable = "ServerConfigurationFlags";
|
||||
|
||||
/// <summary>
|
||||
/// Get global configuration option
|
||||
/// </summary>
|
||||
/// <param name="configurationFlag"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetValue<T>(string configurationFlag)
|
||||
{
|
||||
return AppSettingsHandler.GetValue<T>(configurationFlag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get guild specific configuration option
|
||||
/// </summary>
|
||||
/// <param name="configurationFlag"></param>
|
||||
/// <param name="guildId"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public static T GetValue<T>(string configurationFlag, ulong guildId)
|
||||
{
|
||||
if (Program._logger == null) return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
||||
|
||||
Program._logger.Info($"===> called for {configurationFlag} in {guildId}");
|
||||
Dictionary<string, object> filterColumns = new Dictionary<string, object>();
|
||||
filterColumns.Add("key", configurationFlag);
|
||||
filterColumns.Add("guildId", Convert.ToInt64(guildId));
|
||||
DataTable valueTable = Controller.SelectQuery(ServersTable, filterColumns: filterColumns);
|
||||
if (valueTable.Rows.Count == 1)
|
||||
{
|
||||
return ServerConfigurationFlag<T>.Deserialize(valueTable.Rows[0]["serializedValue"].ToString());
|
||||
}
|
||||
return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
||||
}
|
||||
|
||||
public static T SetValue<T>(string configurationFlag, T value)
|
||||
{
|
||||
return AppSettingsHandler.GetValue<T>(configurationFlag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Grab configuration from appsettings.json, method does not look at the database
|
||||
/// </summary>
|
||||
/// <param name="configurationFlag"></param>
|
||||
/// <returns></returns>
|
||||
public static IConfigurationSection GetSection(string configurationFlag)
|
||||
{
|
||||
return AppSettingsHandler.GetSection(configurationFlag);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user