diff options
author | crupest <crupest@outlook.com> | 2022-12-21 13:41:17 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-21 13:41:17 +0800 |
commit | e3510f87617cebf4d11c9bf0e5e4ba640a5741e4 (patch) | |
tree | 4618356c2087d42c7fdf48f6fd28a199aae70c58 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs | |
parent | 10c32c56951fffda6657c75155c80d9b2988c263 (diff) | |
download | crupest-e3510f87617cebf4d11c9bf0e5e4ba640a5741e4.tar.gz crupest-e3510f87617cebf4d11c9bf0e5e4ba640a5741e4.tar.bz2 crupest-e3510f87617cebf4d11c9bf0e5e4ba640a5741e4.zip |
Develop secret api. v53
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs index 33ff2ed..a92eb66 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs @@ -63,8 +63,12 @@ public class CrudService<TEntity> : IDisposable where TEntity : class public TEntity GetByKey(object key) { - var result = _table.Select<TEntity>(_dbConnection, null, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)); - return result.Single(); + var result = _table.Select<TEntity>(_dbConnection, null, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)).SingleOrDefault(); + if (result is null) + { + throw new EntityNotExistException("Required entity not found."); + } + return result; } public IInsertClause ConvertEntityToInsertClauses(TEntity entity) @@ -100,12 +104,16 @@ public class CrudService<TEntity> : IDisposable where TEntity : class public void UpdateByKey(object key, TEntity entity, UpdateBehavior behavior) { - _table.Update(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key), + var affectedCount = _table.Update(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key), ConvertEntityToUpdateClauses(entity, behavior)); + if (affectedCount == 0) + { + throw new EntityNotExistException("Required entity not found."); + } } - public void DeleteByKey(object key) + public bool DeleteByKey(object key) { - _table.Delete(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)); + return _table.Delete(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key)) == 1; } } |