diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs index 47f9e83..a7bf720 100644 --- a/ChaosBot/Database/Controller.cs +++ b/ChaosBot/Database/Controller.cs @@ -91,6 +91,56 @@ namespace ChaosBot.Database } } + public static int TransactionQuery(List cmds) + { + SqliteCommand command = _conn.CreateCommand(); + SqliteTransaction transaction; + + // Start a local transaction. + transaction = _conn.BeginTransaction(); + + // Must assign both transaction object and connection + // to Command object for a pending local transaction + command.Connection = _conn; + command.Transaction = transaction; + + try + { + foreach (var cmd in cmds) + { + command.CommandText = cmd.ToString(); + command.ExecuteNonQuery(); + } + + // Attempt to commit the transaction. + transaction.Commit(); + _logger.Info($"{cmds.Count} record(s) are written to database."); + } + catch (Exception ex) + { + _logger.Warn("Commit Exception Type: {0}", ex.GetType()); + _logger.Warn(" Message: {0}", ex.Message); + + // Attempt to roll back the transaction. + try + { + transaction.Rollback(); + } + catch (Exception ex2) + { + // This catch block will handle any errors that may have occurred + // on the server that would cause the rollback to fail, such as + // a closed connection. + _logger.Warn("Rollback Exception Type: {0}", ex2.GetType()); + _logger.Warn(" Message: {0}", ex2.Message); + } + + return 0; + } + + return cmds.Count; + } + public static DataTable SelectQuery(string table, string selectColumns = "*", Dictionary filterColumns = null, string orderByKey = null) { DataTable dt = new DataTable(); diff --git a/ChaosBot/Database/Repository/RaffleRepository.cs b/ChaosBot/Database/Repository/RaffleRepository.cs index dab4711..d53e825 100644 --- a/ChaosBot/Database/Repository/RaffleRepository.cs +++ b/ChaosBot/Database/Repository/RaffleRepository.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Data; using System.Linq; using ChaosBot.Database.Entity; +using Microsoft.Data.Sqlite; namespace ChaosBot.Database.Repository { @@ -93,6 +94,15 @@ namespace ChaosBot.Database.Repository return new Raffle(idFetch, userIdFetch); } + public static int clearRaffle() + { + List cmds = new List(); + + cmds.Add(new SqliteCommand("delete from RaffleTable;")); + cmds.Add(new SqliteCommand("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE name = 'RaffleTable';")); + + return Controller.TransactionQuery(cmds); + } // public static void delete() // { // diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index e0e62f7..c0655bc 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -167,11 +167,9 @@ namespace ChaosBot.Discord.Modules private void ClearRaffle(StringBuilder sb, bool noOutput = false) { - _currentPot.Clear(); - - if (noOutput) return; + int Removed = RaffleRepository.clearRaffle(); - sb.AppendLine($"{Context.User.Mention} has cleared all rafflepoints"); + sb.AppendLine($"{Context.User.Mention} has cleared all {Removed} rafflepoints"); } private void StatusRaffle(StringBuilder sb, string user)