aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs39
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);
+ }
}