Fix upsert

This commit is contained in:
Daniel_I_Am 2020-10-13 01:35:48 +02:00
parent 8d072d48eb
commit 89175ef9da
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
4 changed files with 50 additions and 25 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Dynamic; using System.Dynamic;
using System.Linq; using System.Linq;
@ -64,14 +65,23 @@ namespace ChaosBot.WebServer.App.ApiControllers
await using ChaosbotContext dbContext = new ChaosbotContext(); await using ChaosbotContext dbContext = new ChaosbotContext();
T databaseObject = new T(); T databaseObject = SetDefaultFieldsForUpsert(new T(), guildId);
foreach (string key in GetValidationRules().Keys) foreach (string key in GetValidationRules().Keys)
{ {
Type type = databaseObject?.GetType()?.GetProperty(key)?.PropertyType;
if (type == null) continue;
dynamic value;
if (type.IsEnum)
value = Convert.ChangeType(Enum.ToObject(type, JsonElementHelper.GetValueFromRequest(requestBody, key)), type);
else
value = Convert.ChangeType(JsonElementHelper.GetValueFromRequest(requestBody, key), type);
databaseObject databaseObject
.GetType() .GetType()
.GetProperty(key) .GetProperty(key)
?.SetValue(databaseObject, requestBody.GetType().GetProperty(key)?.GetValue(requestBody, null)); ?.SetValue(databaseObject, value);
} }
await ApplyFilterForUpsert(GetBasicQuery(dbContext).Upsert(databaseObject)).RunAsync(); await ApplyFilterForUpsert(GetBasicQuery(dbContext).Upsert(databaseObject)).RunAsync();
@ -110,6 +120,7 @@ namespace ChaosBot.WebServer.App.ApiControllers
protected abstract UpsertCommandBuilder<T> ApplyFilterForUpsert(UpsertCommandBuilder<T> builder); protected abstract UpsertCommandBuilder<T> ApplyFilterForUpsert(UpsertCommandBuilder<T> builder);
protected abstract T FilterQueryForDeletion(IQueryable<T> query, ulong guildId, TDeleteParameter deleteParameter); protected abstract T FilterQueryForDeletion(IQueryable<T> query, ulong guildId, TDeleteParameter deleteParameter);
protected abstract List<T> FilterQueryMultipleForDeletion(IQueryable<T> query, ulong guildId, TDeleteParameter deleteParameter); protected abstract List<T> FilterQueryMultipleForDeletion(IQueryable<T> query, ulong guildId, TDeleteParameter deleteParameter);
protected abstract T SetDefaultFieldsForUpsert(T obj, ulong guildId);
private class DynamicResponse : DynamicObject private class DynamicResponse : DynamicObject
{ {

View File

@ -93,5 +93,11 @@ namespace ChaosBot.WebServer.App.ApiControllers
{ {
return new List<CustomCommand>(); return new List<CustomCommand>();
} }
protected override CustomCommand SetDefaultFieldsForUpsert(CustomCommand obj, ulong guildId)
{
obj.DiscordGuildId = guildId;
return obj;
}
} }
} }

View File

@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Text.Json;
namespace ChaosBot.WebServer.Services
{
public static class JsonElementHelper
{
public static dynamic GetValueFromRequest(JsonElement requestBody, string key)
{
JsonElement prop;
try
{
prop = requestBody.GetProperty(key);
}
catch (KeyNotFoundException)
{
return null;
}
return prop.ValueKind switch
{
JsonValueKind.String => prop.GetString(),
JsonValueKind.Number => prop.GetInt64(),
JsonValueKind.True => prop.GetBoolean(),
JsonValueKind.False => prop.GetBoolean(),
_ => null
};
}
}
}

View File

@ -15,7 +15,7 @@ namespace ChaosBot.WebServer.Services
foreach (KeyValuePair<string,List<string>> validationRule in getValidationRules) foreach (KeyValuePair<string,List<string>> validationRule in getValidationRules)
{ {
string key = validationRule.Key; string key = validationRule.Key;
dynamic value = GetValueFromRequest(requestBody, key); dynamic value = JsonElementHelper.GetValueFromRequest(requestBody, key);
List<string> rules = validationRule.Value; List<string> rules = validationRule.Value;
@ -48,28 +48,6 @@ namespace ChaosBot.WebServer.Services
return error.Length == 0; return error.Length == 0;
} }
private static dynamic GetValueFromRequest(JsonElement requestBody, string key)
{
JsonElement prop;
try
{
prop = requestBody.GetProperty(key);
}
catch (KeyNotFoundException)
{
return null;
}
return prop.ValueKind switch
{
JsonValueKind.String => prop.GetString(),
JsonValueKind.Number => prop.GetInt64(),
JsonValueKind.True => prop.GetBoolean(),
JsonValueKind.False => prop.GetBoolean(),
_ => null
};
}
private ValidationResult CheckRequired(string key, dynamic value) private ValidationResult CheckRequired(string key, dynamic value)
{ {
if (value != null) if (value != null)