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 | 84 |
1 files changed, 17 insertions, 67 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 7da6ac7..9402d69 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -1,89 +1,39 @@ +using System.Data; using Dapper; -using Microsoft.Data.Sqlite; using Microsoft.Extensions.Options; namespace CrupestApi.Commons.Crud; -public class CrudService<TEntity> +// TODO: Implement and register this service. +public class CrudService<TEntity> : IDisposable { protected readonly TableInfo _table; + protected readonly IDbConnection _dbConnection; protected readonly IOptionsSnapshot<CrupestApiConfig> _crupestApiOptions; private readonly ILogger<CrudService<TEntity>> _logger; - public CrudService(ServiceProvider services) + public CrudService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, IOptionsSnapshot<CrupestApiConfig> crupestApiOptions, ILogger<CrudService<TEntity>> logger) { - _table = new TableInfo(typeof(TEntity)); - _crupestApiOptions = services.GetRequiredService<IOptionsSnapshot<CrupestApiConfig>>(); - _logger = services.GetRequiredService<ILogger<CrudService<TEntity>>>(); - } - - public virtual string GetDbConnectionString() - { - var fileName = Path.Combine(_crupestApiOptions.Value.DataDir, "crupest-api.db"); + _table = tableInfoFactory.Get(typeof(TEntity)); + _dbConnection = dbConnectionFactory.Get(); + _crupestApiOptions = crupestApiOptions; + _logger = logger; - return new SqliteConnectionStringBuilder() + if (!_table.CheckExistence(_dbConnection)) { - DataSource = fileName, - Mode = SqliteOpenMode.ReadWriteCreate - }.ToString(); - } - - - public async Task<SqliteConnection> CreateDbConnection() - { - var connection = new SqliteConnection(GetDbConnectionString()); - await connection.OpenAsync(); - return connection; - } - - public virtual async Task DoInitializeDatabase(SqliteConnection connection) - { - await using var transaction = await connection.BeginTransactionAsync(); - await connection.ExecuteAsync(_table.GenerateCreateTableSql(), transaction: transaction); - await transaction.CommitAsync(); - } - - public virtual async Task<SqliteConnection> EnsureDatabase() - { - var connection = await CreateDbConnection(); - var exist = await _table.CheckExistence(connection); - if (!exist) - { - await DoInitializeDatabase(connection); + DoInitializeDatabase(_dbConnection); } - return connection; - } - - - public virtual async Task<IEnumerable<TEntity>> QueryAsync(WhereClause? where = null, OrderByClause? orderBy = null, int? skip = null, int? limit = null) - { - var connection = await EnsureDatabase(); - DynamicParameters parameters; - var sql = _table.GenerateSelectSql(where, orderBy, skip, limit, out parameters); - return await connection.QueryAsync<TEntity>(sql, parameters); - } - - public virtual async Task<int> InsertAsync(InsertClause insert) - { - var connection = await EnsureDatabase(); - DynamicParameters parameters; - var sql = _table.GenerateInsertSql(insert, out parameters); - return await connection.ExecuteAsync(sql, parameters); } - public virtual async Task<int> UpdateAsync(WhereClause? where, UpdateClause update) + public virtual void DoInitializeDatabase(IDbConnection connection) { - var connection = await EnsureDatabase(); - DynamicParameters parameters; - var sql = _table.GenerateUpdateSql(where, update, out parameters); - return await connection.ExecuteAsync(sql, parameters); + using var transaction = connection.BeginTransaction(); + connection.Execute(_table.GenerateCreateTableSql(), transaction: transaction); + transaction.Commit(); } - public virtual async Task<int> DeleteAsync(WhereClause? where) + public void Dispose() { - var connection = await EnsureDatabase(); - DynamicParameters parameters; - var sql = _table.GenerateDeleteSql(where, out parameters); - return await connection.ExecuteAsync(sql, parameters); + _dbConnection.Dispose(); } } |