diff --git a/ChaosBot/Controllers/DBWork.cs b/ChaosBot/Controllers/DBWork.cs deleted file mode 100644 index 3c5ab73..0000000 --- a/ChaosBot/Controllers/DBWork.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Data; -using Microsoft.Data.Sqlite; -using Microsoft.Extensions.Configuration; -using NLog; - -namespace ChaosBot.Controllers -{ - public class DBWork - { - private static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{Program.Cfg.GetValue("Bot:Database")}"); - private static Logger _logger = Program._logger; - - public static DataTable RawQuery(string query) - { - DataTable dt = new DataTable(); - - try - { - using (_conn) - { - _conn.Open(); - - SqliteCommand cmd = _conn.CreateCommand(); - cmd.CommandText = query; - SqliteDataReader executeReader = cmd.ExecuteReader(CommandBehavior.SingleResult); - - dt.Load(executeReader); - - _conn.Close(); - } - } - catch (Exception ex) - { - _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); - } - - return dt; - } - } - - public class Raffle - { - private int _id; - private ulong userId; - } -} \ No newline at end of file diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs new file mode 100644 index 0000000..a5f0217 --- /dev/null +++ b/ChaosBot/Database/Controller.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Text; +using Microsoft.Data.Sqlite; +using Microsoft.Extensions.Configuration; +using NLog; + +namespace ChaosBot.Database +{ + public static class Controller + { + static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{Program.Cfg.GetValue("Bot:Database")}"); + private static Logger _logger = Program._logger; + + public static DataTable RawQuery(string query) + { + DataTable dt = new DataTable(); + + try + { + using (_conn) + { + _conn.Open(); + + SqliteCommand cmd = _conn.CreateCommand(); + cmd.CommandText = query; + SqliteDataReader executeReader = cmd.ExecuteReader(CommandBehavior.SingleResult); + + dt.Load(executeReader); + + _conn.Close(); + } + } + catch (Exception ex) + { + _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + + return dt; + } + + public static void InsertQuery(string table, Dictionary parameters) + { + try + { + using (_conn) + { + _conn.Open(); + + SqliteCommand cmd = _conn.CreateCommand(); + StringBuilder commandText = new StringBuilder(); + commandText.Append("INSERT INTO "); + commandText.Append(table); + commandText.Append(" ("); + + foreach (string key in parameters.Keys) + { + commandText.Append($"{key}, "); + } + + commandText.Remove(commandText.Length - 2, 2); + commandText.Append(") VALUES ("); + + foreach (string key in parameters.Keys) + { + commandText.Append($"@{key},"); + } + + commandText.Remove(commandText.Length - 1, 1); + commandText.Append(")"); + + cmd.CommandText = commandText.ToString(); + + foreach (string key in parameters.Keys) + { + cmd.Parameters.AddWithValue($"@{key}", parameters.GetValueOrDefault(key)); + } + + cmd.Prepare(); + cmd.ExecuteNonQuery(); + + _conn.Close(); + } + } + catch (Exception ex) + { + _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + } +} \ No newline at end of file diff --git a/ChaosBot/Database/Entity/Raffle.cs b/ChaosBot/Database/Entity/Raffle.cs new file mode 100644 index 0000000..fc77a43 --- /dev/null +++ b/ChaosBot/Database/Entity/Raffle.cs @@ -0,0 +1,13 @@ +namespace ChaosBot.Database.Entity +{ + public class Raffle + { + public int id { get; } + public string userId { get; private set; } + + public Raffle(string userId) + { + this.userId = userId; + } + } +} \ No newline at end of file diff --git a/ChaosBot/Database/Repository/RaffleRepository.cs b/ChaosBot/Database/Repository/RaffleRepository.cs new file mode 100644 index 0000000..5b06506 --- /dev/null +++ b/ChaosBot/Database/Repository/RaffleRepository.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using ChaosBot.Database.Entity; + +namespace ChaosBot.Database.Repository +{ + public static class RaffleRepository + { + private static readonly string Table = "RaffleTable"; + + // public static Raffle[] all() + // { + // + // } + + public static void insert(Raffle raffle) + { + Dictionary dict = new Dictionary(); + + dict.Add("userId", raffle.userId); + + Controller.InsertQuery(Table, dict); + } + + // public static void delete() + // { + // + // } + } +} \ No newline at end of file diff --git a/ChaosBot/Discord/Modules/InfoCommands.cs b/ChaosBot/Discord/Modules/InfoCommands.cs index ba7fc5e..d4bd115 100644 --- a/ChaosBot/Discord/Modules/InfoCommands.cs +++ b/ChaosBot/Discord/Modules/InfoCommands.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using System.Collections.Generic; using System.Data; using ChaosBot.Controllers; +using ChaosBot.Database; using Microsoft.Extensions.Configuration; using NLog; @@ -31,7 +32,7 @@ namespace ChaosBot.Discord.Modules sb.AppendLine(); sb.AppendLine($"Bot Version: {Program.Cfg.GetValue("Bot:Version")}"); sb.AppendLine($"Bot Prefix: {Program.Cfg.GetValue("Discord:Prefix")}"); - sb.AppendLine($"{DBWork.RawQuery("select * from RaffleTable where Id = (abs(random()) % (select (select max(Id) from RaffleTable)+1)) or rowid = (select min(Id) from RaffleTable) order by Id DESC limit 1;").Rows[0]["userId"]}"); + sb.AppendLine($"{Controller.RawQuery("select * from RaffleTable where Id = (abs(random()) % (select (select max(Id) from RaffleTable)+1)) or rowid = (select min(Id) from RaffleTable) order by Id DESC limit 1;").Rows[0]["userId"]}"); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index fc3e618..1484ad1 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -3,6 +3,9 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using ChaosBot.Database; +using ChaosBot.Database.Entity; +using ChaosBot.Database.Repository; using Discord; using Discord.Commands; using Discord.WebSocket;