Fix upsert
This commit is contained in:
parent
8d072d48eb
commit
89175ef9da
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
@ -64,14 +65,23 @@ namespace ChaosBot.WebServer.App.ApiControllers
|
||||
|
||||
await using ChaosbotContext dbContext = new ChaosbotContext();
|
||||
|
||||
T databaseObject = new T();
|
||||
T databaseObject = SetDefaultFieldsForUpsert(new T(), guildId);
|
||||
|
||||
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
|
||||
.GetType()
|
||||
.GetProperty(key)
|
||||
?.SetValue(databaseObject, requestBody.GetType().GetProperty(key)?.GetValue(requestBody, null));
|
||||
?.SetValue(databaseObject, value);
|
||||
}
|
||||
|
||||
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 T FilterQueryForDeletion(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
|
||||
{
|
||||
|
||||
@ -93,5 +93,11 @@ namespace ChaosBot.WebServer.App.ApiControllers
|
||||
{
|
||||
return new List<CustomCommand>();
|
||||
}
|
||||
|
||||
protected override CustomCommand SetDefaultFieldsForUpsert(CustomCommand obj, ulong guildId)
|
||||
{
|
||||
obj.DiscordGuildId = guildId;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
ChaosBot/WebServer/Services/JsonElementHelper.cs
Normal file
30
ChaosBot/WebServer/Services/JsonElementHelper.cs
Normal 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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -15,7 +15,7 @@ namespace ChaosBot.WebServer.Services
|
||||
foreach (KeyValuePair<string,List<string>> validationRule in getValidationRules)
|
||||
{
|
||||
string key = validationRule.Key;
|
||||
dynamic value = GetValueFromRequest(requestBody, key);
|
||||
dynamic value = JsonElementHelper.GetValueFromRequest(requestBody, key);
|
||||
|
||||
List<string> rules = validationRule.Value;
|
||||
|
||||
@ -48,28 +48,6 @@ namespace ChaosBot.WebServer.Services
|
||||
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)
|
||||
{
|
||||
if (value != null)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user