diff options
author | crupest <crupest@outlook.com> | 2022-12-12 20:38:49 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:53 +0800 |
commit | bc2a3db855cca16d4f66b23f814528671b5d8591 (patch) | |
tree | 8ff8b574c550e9df2dda8108a09a392636b54075 /docker/crupest-api/CrupestApi | |
parent | d8f12e14f3cebd759a8ee911cabb9585063606cb (diff) | |
download | crupest-bc2a3db855cca16d4f66b23f814528671b5d8591.tar.gz crupest-bc2a3db855cca16d4f66b23f814528671b5d8591.tar.bz2 crupest-bc2a3db855cca16d4f66b23f814528671b5d8591.zip |
Develop secret api. v32
Diffstat (limited to 'docker/crupest-api/CrupestApi')
3 files changed, 16 insertions, 63 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 7944c18..5d73002 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -38,50 +38,4 @@ public class CrudService<TEntity> : IDisposable where TEntity : class { _dbConnection.Dispose(); } - - public List<TEntity> Select(IWhereClause? filter) - { - return _table.Select<TEntity>(_dbConnection, null, filter).ToList(); - } - - public bool Exists(IWhereClause? filter) - { - return _table.SelectCount(_dbConnection, filter) > 0; - } - - public int Count(IWhereClause? filter) - { - return _table.SelectCount(_dbConnection, filter); - } - - // Return the key. - public object Insert(IInsertClause insertClause) - { - return _table.Insert(_dbConnection, insertClause); - } - - public int Update(IUpdateClause updateClause, IWhereClause? filter) - { - return _table.Update(_dbConnection, filter, updateClause); - } - - public int Delete(IWhereClause? filter) - { - return _table.Delete(_dbConnection, filter); - } - - public TEntity SelectByKey(object key) - { - return Select(WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)).Single(); - } - - public List<JsonDocument> SelectAsJson(IWhereClause? filter) - { - return Select(filter).Select(e => _jsonHelper.ConvertEntityToJson(e)).ToList(); - } - - public JsonDocument SelectAsJsonByKey(object key) - { - return SelectAsJson(WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)).Single(); - } } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs index 60a0d5b..70ccacd 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs @@ -7,8 +7,6 @@ public static class CrudWebApplicationExtensions app.MapGet(path, async (context) => { var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>(); - var result = crudService.SelectAsJson(null); - await context.ResponseJsonAsync(result); }); app.MapPost(path, async (context) => diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs index 6f2f446..19208d8 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.Extensions.Options; namespace CrupestApi.Commons.Crud; @@ -29,27 +30,27 @@ public class EntityJsonHelper<TEntity> where TEntity : class } } - public virtual JsonDocument ConvertEntityToJson(object? entity) + public virtual Dictionary<string, object?> ConvertEntityToDictionary(object? entity) { Debug.Assert(entity is null || entity is TEntity); - return JsonSerializer.SerializeToDocument<TEntity?>((TEntity?)entity, _jsonSerializerOptions); - } - public virtual TEntity? ConvertJsonToEntity(JsonDocument json) - { - var entity = json.Deserialize<TEntity>(); - if (entity is null) return null; + var result = new Dictionary<string, object?>(); - foreach (var column in _table.Columns) + foreach (var column in _table.PropertyColumns) { - var propertyValue = column.PropertyInfo?.GetValue(entity); - - if ((column.IsAutoIncrement || column.IsGenerated) && propertyValue is not null) - { - throw new Exception("You can't specify this property because it is auto generated."); - } + var propertyInfo = column.PropertyInfo; + var value = propertyInfo!.GetValue(entity); + result[column.ColumnName] = value; } - return entity; + return result; + } + + public virtual string ConvertEntityToJson(object? entity) + { + Debug.Assert(entity is null || entity is TEntity); + + var dictionary = ConvertEntityToDictionary(entity); + return JsonSerializer.Serialize(dictionary, _jsonSerializerOptions); } } |