diff --git a/ChaosBot/Database/Entity/Raffle.cs b/ChaosBot/Database/Entity/Raffle.cs index 9cc2e65..443398a 100644 --- a/ChaosBot/Database/Entity/Raffle.cs +++ b/ChaosBot/Database/Entity/Raffle.cs @@ -4,14 +4,15 @@ namespace ChaosBot.Database.Entity { public int id { get; } public string userId { get; private set; } + public string guildId { get; private set; } - public Raffle(int id, string userId) + public Raffle(int id, string userId, string guildId) { this.id = id; this.userId = userId; } - public Raffle(string userId) + public Raffle(string userId, string guildId) { this.userId = userId; } diff --git a/ChaosBot/Database/Repository/RaffleRepository.cs b/ChaosBot/Database/Repository/RaffleRepository.cs index ae9fe2d..b7c3204 100644 --- a/ChaosBot/Database/Repository/RaffleRepository.cs +++ b/ChaosBot/Database/Repository/RaffleRepository.cs @@ -20,8 +20,29 @@ namespace ChaosBot.Database.Repository { int id = Convert.ToInt32((long) row["id"]); string userId = row["userId"].ToString(); + string guildId = row["guildId"].ToString(); - raffles.Add(new Raffle(id, userId)); + raffles.Add(new Raffle(id, userId, guildId)); + } + + return raffles.ToArray(); + } + + public static Raffle[] All(string guildId) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("guildId", guildId); + + DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict); + + List raffles = new List(); + foreach (DataRow row in dataTable.Rows) + { + int idFetch = Convert.ToInt32((long) row["id"]); + string userIdFetch = row["userId"].ToString(); + string guildIdFetch = row["guildId"].ToString(); + + raffles.Add(new Raffle(idFetch, userIdFetch, guildIdFetch)); } return raffles.ToArray(); @@ -34,17 +55,28 @@ namespace ChaosBot.Database.Repository return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]); } - public static int Count(string userId) + public static int Count(string guildId) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("guildId", guildId); + + DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict); + + return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]); + } + + public static int Count(string userId, string guildId) { Dictionary filterDict = new Dictionary(); filterDict.Add("userId", userId); + filterDict.Add("guildId", guildId); DataTable dataTable = Controller.SelectQuery(Table, "COUNT(*)", filterDict); return Convert.ToInt32(dataTable.Rows[0]["COUNT(*)"]); } - public static Raffle[] selectUser(string userId) + public static Raffle[] SelectUser(string userId) { Dictionary filterDict = new Dictionary(); filterDict.Add("userId", userId); @@ -56,8 +88,30 @@ namespace ChaosBot.Database.Repository { int id = Convert.ToInt32((long) row["id"]); string userIdFetch = row["userId"].ToString(); + string guildIdFetch = row["guildId"].ToString(); - raffles.Add(new Raffle(id, userIdFetch)); + raffles.Add(new Raffle(id, userIdFetch, guildIdFetch)); + } + + return raffles.ToArray(); + } + + public static Raffle[] SelectUser(string userId, string guildId) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("userId", userId); + filterDict.Add("guildId", guildId); + + DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict); + + List raffles = new List(); + foreach (DataRow row in dataTable.Rows) + { + int id = Convert.ToInt32((long) row["id"]); + string userIdFetch = row["userId"].ToString(); + string guildIdFetch = row["guildId"].ToString(); + + raffles.Add(new Raffle(id, userIdFetch, guildIdFetch)); } return raffles.ToArray(); @@ -68,6 +122,7 @@ namespace ChaosBot.Database.Repository Dictionary dict = new Dictionary(); dict.Add("userId", raffle.userId); + dict.Add("guildId", raffle.guildId); Controller.InsertQuery(Table, dict); } @@ -79,29 +134,34 @@ namespace ChaosBot.Database.Repository Dictionary dict = new Dictionary(); dict.Add("userId", raf.userId); + dict.Add("guildId", raf.guildId); + Controller.InsertQuery(Table, dict); } } - public static Raffle PickRandom() + public static Raffle PickRandom(string guildId) { - DataTable dataTable = Controller.SelectQuery(Table, "*", orderByKey: "RANDOM()"); + Dictionary filterDict = new Dictionary(); + filterDict.Add("guildId", guildId); + + DataTable dataTable = Controller.SelectQuery(Table, "*", filterDict, "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); + string guildIdFetch = row["guildId"].ToString(); + return new Raffle(idFetch, userIdFetch, guildIdFetch); } - public static int ClearRaffle() + public static void ClearRaffle(string guildId) { - List cmds = new List(); + Dictionary filterDict = new Dictionary(); - cmds.Add(new SqliteCommand("delete from RaffleTable;")); - cmds.Add(new SqliteCommand("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE name = 'RaffleTable';")); - - return Controller.TransactionQuery(cmds); + filterDict.Add("guildId", guildId); + + Controller.DeleteQuery(Table, filterDict); } public static void Delete(int id) diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index 52ccf2d..ac6d211 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -152,23 +152,23 @@ namespace ChaosBot.Discord.Modules sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to <@{userId}>."); sb.AppendLine(); - sb.AppendLine($"<@{userId}> now has {RaffleRepository.Count(userId.ToString())} rafflepoints!"); + sb.AppendLine($"<@{userId}> now has {RaffleRepository.Count(userId.ToString(), Context.Guild.Id.ToString())} rafflepoints!"); } private void PickRaffle(StringBuilder sb) { Raffle winner = RaffleRepository.PickRandom(Context.Guild.Id.ToString()); - RaffleRepository.Delete(winner.id, Context.Guild.Id.ToString()); + RaffleRepository.Delete(winner.id); sb.Append($"<@{winner.userId}> has won the raffle!"); } private void ClearRaffle(StringBuilder sb, bool noOutput = false) { - int Removed = RaffleRepository.Count(); + int removed = RaffleRepository.Count(); RaffleRepository.ClearRaffle(Context.Guild.Id.ToString()); - sb.AppendLine($"{Context.User.Mention} has cleared all {Removed} rafflepoints"); + sb.AppendLine($"{Context.User.Mention} has cleared all {removed} rafflepoints"); } private void StatusRaffle(StringBuilder sb, string user)