aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs51
1 files changed, 31 insertions, 20 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
index e893638..1265fe9 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
@@ -93,36 +93,20 @@ public class EntityJsonHelper<TEntity> where TEntity : class
return ConvertJsonElementToInsertClauses(document.RootElement);
}
- public IUpdateClause ConvertJsonElementToUpdateClause(JsonDocument json)
+ public IUpdateClause ConvertJsonElementToUpdateClause(JsonElement rootElement, bool saveNull)
{
var updateClause = UpdateClause.Create();
- if (json.RootElement.ValueKind != JsonValueKind.Object)
+ if (rootElement.ValueKind != JsonValueKind.Object)
{
throw new UserException("The root element must be an object.");
}
- bool saveNull = false;
-
- if (json.RootElement.TryGetProperty("$saveNull", out var propertyElement))
- {
- if (propertyElement.ValueKind is not JsonValueKind.True or JsonValueKind.False)
- {
- throw new UserException("$saveNull can only be true or false.");
- }
-
- if (propertyElement.ValueKind is JsonValueKind.True)
- {
- saveNull = true;
- }
- }
-
-
foreach (var column in _table.PropertyColumns)
{
object? value = null;
- if (json.RootElement.TryGetProperty(column.ColumnName, out propertyElement))
+ if (rootElement.TryGetProperty(column.ColumnName, out var propertyElement))
{
value = propertyElement.ValueKind switch
{
@@ -151,9 +135,36 @@ public class EntityJsonHelper<TEntity> where TEntity : class
return updateClause;
}
+ public IUpdateClause ConvertJsonElementToUpdateClause(JsonElement rootElement)
+ {
+ var updateClause = UpdateClause.Create();
+
+ if (rootElement.ValueKind != JsonValueKind.Object)
+ {
+ throw new UserException("The root element must be an object.");
+ }
+
+ bool saveNull = false;
+
+ if (rootElement.TryGetProperty("$saveNull", out var propertyElement))
+ {
+ if (propertyElement.ValueKind is not JsonValueKind.True or JsonValueKind.False)
+ {
+ throw new UserException("$saveNull can only be true or false.");
+ }
+
+ if (propertyElement.ValueKind is JsonValueKind.True)
+ {
+ saveNull = true;
+ }
+ }
+
+ return ConvertJsonElementToUpdateClause(rootElement, saveNull);
+ }
+
public IUpdateClause ConvertJsonToUpdateClause(string json)
{
var document = JsonSerializer.Deserialize<JsonDocument>(json, _jsonSerializerOptions.CurrentValue)!;
- return ConvertJsonElementToUpdateClause(document);
+ return ConvertJsonElementToUpdateClause(document.RootElement);
}
}