diff options
5 files changed, 43 insertions, 3 deletions
diff --git a/cspell.yaml b/cspell.yaml index 4272ef7..23f6d5b 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -13,5 +13,6 @@ dictionaries: - filetypes - npm words: + - autoincrement - crupest - - Todos + - todos diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 27f0c85..d1958e5 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -62,6 +62,12 @@ public class CrudService<TEntity> : IDisposable where TEntity : class return result.Single(); } + public string Create(TEntity entity) + { + var key = _table.Insert(_dbConnection, entity); + return (string)key; + } + public string Create(JsonElement jsonElement) { var insertClauses = _jsonHelper.ConvertJsonElementToInsertClauses(jsonElement); diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs index 3147e99..a20963c 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/TableInfo.cs @@ -1,4 +1,5 @@ using System.Data; +using System.Diagnostics; using System.Reflection; using System.Text; using Dapper; @@ -438,6 +439,18 @@ CREATE TABLE {tableName}( return queryResult.Select(MapDynamicTo<TResult>).ToList(); } + public IInsertClause ConvertEntityToInsertClause(object entity) + { + Debug.Assert(EntityType.IsInstanceOfType(entity)); + var result = new InsertClause(); + foreach (var column in PropertyColumns) + { + var value = column.PropertyInfo!.GetValue(entity); + result.Add(column.ColumnName, value); + } + return result; + } + /// <summary> /// Insert a entity and call hooks. /// </summary> @@ -504,6 +517,14 @@ 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/SecretInfo.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs index 8b9420a..1f00f9a 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretInfo.cs @@ -8,7 +8,7 @@ public class SecretInfo { [Column(NotNull = true)] public string Key { get; set; } = default!; - [Column(NotNull = true, Generated = true, NoUpdate = true, ActAsKey = true)] + [Column(NotNull = true, NoUpdate = true, ActAsKey = true)] public string Secret { get; set; } = default!; [Column(DefaultEmptyForString = true)] public string Description { get; set; } = default!; diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs index fc13707..4d9b14c 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs @@ -1,4 +1,3 @@ -using CrupestApi.Commons; using CrupestApi.Commons.Crud; namespace CrupestApi.Commons.Secrets; @@ -11,6 +10,19 @@ public class SecretService : CrudService<SecretInfo>, ISecretService } + protected override void DoInitializeDatabase(System.Data.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." + }); + transaction.Commit(); + } + public List<string> GetPermissions(string secret) { var list = _table.Select<SecretInfo>(_dbConnection, |