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 | 4264d8135c066081dbabb412db17bf537ceab86e (patch) | |
tree | 85b7b1413518d5ff72c804085cac11a07862807e /docker/crupest-api/CrupestApi/CrupestApi.Commons | |
parent | 51bf9d6c9e522ecc31f9068d9e5eaa0321fef587 (diff) | |
download | crupest-4264d8135c066081dbabb412db17bf537ceab86e.tar.gz crupest-4264d8135c066081dbabb412db17bf537ceab86e.tar.bz2 crupest-4264d8135c066081dbabb412db17bf537ceab86e.zip |
Develop secret api. v50
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons')
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) |