diff options
author | crupest <crupest@outlook.com> | 2022-12-09 18:22:20 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:53 +0800 |
commit | a586767b9b6e122891a8cddba57aecef11ef4bd2 (patch) | |
tree | 058f34a17aa0cff56c702ace3f84d1918e541789 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs | |
parent | bed807e3f8fab2f8b6ea3409886aac9f23f0f761 (diff) | |
download | crupest-a586767b9b6e122891a8cddba57aecef11ef4bd2.tar.gz crupest-a586767b9b6e122891a8cddba57aecef11ef4bd2.tar.bz2 crupest-a586767b9b6e122891a8cddba57aecef11ef4bd2.zip |
Develop secret api. v21
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 9402d69..e098aca 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -1,23 +1,21 @@ using System.Data; using Dapper; -using Microsoft.Extensions.Options; namespace CrupestApi.Commons.Crud; -// TODO: Implement and register this service. -public class CrudService<TEntity> : IDisposable +public class CrudService<TEntity> : IDisposable where TEntity : class { protected readonly TableInfo _table; + protected readonly string? _connectionName; protected readonly IDbConnection _dbConnection; - protected readonly IOptionsSnapshot<CrupestApiConfig> _crupestApiOptions; private readonly ILogger<CrudService<TEntity>> _logger; - public CrudService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, IOptionsSnapshot<CrupestApiConfig> crupestApiOptions, ILogger<CrudService<TEntity>> logger) + public CrudService(string? connectionName, ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, ILoggerFactory loggerFactory) { + _connectionName = connectionName; _table = tableInfoFactory.Get(typeof(TEntity)); - _dbConnection = dbConnectionFactory.Get(); - _crupestApiOptions = crupestApiOptions; - _logger = logger; + _dbConnection = dbConnectionFactory.Get(_connectionName); + _logger = loggerFactory.CreateLogger<CrudService<TEntity>>(); if (!_table.CheckExistence(_dbConnection)) { @@ -36,4 +34,29 @@ public class CrudService<TEntity> : IDisposable { _dbConnection.Dispose(); } + + public List<TEntity> Select(IWhereClause? filter) + { + return _table.Select(_dbConnection, filter).Cast<TEntity>().ToList(); + } + + public int Insert(IInsertClause insertClause) + { + return _table.Insert(_dbConnection, insertClause); + } + + public int Insert(TEntity entity) + { + return _table.Insert(_dbConnection, _table.GenerateInsertClauseFromEntity(entity)); + } + + public int Update(IUpdateClause updateClause, IWhereClause? filter) + { + return _table.Update(_dbConnection, filter, updateClause); + } + + public int Delete(IWhereClause? filter) + { + return _table.Delete(_dbConnection, filter); + } } |