From 74541c7bd7005fb945957bbace348d4a8c59602c Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 9 Dec 2022 18:22:20 +0800 Subject: Develop secret api. v21 --- .../CrupestApi.Commons/Crud/CrudService.cs | 39 +++++++++++++++++----- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs') 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 : IDisposable +public class CrudService : IDisposable where TEntity : class { protected readonly TableInfo _table; + protected readonly string? _connectionName; protected readonly IDbConnection _dbConnection; - protected readonly IOptionsSnapshot _crupestApiOptions; private readonly ILogger> _logger; - public CrudService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, IOptionsSnapshot crupestApiOptions, ILogger> 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>(); if (!_table.CheckExistence(_dbConnection)) { @@ -36,4 +34,29 @@ public class CrudService : IDisposable { _dbConnection.Dispose(); } + + public List Select(IWhereClause? filter) + { + return _table.Select(_dbConnection, filter).Cast().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); + } } -- cgit v1.2.3