From 09e5b671c8ce02aa8d2c9c405f21f90baf11f8b6 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Thu, 4 Jun 2020 02:13:35 +0200 Subject: [PATCH] Add pick method --- ChaosBot/Database/Controller.cs | 4 ++-- ChaosBot/Database/Repository/RaffleRepository.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs index 43ad76a..47f9e83 100644 --- a/ChaosBot/Database/Controller.cs +++ b/ChaosBot/Database/Controller.cs @@ -122,11 +122,11 @@ namespace ChaosBot.Database string order = null; if (orderByKey != null) { - order = $"ORDER BY ${orderByKey}"; + order = $"ORDER BY {orderByKey}"; } string query = $"SELECT {selectColumns} FROM {table} {filter} {order}"; - + cmd.CommandText = query; cmd.Prepare(); SqliteDataReader executeReader = cmd.ExecuteReader(); diff --git a/ChaosBot/Database/Repository/RaffleRepository.cs b/ChaosBot/Database/Repository/RaffleRepository.cs index b8a23f4..ed6d2da 100644 --- a/ChaosBot/Database/Repository/RaffleRepository.cs +++ b/ChaosBot/Database/Repository/RaffleRepository.cs @@ -65,6 +65,17 @@ namespace ChaosBot.Database.Repository } } + public static Raffle PickRandom() + { + DataTable dataTable = Controller.SelectQuery(Table, "*", orderByKey: "RANDOM()"); + + if (dataTable.Rows.Count == 0) return null; + DataRow row = dataTable.Rows[0]; + int idFetch = Convert.ToInt32((long)row["id"]); + string userIdFetch = row["userId"].ToString(); + return new Raffle(idFetch, userIdFetch); + } + // public static void delete() // { //