From e19d5550d88c114acaab77799345135d63117fcc Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 23 Dec 2022 11:48:11 +0800 Subject: Develop secret api. v60 --- .../CrupestApi/CrupestApi.Commons/Crud/CrudService.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs') diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index dc32387..a56790a 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -72,7 +72,7 @@ public class CrudService : IDisposable where TEntity : class var result = _table.Select(_dbConnection, null, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)).SingleOrDefault(); if (result is null) { - throw new EntityNotExistException("Required entity not found."); + throw new EntityNotExistException($"Required entity for key {key} not found."); } return result; } @@ -91,7 +91,7 @@ public class CrudService : IDisposable where TEntity : class public object Create(TEntity entity) { var insertClause = ConvertEntityToInsertClauses(entity); - var key = _table.Insert(_dbConnection, insertClause); + _table.Insert(_dbConnection, insertClause, out var key); return key; } @@ -108,14 +108,16 @@ public class CrudService : IDisposable where TEntity : class return result; } - public void UpdateByKey(object key, TEntity entity, UpdateBehavior behavior = UpdateBehavior.None) + // Return new key. + public object UpdateByKey(object key, TEntity entity, UpdateBehavior behavior = UpdateBehavior.None) { var affectedCount = _table.Update(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key), - ConvertEntityToUpdateClauses(entity, behavior)); + ConvertEntityToUpdateClauses(entity, behavior), out var newKey); if (affectedCount == 0) { - throw new EntityNotExistException("Required entity not found."); + throw new EntityNotExistException($"Required entity for key {key} not found."); } + return newKey ?? key; } public bool DeleteByKey(object key) -- cgit v1.2.3