aboutsummaryrefslogtreecommitdiff
path: root/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-05 16:25:43 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:52 +0800
commit875a3863cf009cf3521f5cbb06548b1de22536e2 (patch)
tree6fbc48db475a477533026410256c3e35a4e1e5a9 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
parent9f7162f12944cc722dba9e91f4d30aa312142d49 (diff)
downloadcrupest-875a3863cf009cf3521f5cbb06548b1de22536e2.tar.gz
crupest-875a3863cf009cf3521f5cbb06548b1de22536e2.tar.bz2
crupest-875a3863cf009cf3521f5cbb06548b1de22536e2.zip
Develop secret api. v8
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs')
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs51
1 files changed, 4 insertions, 47 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
index bc4ed50..63a247b 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
@@ -7,20 +7,17 @@ namespace CrupestApi.Commons.Crud;
public class CrudService<TEntity>
{
+ protected readonly TableInfo _table;
protected readonly IOptionsSnapshot<CrupestApiConfig> _crupestApiOptions;
protected readonly ILogger<CrudService<TEntity>> _logger;
public CrudService(IOptionsSnapshot<CrupestApiConfig> crupestApiOptions, ILogger<CrudService<TEntity>> logger)
{
+ _table = new TableInfo(typeof(TEntity));
_crupestApiOptions = crupestApiOptions;
_logger = logger;
}
- public virtual string GetTableName()
- {
- return typeof(TEntity).Name;
- }
-
public virtual string GetDbConnectionString()
{
var fileName = Path.Combine(_crupestApiOptions.Value.DataDir, "crupest-api.db");
@@ -40,57 +37,17 @@ public class CrudService<TEntity>
return connection;
}
- public virtual async Task<bool> CheckDatabaseExist(SqliteConnection connection)
- {
- var tableName = GetTableName();
- var count = (await connection.QueryAsync<int>(
- @"SELECT count(*) FROM sqlite_schema WHERE type = 'table' AND tbl_name = @TableName;",
- new { TableName = tableName })).Single();
- if (count == 0)
- {
- return false;
- }
- else if (count > 1)
- {
- throw new DatabaseInternalException($"More than 1 table has name {tableName}. What happened?");
- }
- else
- {
- return true;
- }
- }
-
- public string GetSqlType(Type type)
- {
- return ColumnTypeInfoRegistry.Singleton.GetSqlType(type);
- }
-
- public string GetCreateTableColumnSql()
- {
- var properties = typeof(TEntity).GetProperties();
- var sql = string.Join(", ", properties.Select(p => $"{p.Name} {GetSqlType(p.PropertyType)}"));
- return sql;
- }
-
public virtual async Task DoInitializeDatabase(SqliteConnection connection)
{
await using var transaction = await connection.BeginTransactionAsync();
- var tableName = GetTableName();
- var columnSql = GetCreateTableColumnSql();
- var sql = $@"
-CREATE TABLE {tableName}(
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- {columnSql}
-);
- ";
- await connection.ExecuteAsync(sql, transaction: transaction);
+ await connection.ExecuteAsync(_table.GenerateCreateTableSql(), transaction: transaction);
await transaction.CommitAsync();
}
public virtual async Task<SqliteConnection> EnsureDatabase()
{
var connection = await CreateDbConnection();
- var exist = await CheckDatabaseExist(connection);
+ var exist = await _table.CheckExistence(connection);
if (!exist)
{
await DoInitializeDatabase(connection);