make some methods public for testing purposes
This commit is contained in:
parent
3c682780a4
commit
5b552ac310
@ -11,106 +11,114 @@ namespace ChaosBot.Attribute
|
|||||||
public static class AssemblyController
|
public static class AssemblyController
|
||||||
{
|
{
|
||||||
private static Logger _logger = Program._logger;
|
private static Logger _logger = Program._logger;
|
||||||
public static void Register()
|
public static void RegisterAll()
|
||||||
{
|
{
|
||||||
Assembly dbEntityAssembly = Assembly.GetAssembly(typeof(DBEntity));
|
Assembly dbEntityAssembly = Assembly.GetAssembly(typeof(DBEntity));
|
||||||
if (dbEntityAssembly != null)
|
if (dbEntityAssembly != null)
|
||||||
{
|
{
|
||||||
foreach (Type type in dbEntityAssembly.GetTypes())
|
foreach (Type type in dbEntityAssembly.GetTypes())
|
||||||
{
|
{
|
||||||
try
|
RegisterDBEntityType(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void RegisterDBEntityType(Type type)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (type.GetCustomAttributes(typeof(DBEntity), true).Length > 0)
|
||||||
|
{
|
||||||
|
// Get a reference to the attribute
|
||||||
|
DBEntity dbEntity = type.GetCustomAttributes(typeof(DBEntity)).Cast<DBEntity>().First();
|
||||||
|
|
||||||
|
// Generate columns
|
||||||
|
// name, type, constraintName, constraints,
|
||||||
|
List<Tuple<string, string, string, string>> columnList = new List<Tuple<string, string, string, string>>();
|
||||||
|
|
||||||
|
// Get the table as an easy variable
|
||||||
|
string table = dbEntity.Table;
|
||||||
|
|
||||||
|
// Loop through all fields
|
||||||
|
foreach (PropertyInfo prop in type.GetProperties())
|
||||||
{
|
{
|
||||||
if (type.GetCustomAttributes(typeof(DBEntity), true).Length > 0)
|
string columnName = prop.Name;
|
||||||
|
string columnType = null;
|
||||||
|
if (prop.PropertyType.IsGenericType &&
|
||||||
|
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||||
{
|
{
|
||||||
// Get a reference to the attribute
|
Type[] typeArguments = prop.PropertyType.GetGenericArguments();
|
||||||
DBEntity dbEntity = type.GetCustomAttributes(typeof(DBEntity)).Cast<DBEntity>().First();
|
if (typeArguments.Length == 1)
|
||||||
|
columnType = DBEntity.DataTypes.GetValueOrDefault(typeArguments[0]).ToString();
|
||||||
// Generate columns
|
}
|
||||||
// name, type, constraintName, constraints,
|
else
|
||||||
List<Tuple<string, string, string, string>> columnList = new List<Tuple<string, string, string, string>>();
|
{
|
||||||
|
columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString();
|
||||||
// Get the table as an easy variable
|
}
|
||||||
string table = dbEntity.Table;
|
|
||||||
|
|
||||||
// Loop through all fields
|
StringBuilder constraintNameBuilder = new StringBuilder($"{table}_{columnName}");
|
||||||
foreach (PropertyInfo prop in type.GetProperties())
|
List<string> constraintsList = new List<string>();
|
||||||
|
|
||||||
|
// Go through every attribute
|
||||||
|
foreach (object fieldAttribute in prop.GetCustomAttributes(true))
|
||||||
|
{
|
||||||
|
if (fieldAttribute is DBFieldAttribute dBFieldAttribute)
|
||||||
{
|
{
|
||||||
string columnName = prop.Name;
|
string constraintSuffix =
|
||||||
string columnType = null;
|
DBEntity.ConstrainNames.GetValueOrDefault(dBFieldAttribute.GetType(), null);
|
||||||
if (prop.PropertyType.IsGenericType &&
|
if (constraintSuffix != null)
|
||||||
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
||||||
{
|
{
|
||||||
Type[] typeArguments = prop.PropertyType.GetGenericArguments();
|
constraintNameBuilder.Append($"_{constraintSuffix}");
|
||||||
if (typeArguments.Length == 1)
|
constraintsList.Add($"{dBFieldAttribute.GetSQLiteQuery()}");
|
||||||
columnType = DBEntity.DataTypes.GetValueOrDefault(typeArguments[0]).ToString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString();
|
|
||||||
}
|
|
||||||
StringBuilder constraintNameBuilder = new StringBuilder($"{table}_{columnName}");
|
|
||||||
List<string> constraintsList = new List<string>();
|
|
||||||
|
|
||||||
// Go through every attribute
|
|
||||||
foreach (object fieldAttribute in prop.GetCustomAttributes(true))
|
|
||||||
{
|
|
||||||
if (fieldAttribute is DBFieldAttribute dBFieldAttribute)
|
|
||||||
{
|
|
||||||
string constraintSuffix =
|
|
||||||
DBEntity.ConstrainNames.GetValueOrDefault(dBFieldAttribute.GetType(), null);
|
|
||||||
if (constraintSuffix != null)
|
|
||||||
{
|
|
||||||
constraintNameBuilder.Append($"_{constraintSuffix}");
|
|
||||||
constraintsList.Add($"{dBFieldAttribute.GetSQLiteQuery()}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
string constraintName = constraintNameBuilder.ToString();
|
|
||||||
if (constraintsList.Count > 0)
|
|
||||||
{
|
|
||||||
constraintsList.Insert(0, "CONSTRAINT");
|
|
||||||
constraintsList.Insert(1, constraintName);
|
|
||||||
}
|
|
||||||
string constraints = String.Join(" ", constraintsList);
|
|
||||||
columnList.Add(new Tuple<string, string, string, string>(columnName, columnType, constraintName, constraints));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check table exists
|
|
||||||
bool tableExists =
|
|
||||||
Controller.RawQuery($"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'")
|
|
||||||
.Rows.Count > 0;
|
|
||||||
|
|
||||||
if (!tableExists)
|
|
||||||
{
|
|
||||||
string columnDefs = String.Join(", ", columnList.Select(c => $"{c.Item1} {c.Item2} {c.Item4}"));
|
|
||||||
string query = $"CREATE TABLE {table} ({columnDefs})";
|
|
||||||
Controller.RawQuery(query, readOutput: false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (Tuple<string,string,string,string> column in columnList)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string query =
|
|
||||||
$"ALTER TABLE {table} ADD COLUMN {column.Item1} {column.Item2} {column.Item4}";
|
|
||||||
Controller.RawQuery(query, readOutput: false, throwError: true);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// ignored
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string constraintName = constraintNameBuilder.ToString();
|
||||||
|
if (constraintsList.Count > 0)
|
||||||
|
{
|
||||||
|
constraintsList.Insert(0, "CONSTRAINT");
|
||||||
|
constraintsList.Insert(1, constraintName);
|
||||||
|
}
|
||||||
|
|
||||||
|
string constraints = String.Join(" ", constraintsList);
|
||||||
|
columnList.Add(
|
||||||
|
new Tuple<string, string, string, string>(columnName, columnType, constraintName, constraints));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
// Check table exists
|
||||||
|
bool tableExists =
|
||||||
|
Controller.RawQuery($"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'")
|
||||||
|
.Rows.Count > 0;
|
||||||
|
|
||||||
|
if (!tableExists)
|
||||||
{
|
{
|
||||||
_logger.Error($"AssemblyController.Register: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
string columnDefs = String.Join(", ", columnList.Select(c => $"{c.Item1} {c.Item2} {c.Item4}"));
|
||||||
|
string query = $"CREATE TABLE {table} ({columnDefs})";
|
||||||
|
Controller.RawQuery(query, readOutput: false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (Tuple<string, string, string, string> column in columnList)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string query =
|
||||||
|
$"ALTER TABLE {table} ADD COLUMN {column.Item1} {column.Item2} {column.Item4}";
|
||||||
|
Controller.RawQuery(query, readOutput: false, throwError: true);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error($"AssemblyController.Register: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,12 +1,13 @@
|
|||||||
using NLog;
|
using NLog;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
using ChaosBot.Discord;
|
using ChaosBot.Discord;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using ChaosBot.Attribute;
|
using ChaosBot.Attribute;
|
||||||
using ChaosBot.Database.Repository;
|
using ChaosBot.Database.Repository;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
[assembly: InternalsVisibleTo("ChaosBot.UnitTests")]
|
||||||
namespace ChaosBot
|
namespace ChaosBot
|
||||||
{
|
{
|
||||||
internal class Program
|
internal class Program
|
||||||
@ -27,27 +28,26 @@ namespace ChaosBot
|
|||||||
/*
|
/*
|
||||||
* Load configuration from AppSettings.Json and save as Cfg
|
* Load configuration from AppSettings.Json and save as Cfg
|
||||||
*/
|
*/
|
||||||
ConfigurationRepository.AppSettingsHandler = new ConfigurationBuilder()
|
LoadConfiguration(appsettingsPath);
|
||||||
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
|
||||||
.AddJsonFile(appsettingsPath, optional: false, reloadOnChange: true).Build();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize the _logger for logging purposes
|
* Initialize the _logger for logging purposes
|
||||||
*/
|
*/
|
||||||
_logger = Logging.GenLog();
|
LoadLogger();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Attempt to load our custom assemblies
|
* Attempt to load our custom assemblies
|
||||||
*/
|
*/
|
||||||
AssemblyController.Register();
|
RegisterAssemblyController();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize the Discord Client and Login
|
* Initialize the Discord Client and Login
|
||||||
*/
|
*/
|
||||||
_logger.Info($"Starting Up {ConfigurationRepository.GetValue<string>("Bot:Name")} v{ConfigurationRepository.GetValue<string>("Bot:Version")}");
|
_logger.Info($"Starting Up {ConfigurationRepository.GetValue<string>("Bot:Name")} v{ConfigurationRepository.GetValue<string>("Bot:Version")}");
|
||||||
|
|
||||||
var discordBot = DiscordConnect.StartUp();
|
|
||||||
WebServer.WebServer.Start(new string[]{});
|
var discordBot = LoadDiscord();
|
||||||
|
LoadWebServer();
|
||||||
await discordBot;
|
await discordBot;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -55,5 +55,32 @@ namespace ChaosBot
|
|||||||
_logger.Error(ex, $"Program.MainFunction: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
_logger.Error(ex, $"Program.MainFunction: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void LoadConfiguration(string appsettingsPath)
|
||||||
|
{
|
||||||
|
ConfigurationRepository.AppSettingsHandler = new ConfigurationBuilder()
|
||||||
|
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
||||||
|
.AddJsonFile(appsettingsPath, optional: false, reloadOnChange: true).Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LoadLogger()
|
||||||
|
{
|
||||||
|
_logger = Logging.GenLog();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void RegisterAssemblyController()
|
||||||
|
{
|
||||||
|
AssemblyController.RegisterAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LoadWebServer()
|
||||||
|
{
|
||||||
|
WebServer.WebServer.Start(new string[]{});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Task LoadDiscord()
|
||||||
|
{
|
||||||
|
return DiscordConnect.StartUp();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user