diff options
| author | crupest <crupest@outlook.com> | 2022-12-19 12:24:54 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:53 +0800 | 
| commit | 88477dffdd0811a5613dba0aa1db4818bf4fd058 (patch) | |
| tree | 1e431c0b2038cde586be12cfb96c6c2da75b11e8 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud | |
| parent | 2efc4f7ea0784a504ce50207a19a8899a121f8cd (diff) | |
| download | crupest-88477dffdd0811a5613dba0aa1db4818bf4fd058.tar.gz crupest-88477dffdd0811a5613dba0aa1db4818bf4fd058.tar.bz2 crupest-88477dffdd0811a5613dba0aa1db4818bf4fd058.zip  | |
Develop secret api. v43
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud')
4 files changed, 18 insertions, 7 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 184ac0a..27f0c85 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -12,9 +12,9 @@ public class CrudService<TEntity> : IDisposable where TEntity : class      protected readonly EntityJsonHelper<TEntity> _jsonHelper;      private readonly ILogger<CrudService<TEntity>> _logger; -    public CrudService(string? connectionName, ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, EntityJsonHelper<TEntity> jsonHelper, ILoggerFactory loggerFactory) +    public CrudService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, EntityJsonHelper<TEntity> jsonHelper, ILoggerFactory loggerFactory)      { -        _connectionName = connectionName; +        _connectionName = GetConnectionName();          _table = tableInfoFactory.Get(typeof(TEntity));          _dbConnection = dbConnectionFactory.Get(_connectionName);          _jsonHelper = jsonHelper; @@ -23,6 +23,11 @@ public class CrudService<TEntity> : IDisposable where TEntity : class          CheckDatabase(_dbConnection);      } +    protected virtual string GetConnectionName() +    { +        return typeof(TEntity).Name; +    } +      public EntityJsonHelper<TEntity> JsonHelper => _jsonHelper;      protected virtual void CheckDatabase(IDbConnection dbConnection) @@ -33,7 +38,7 @@ public class CrudService<TEntity> : IDisposable where TEntity : class          }      } -    private void DoInitializeDatabase(IDbConnection connection) +    protected virtual void DoInitializeDatabase(IDbConnection connection)      {          using var transaction = connection.BeginTransaction();          connection.Execute(_table.GenerateCreateTableSql(), transaction: transaction); diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudServiceCollectionExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudServiceCollectionExtensions.cs index d51c0a1..e9f28bc 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudServiceCollectionExtensions.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudServiceCollectionExtensions.cs @@ -1,3 +1,4 @@ +using CrupestApi.Commons.Secrets;  using Microsoft.Extensions.DependencyInjection.Extensions;  namespace CrupestApi.Commons.Crud; @@ -9,6 +10,7 @@ public static class CrudServiceCollectionExtensions          services.TryAddSingleton<IDbConnectionFactory, SqliteConnectionFactory>();          services.TryAddSingleton<IColumnTypeProvider, ColumnTypeProvider>();          services.TryAddSingleton<ITableInfoFactory, TableInfoFactory>(); +        services.AddSecrets();          return services;      } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs index 9e85c68..b7bc6f1 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs @@ -2,11 +2,11 @@ namespace CrupestApi.Commons.Crud;  public static class CrudWebApplicationExtensions  { -    public static WebApplication UseCrud<TEntity>(this WebApplication app, string path, string? key) where TEntity : class +    public static WebApplication MapCrud<TEntity>(this WebApplication app, string path, string? permission) where TEntity : class      {          app.MapGet(path, async (context) =>          { -             +            if (!context.RequirePermission(permission)) return;              var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();              var allEntities = crudService.GetAll();              await context.ResponseJsonAsync(allEntities.Select(e => crudService.JsonHelper.ConvertEntityToDictionary(e))); @@ -14,6 +14,7 @@ public static class CrudWebApplicationExtensions          app.MapGet(path + "/{key}", async (context) =>          { +            if (!context.RequirePermission(permission)) return;              var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();              var key = context.Request.RouteValues["key"]?.ToString();              if (key == null) @@ -28,6 +29,7 @@ public static class CrudWebApplicationExtensions          app.MapPost(path, async (context) =>          { +            if (!context.RequirePermission(permission)) return;              var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();              var jsonDocument = await context.Request.ReadJsonAsync();              var key = crudService.Create(jsonDocument.RootElement); @@ -36,6 +38,7 @@ public static class CrudWebApplicationExtensions          app.MapPatch(path + "/{key}", async (context) =>          { +            if (!context.RequirePermission(permission)) return;              var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();              var key = context.Request.RouteValues["key"]?.ToString();              if (key == null) @@ -52,6 +55,7 @@ public static class CrudWebApplicationExtensions          app.MapDelete(path + "/{key}", async (context) =>          { +            if (!context.RequirePermission(permission)) return;              var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();              var key = context.Request.RouteValues["key"]?.ToString();              if (key == null) diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs index b552e6b..3147e99 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs @@ -393,7 +393,7 @@ CREATE TABLE {tableName}(      /// <summary>      /// ConvertParameters. Select. Call hooks.      /// </summary> -    public virtual List<dynamic> SelectDynamic(IDbConnection dbConnection, string? what, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) +    public virtual List<dynamic> SelectDynamic(IDbConnection dbConnection, string? what = null, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null)      {          var (sql, parameters) = GenerateSelectSql(what, where, orderBy, skip, limit);          var queryResult = dbConnection.Query<dynamic>(sql, ConvertParameters(parameters)); @@ -431,7 +431,7 @@ CREATE TABLE {tableName}(      /// <summary>      /// Select and call hooks.      /// </summary> -    public virtual List<TResult> Select<TResult>(IDbConnection dbConnection, string? what, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null) +    public virtual List<TResult> Select<TResult>(IDbConnection dbConnection, string? what = null, IWhereClause? where = null, IOrderByClause? orderBy = null, int? skip = null, int? limit = null)      {          List<dynamic> queryResult = SelectDynamic(dbConnection, what, where, orderBy, skip, limit).ToList();  | 
