diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs index 8c03439..f7599fc 100644 --- a/ChaosBot/Database/Controller.cs +++ b/ChaosBot/Database/Controller.cs @@ -196,5 +196,55 @@ namespace ChaosBot.Database return dt; } + + public static void DeleteQuery(string table, Dictionary filterColumns = null, string orderByKey = null, int limit = -1, int offset = -1) + { + try + { + using (_conn) + { + _conn.Open(); + SqliteCommand cmd = _conn.CreateCommand(); + + string filter = null; + if (filterColumns != null) + { + List filterList = new List(); + foreach (string key in filterColumns.Keys) + { + filterList.Add($"{key} = @{key}"); + } + + filter = $"WHERE {Strings.Join(filterList.ToArray(), " AND ")}"; + + foreach (string key in filterColumns.Keys) + { + cmd.Parameters.AddWithValue($@"{key}", filterColumns.GetValueOrDefault(key)); + } + } + + string order = null; + if (orderByKey != null) + { + order = $"ORDER BY {orderByKey}"; + } + + string limitString = limit > 0 ? $"LIMIT {limit}" : null; + string offsetString = offset > 0 ? $"OFFSET {offset}" : null; + + string query = $"DELETE FROM {table} {filter} {order} {limitString} {offsetString}"; + + cmd.CommandText = query; + 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/Repository/RaffleRepository.cs b/ChaosBot/Database/Repository/RaffleRepository.cs index 3c3710f..ae9fe2d 100644 --- a/ChaosBot/Database/Repository/RaffleRepository.cs +++ b/ChaosBot/Database/Repository/RaffleRepository.cs @@ -103,9 +103,13 @@ namespace ChaosBot.Database.Repository return Controller.TransactionQuery(cmds); } - // public static void Delete() - // { - // - // } + + public static void Delete(int id) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("id", id); + + Controller.DeleteQuery(Table, filterDict); + } } } \ No newline at end of file diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index 507115f..daeb3b7 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -160,6 +160,7 @@ namespace ChaosBot.Discord.Modules private void PickRaffle(StringBuilder sb) { Raffle winner = RaffleRepository.PickRandom(); + RaffleRepository.Delete(winner.id); sb.Append($"<@{winner.userId}> has won the raffle!"); }