diff options
author | crupest <crupest@outlook.com> | 2022-12-21 11:51:25 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-21 11:51:25 +0800 |
commit | 13a1aef3364c3ecfed41d30ceebdcd255ae8141c (patch) | |
tree | 411350f425f767fe6a8b3d1f753c713ea2b0a0e6 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs | |
parent | 4264d8135c066081dbabb412db17bf537ceab86e (diff) | |
download | crupest-13a1aef3364c3ecfed41d30ceebdcd255ae8141c.tar.gz crupest-13a1aef3364c3ecfed41d30ceebdcd255ae8141c.tar.bz2 crupest-13a1aef3364c3ecfed41d30ceebdcd255ae8141c.zip |
Develop secret api. v51
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs | 124 |
1 files changed, 34 insertions, 90 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs index 1265fe9..4489307 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs @@ -17,7 +17,7 @@ public class EntityJsonHelper<TEntity> where TEntity : class _jsonSerializerOptions = jsonSerializerOptions; } - public virtual Dictionary<string, object?> ConvertEntityToDictionary(TEntity entity, bool includeNonColumnProperties = false) + public Dictionary<string, object?> ConvertEntityToDictionary(TEntity entity, bool includeNonColumnProperties = false) { var result = new Dictionary<string, object?>(); @@ -40,131 +40,75 @@ public class EntityJsonHelper<TEntity> where TEntity : class return result; } - public virtual string ConvertEntityToJson(TEntity entity, bool includeNonColumnProperties = false) + public string ConvertEntityToJson(TEntity entity, bool includeNonColumnProperties = false) { var dictionary = ConvertEntityToDictionary(entity, includeNonColumnProperties); return JsonSerializer.Serialize(dictionary, _jsonSerializerOptions.CurrentValue); } - public virtual IInsertClause ConvertJsonElementToInsertClauses(JsonElement rootElement) + public TEntity ConvertJsonToEntityForInsert(JsonElement jsonElement) { - var insertClause = InsertClause.Create(); - - if (rootElement.ValueKind != JsonValueKind.Object) - { - throw new UserException("The root element must be an object."); - } + if (jsonElement.ValueKind is not JsonValueKind.Object) + throw new ArgumentException("The jsonElement must be an object."); + var result = Activator.CreateInstance<TEntity>(); foreach (var column in _table.PropertyColumns) { - object? value = null; - if (rootElement.TryGetProperty(column.ColumnName, out var propertyElement)) - { - value = propertyElement.ValueKind switch - { - JsonValueKind.Null or JsonValueKind.Undefined => null, - JsonValueKind.Number => propertyElement.GetDouble(), - JsonValueKind.True => true, - JsonValueKind.False => false, - JsonValueKind.String => propertyElement.GetString(), - _ => throw new Exception($"Bad json value of property {column.ColumnName}.") - }; - } - - if (column.IsGenerated && value is not null) - { - throw new UserException($"The property {column.ColumnName} is generated. You cannot specify its value."); - } - - if (column.IsNotNull && !column.CanBeGenerated && value is null) + if (jsonElement.TryGetProperty(column.ColumnName, out var value)) { - throw new UserException($"The property {column.ColumnName} can't be null or generated. But you specify a null value."); + var realValue = column.ColumnType.ConvertFromDatabase(value); + column.PropertyInfo!.SetValue(result, realValue); } - - insertClause.Add(column.ColumnName, value); } - return insertClause; + return result; } - public IInsertClause ConvertJsonToInsertClauses(string json) + public TEntity ConvertJsonToEntityForInsert(string json) { - var document = JsonSerializer.Deserialize<JsonDocument>(json, _jsonSerializerOptions.CurrentValue)!; - return ConvertJsonElementToInsertClauses(document.RootElement); + var jsonElement = JsonSerializer.Deserialize<JsonElement>(json, _jsonSerializerOptions.CurrentValue); + return ConvertJsonToEntityForInsert(jsonElement!); } - public IUpdateClause ConvertJsonElementToUpdateClause(JsonElement rootElement, bool saveNull) + public TEntity ConvertJsonToEntityForUpdate(JsonElement jsonElement, out UpdateBehavior updateBehavior) { - var updateClause = UpdateClause.Create(); + if (jsonElement.ValueKind is not JsonValueKind.Object) + throw new UserException("The jsonElement must be an object."); - if (rootElement.ValueKind != JsonValueKind.Object) - { - throw new UserException("The root element must be an object."); - } + updateBehavior = UpdateBehavior.None; - foreach (var column in _table.PropertyColumns) + if (jsonElement.TryGetProperty("$saveNull", out var saveNullValue)) { - object? value = null; - - if (rootElement.TryGetProperty(column.ColumnName, out var propertyElement)) + if (saveNullValue.ValueKind is JsonValueKind.True) { - value = propertyElement.ValueKind switch - { - JsonValueKind.Null or JsonValueKind.Undefined => null, - JsonValueKind.Number => propertyElement.GetDouble(), - JsonValueKind.True => true, - JsonValueKind.False => false, - JsonValueKind.String => propertyElement.GetString(), - _ => throw new Exception($"Bad json value of property {column.ColumnName}.") - }; - - if (column.IsNoUpdate && (value is not null || saveNull)) - { - throw new UserException($"The property {column.ColumnName} is not updatable. You cannot specify its value."); - } + updateBehavior |= UpdateBehavior.SaveNull; } + else if (saveNullValue.ValueKind is JsonValueKind.False) + { - if (value is null && !saveNull) + } + else { - continue; + throw new UserException("The $saveNull must be a boolean."); } - - updateClause.Add(column.ColumnName, value ?? DbNullValue.Instance); } - 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)) + var result = Activator.CreateInstance<TEntity>(); + foreach (var column in _table.PropertyColumns) { - if (propertyElement.ValueKind is not JsonValueKind.True or JsonValueKind.False) + if (jsonElement.TryGetProperty(column.ColumnName, out var value)) { - throw new UserException("$saveNull can only be true or false."); - } - - if (propertyElement.ValueKind is JsonValueKind.True) - { - saveNull = true; + var realValue = column.ColumnType.ConvertFromDatabase(value); + column.PropertyInfo!.SetValue(result, realValue); } } - return ConvertJsonElementToUpdateClause(rootElement, saveNull); + return result; } - public IUpdateClause ConvertJsonToUpdateClause(string json) + public TEntity ConvertJsonToEntityForUpdate(string json, out UpdateBehavior updateBehavior) { - var document = JsonSerializer.Deserialize<JsonDocument>(json, _jsonSerializerOptions.CurrentValue)!; - return ConvertJsonElementToUpdateClause(document.RootElement); + var jsonElement = JsonSerializer.Deserialize<JsonElement>(json, _jsonSerializerOptions.CurrentValue); + return ConvertJsonToEntityForUpdate(jsonElement!, out updateBehavior); } } |