Delete on pick

This commit is contained in:
Daniel_I_Am 2020-06-04 02:55:09 +02:00
parent dee07b44f4
commit 462a67e252
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 59 additions and 4 deletions

View File

@ -196,5 +196,55 @@ namespace ChaosBot.Database
return dt;
}
public static void DeleteQuery(string table, Dictionary<string, object> 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<string> filterList = new List<string>();
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}]>.");
}
}
}
}

View File

@ -103,9 +103,13 @@ namespace ChaosBot.Database.Repository
return Controller.TransactionQuery(cmds);
}
// public static void Delete()
// {
//
// }
public static void Delete(int id)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("id", id);
Controller.DeleteQuery(Table, filterDict);
}
}
}

View File

@ -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!");
}