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 | |
| parent | 10c32c56951fffda6657c75155c80d9b2988c263 (diff) | |
| download | crupest-e3510f87617cebf4d11c9bf0e5e4ba640a5741e4.tar.gz crupest-e3510f87617cebf4d11c9bf0e5e4ba640a5741e4.tar.bz2 crupest-e3510f87617cebf4d11c9bf0e5e4ba640a5741e4.zip  | |
Develop secret api. v53
Diffstat (limited to 'docker')
| -rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs | 18 | ||||
| -rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs | 7 | 
2 files changed, 18 insertions, 7 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;      }  } diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs index c91c969..dfb67d1 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs @@ -68,8 +68,11 @@ public static class CrudWebApplicationExtensions                  return;              } -            crudService.DeleteByKey(key); -            await context.ResponseMessageAsync("Deleted.", StatusCodes.Status200OK); +            var deleted = crudService.DeleteByKey(key); +            if (deleted) +                await context.ResponseMessageAsync("Deleted.", StatusCodes.Status200OK); +            else +                await context.ResponseMessageAsync("Not exist.", StatusCodes.Status200OK);          });          return app;  | 
