diff options
| author | crupest <crupest@outlook.com> | 2022-12-20 19:25:02 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-12-20 20:32:53 +0800 | 
| commit | 41ba5e625d22543a4531df749abc3e74da6aaef1 (patch) | |
| tree | 85b7b1413518d5ff72c804085cac11a07862807e /docker/crupest-api/CrupestApi | |
| parent | 807ffbc0edf25754351c71af147cb58beaaf1498 (diff) | |
| download | crupest-41ba5e625d22543a4531df749abc3e74da6aaef1.tar.gz crupest-41ba5e625d22543a4531df749abc3e74da6aaef1.tar.bz2 crupest-41ba5e625d22543a4531df749abc3e74da6aaef1.zip  | |
Develop secret api. v50
Diffstat (limited to 'docker/crupest-api/CrupestApi')
3 files changed, 25 insertions, 18 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index d1958e5..1a2a055 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -62,9 +62,21 @@ public class CrudService<TEntity> : IDisposable where TEntity : class          return result.Single();      } +    public IInsertClause ConvertEntityToInsertClauses(TEntity entity) +    { +        var result = new InsertClause(); +        foreach (var column in _table.PropertyColumns) +        { +            var value = column.PropertyInfo!.GetValue(entity); +            result.Add(column.ColumnName, value); +        } +        return result; +    } +      public string Create(TEntity entity)      { -        var key = _table.Insert(_dbConnection, entity); +        var insertClause = ConvertEntityToInsertClauses(entity); +        var key = _table.Insert(_dbConnection, insertClause);          return (string)key;      } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs index 0565561..c1f647b 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs @@ -537,14 +537,6 @@ CREATE TABLE {tableName}(          return key ?? throw new Exception("No key???");      } -    public object Insert(IDbConnection dbConnection, object entity) -    { -        Debug.Assert(EntityType.IsInstanceOfType(entity)); -        var insert = ConvertEntityToInsertClause(entity); -        return Insert(dbConnection, insert); -    } - -      /// <summary>      /// Upgrade a entity and call hooks.      /// </summary> diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs index 47e9583..69acad3 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs @@ -1,5 +1,5 @@ +using System.Data;  using CrupestApi.Commons.Crud; -using Dapper;  namespace CrupestApi.Commons.Secrets; @@ -11,23 +11,26 @@ public class SecretService : CrudService<SecretInfo>, ISecretService      } -    protected override void DoInitializeDatabase(System.Data.IDbConnection connection) +    protected override void DoInitializeDatabase(IDbConnection connection)      {          base.DoInitializeDatabase(connection);          using var transaction = connection.BeginTransaction(); -        _table.Insert(connection, new SecretInfo -        { -            Key = SecretsConstants.SecretManagementKey, -            Secret = "crupest", -            Description = "This is the init key. Please revoke it immediately after creating a new one." -        }); +        var insertClause = InsertClause.Create() +            .Add(nameof(SecretInfo.Key), SecretsConstants.SecretManagementKey) +            .Add(nameof(SecretInfo.Secret), "crupest") +            .Add(nameof(SecretInfo.Description), "This is the init key. Please revoke it immediately after creating a new one."); +        _table.Insert(connection, insertClause);          transaction.Commit();      }      public void CreateTestSecret(string key, string secret)      {          var connection = _dbConnection; -        connection.Execute("INSERT INTO secrets (key, secret, description) VALUES (@key, @secret, @desc)", new { key, secret, desc = "Test key." }); +        var insertClause = InsertClause.Create() +               .Add(nameof(SecretInfo.Key), key) +               .Add(nameof(SecretInfo.Secret), secret) +               .Add(nameof(SecretInfo.Description), "Test secret."); +        _table.Insert(connection, insertClause);      }      public List<string> GetPermissions(string secret)  | 
