From 3c682780a43e89b7e76baee15f7eb75d1519b6dd Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Mon, 8 Jun 2020 21:56:58 +0200 Subject: [PATCH] Fix multiple where conditions and nullable fields on models --- ChaosBot/Attribute/AssemblyController.cs | 13 ++++++++++++- ChaosBot/Database/Entity/Points.cs | 3 ++- ChaosBot/Database/Entity/Raffle.cs | 3 ++- ChaosBot/Database/QueryBuilderRaw.cs | 5 +++-- ChaosBot/Discord/Modules/RaffleSystem.cs | 3 ++- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ChaosBot/Attribute/AssemblyController.cs b/ChaosBot/Attribute/AssemblyController.cs index 2532b88..59d2364 100644 --- a/ChaosBot/Attribute/AssemblyController.cs +++ b/ChaosBot/Attribute/AssemblyController.cs @@ -36,7 +36,18 @@ namespace ChaosBot.Attribute foreach (PropertyInfo prop in type.GetProperties()) { string columnName = prop.Name; - string columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString(); + string columnType = null; + if (prop.PropertyType.IsGenericType && + prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + Type[] typeArguments = prop.PropertyType.GetGenericArguments(); + if (typeArguments.Length == 1) + columnType = DBEntity.DataTypes.GetValueOrDefault(typeArguments[0]).ToString(); + } + else + { + columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString(); + } StringBuilder constraintNameBuilder = new StringBuilder($"{table}_{columnName}"); List constraintsList = new List(); diff --git a/ChaosBot/Database/Entity/Points.cs b/ChaosBot/Database/Entity/Points.cs index b6803df..c30de71 100644 --- a/ChaosBot/Database/Entity/Points.cs +++ b/ChaosBot/Database/Entity/Points.cs @@ -1,3 +1,4 @@ +using System; using System.Data; using ChaosBot.Attribute; @@ -10,7 +11,7 @@ namespace ChaosBot.Database.Entity [DBAutoIncrement] [DBNotNull] [DBUnique] - public int id { get; private set; } + public Nullable id { get; private set; } public int points { get; private set; } public long userId { get; private set; } public long guildId { get; private set; } diff --git a/ChaosBot/Database/Entity/Raffle.cs b/ChaosBot/Database/Entity/Raffle.cs index d3da395..399de4f 100644 --- a/ChaosBot/Database/Entity/Raffle.cs +++ b/ChaosBot/Database/Entity/Raffle.cs @@ -1,3 +1,4 @@ +using System; using System.Data; using ChaosBot.Attribute; @@ -10,7 +11,7 @@ namespace ChaosBot.Database.Entity [DBAutoIncrement] [DBNotNull] [DBUnique] - public int id { get; private set; } + public Nullable id { get; private set; } public long userId { get; private set; } public long guildId { get; private set; } diff --git a/ChaosBot/Database/QueryBuilderRaw.cs b/ChaosBot/Database/QueryBuilderRaw.cs index 78e915d..2300a87 100644 --- a/ChaosBot/Database/QueryBuilderRaw.cs +++ b/ChaosBot/Database/QueryBuilderRaw.cs @@ -117,13 +117,14 @@ namespace ChaosBot.Database public QueryBuilderRaw Where(Dictionary whereCondition) { Append("WHERE"); + List whereConditionList = new List(); foreach (string key in whereCondition.Keys) { FilterValue value = whereCondition.GetValueOrDefault(key); string parameterName = AddParameterAndReturnKey(key, value.Value); - Append($"{key} {value.Comparison ?? "="} {parameterName}"); + whereConditionList.Add($"{key} {value.Comparison ?? "="} {parameterName}"); } - + Append(string.Join(" AND ", whereConditionList)); return this; } diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index 703688f..6158c29 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -157,7 +157,8 @@ namespace ChaosBot.Discord.Modules private void PickRaffle(StringBuilder sb) { Raffle winner = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id)); - RaffleRepository.Delete(winner.id); + if (winner.id != null) + RaffleRepository.Delete((int)winner.id); sb.Append($"<@{winner.userId}> has won the raffle!"); }