aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-12 20:30:55 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:53 +0800
commitd8f12e14f3cebd759a8ee911cabb9585063606cb (patch)
tree5612df6f003faeb2b39c063c7fb3b09c7acb7e4a /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
parentce8adb2c5ff7d71592a4173eb06391b01cc45f22 (diff)
downloadcrupest-d8f12e14f3cebd759a8ee911cabb9585063606cb.tar.gz
crupest-d8f12e14f3cebd759a8ee911cabb9585063606cb.tar.bz2
crupest-d8f12e14f3cebd759a8ee911cabb9585063606cb.zip
Develop secret api. v31
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs49
1 files changed, 34 insertions, 15 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
index a1e4583..6f2f446 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
@@ -1,36 +1,55 @@
+using System.Diagnostics;
using System.Text.Json;
+using Microsoft.Extensions.Options;
namespace CrupestApi.Commons.Crud;
-public class EntityJsonHelper
+// TODO: Register this.
+/// <summary>
+/// Contains all you need to do with json.
+/// </summary>
+public class EntityJsonHelper<TEntity> where TEntity : class
{
private readonly TableInfo _table;
+ private readonly JsonSerializerOptions _jsonSerializerOptions;
- public EntityJsonHelper(TableInfo table)
+ public EntityJsonHelper(TableInfoFactory tableInfoFactory)
{
- _table = table;
+ _table = tableInfoFactory.Get(typeof(TEntity));
+ _jsonSerializerOptions = new JsonSerializerOptions();
+ _jsonSerializerOptions.AllowTrailingCommas = true;
+ _jsonSerializerOptions.PropertyNameCaseInsensitive = true;
+ _jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
+ foreach (var type in _table.Columns.Select(c => c.ColumnType))
+ {
+ if (type.JsonConverter is not null)
+ {
+ _jsonSerializerOptions.Converters.Add(type.JsonConverter);
+ }
+ }
}
public virtual JsonDocument ConvertEntityToJson(object? entity)
{
- if (entity is null) return JsonSerializer.SerializeToDocument<object?>(null);
+ Debug.Assert(entity is null || entity is TEntity);
+ return JsonSerializer.SerializeToDocument<TEntity?>((TEntity?)entity, _jsonSerializerOptions);
+ }
- var result = new Dictionary<string, object?>();
+ public virtual TEntity? ConvertJsonToEntity(JsonDocument json)
+ {
+ var entity = json.Deserialize<TEntity>();
+ if (entity is null) return null;
- foreach (var column in _table.ColumnInfos)
+ foreach (var column in _table.Columns)
{
- if (column.PropertyInfo is not null)
+ var propertyValue = column.PropertyInfo?.GetValue(entity);
+
+ if ((column.IsAutoIncrement || column.IsGenerated) && propertyValue is not null)
{
- result.Add(column.ColumnName, column.PropertyInfo.GetValue(entity));
+ throw new Exception("You can't specify this property because it is auto generated.");
}
}
- return JsonSerializer.SerializeToDocument(result);
- }
-
- public virtual object? ConvertJsonToEntity(JsonDocument? json)
- {
- // TODO: Implement this.
- throw new NotImplementedException();
+ return entity;
}
}