diff options
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 811b2e6..af6a8d5 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -1,13 +1,16 @@ using System.Data; +using System.Text.Json; using Dapper; namespace CrupestApi.Commons.Crud; +// TODO: Register this. public class CrudService<TEntity> : IDisposable where TEntity : class { protected readonly TableInfo _table; protected readonly string? _connectionName; protected readonly IDbConnection _dbConnection; + protected readonly EntityJsonHelper _jsonHelper; private readonly ILogger<CrudService<TEntity>> _logger; public CrudService(string? connectionName, ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, ILoggerFactory loggerFactory) @@ -15,6 +18,7 @@ public class CrudService<TEntity> : IDisposable where TEntity : class _connectionName = connectionName; _table = tableInfoFactory.Get(typeof(TEntity)); _dbConnection = dbConnectionFactory.Get(_connectionName); + _jsonHelper = new EntityJsonHelper(_table); _logger = loggerFactory.CreateLogger<CrudService<TEntity>>(); if (!_table.CheckExistence(_dbConnection)) @@ -50,12 +54,14 @@ public class CrudService<TEntity> : IDisposable where TEntity : class return _table.SelectCount(_dbConnection, filter); } - public int Insert(IInsertClause insertClause) + // Return the key. + public object Insert(IInsertClause insertClause) { return _table.Insert(_dbConnection, insertClause); } - public int Insert(TEntity entity) + // Return the key. + public object Insert(TEntity entity) { return _table.Insert(_dbConnection, _table.GenerateInsertClauseFromEntity(entity)); } @@ -69,4 +75,25 @@ public class CrudService<TEntity> : IDisposable where TEntity : class { 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(); + } + + public object InsertFromJson(JsonDocument? json) + { + // TODO: Implement this. + throw new NotImplementedException(); + } } |