Delete on pick
This commit is contained in:
parent
dee07b44f4
commit
462a67e252
@ -196,5 +196,55 @@ namespace ChaosBot.Database
|
|||||||
|
|
||||||
return dt;
|
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}]>.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,9 +103,13 @@ namespace ChaosBot.Database.Repository
|
|||||||
|
|
||||||
return Controller.TransactionQuery(cmds);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,6 +160,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
private void PickRaffle(StringBuilder sb)
|
private void PickRaffle(StringBuilder sb)
|
||||||
{
|
{
|
||||||
Raffle winner = RaffleRepository.PickRandom();
|
Raffle winner = RaffleRepository.PickRandom();
|
||||||
|
RaffleRepository.Delete(winner.id);
|
||||||
sb.Append($"<@{winner.userId}> has won the raffle!");
|
sb.Append($"<@{winner.userId}> has won the raffle!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user