diff options
author | crupest <crupest@outlook.com> | 2022-12-21 12:09:58 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-21 12:09:58 +0800 |
commit | 10c32c56951fffda6657c75155c80d9b2988c263 (patch) | |
tree | 0142bc4ddf873b9d2f10e1995214c6bee03ef0f0 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud | |
parent | 13a1aef3364c3ecfed41d30ceebdcd255ae8141c (diff) | |
download | crupest-10c32c56951fffda6657c75155c80d9b2988c263.tar.gz crupest-10c32c56951fffda6657c75155c80d9b2988c263.tar.bz2 crupest-10c32c56951fffda6657c75155c80d9b2988c263.zip |
Develop secret api. v52
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs index 4489307..ea8197a 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs @@ -46,6 +46,36 @@ public class EntityJsonHelper<TEntity> where TEntity : class return JsonSerializer.Serialize(dictionary, _jsonSerializerOptions.CurrentValue); } + private static Type MapJsonValueKindToType(JsonElement jsonElement, out object? value) + { + switch (jsonElement.ValueKind) + { + case JsonValueKind.String: + { + value = jsonElement.GetString()!; + return typeof(string); + } + case JsonValueKind.Number: + { + value = jsonElement.GetDouble(); + return typeof(double); + } + case JsonValueKind.True: + case JsonValueKind.False: + { + value = jsonElement.GetBoolean(); + return typeof(bool); + } + case JsonValueKind.Null: + { + value = null; + return typeof(object); + } + default: + throw new UserException("Unsupported json value type."); + } + } + public TEntity ConvertJsonToEntityForInsert(JsonElement jsonElement) { if (jsonElement.ValueKind is not JsonValueKind.Object) @@ -54,11 +84,29 @@ public class EntityJsonHelper<TEntity> where TEntity : class var result = Activator.CreateInstance<TEntity>(); foreach (var column in _table.PropertyColumns) { - if (jsonElement.TryGetProperty(column.ColumnName, out var value)) + if (jsonElement.TryGetProperty(column.ColumnName, out var jsonValue)) { + if (column.IsGenerated) + { + throw new UserException($"Property {column.ColumnName} is auto generated, you cannot set it."); + } + + var valueType = MapJsonValueKindToType(jsonValue, out var value); + if (!valueType.IsAssignableTo(column.ColumnType.DatabaseClrType)) + { + throw new UserException($"Property {column.ColumnName} is of wrong type."); + } + var realValue = column.ColumnType.ConvertFromDatabase(value); column.PropertyInfo!.SetValue(result, realValue); } + else + { + if (!column.CanBeGenerated) + { + throw new UserException($"Property {column.ColumnName} is not auto generated, you must set it."); + } + } } return result; @@ -96,8 +144,24 @@ public class EntityJsonHelper<TEntity> where TEntity : class var result = Activator.CreateInstance<TEntity>(); foreach (var column in _table.PropertyColumns) { - if (jsonElement.TryGetProperty(column.ColumnName, out var value)) + if (jsonElement.TryGetProperty(column.ColumnName, out var jsonValue)) { + if (column.IsGenerated) + { + throw new UserException($"Property {column.ColumnName} is auto generated, you cannot set it."); + } + + if (column.IsNoUpdate) + { + throw new UserException($"Property {column.ColumnName} is not updatable, you cannot set it."); + } + + var valueType = MapJsonValueKindToType(jsonValue, out var value); + if (!valueType.IsAssignableTo(column.ColumnType.DatabaseClrType)) + { + throw new UserException($"Property {column.ColumnName} is of wrong type."); + } + var realValue = column.ColumnType.ConvertFromDatabase(value); column.PropertyInfo!.SetValue(result, realValue); } |