From d12ac659f555673e6cb266554ef4d9128fe21c1b Mon Sep 17 00:00:00 2001 From: anomny Date: Sun, 5 Oct 2025 14:50:59 +0200 Subject: [PATCH] Add return model to AssetsController --- src/Controllers/AssetsController.cs | 24 ++++++++++++++++-------- src/Controllers/GroupsController.cs | 4 ++-- src/Controllers/LocationsController.cs | 4 ++-- src/Controllers/UsersController.cs | 4 ++-- src/Models/AssetsDeleteResponseModel.cs | 14 ++++++++++++++ src/Services/LdapService.cs | 18 +++++++++--------- 6 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 src/Models/AssetsDeleteResponseModel.cs diff --git a/src/Controllers/AssetsController.cs b/src/Controllers/AssetsController.cs index bb6df84..7a5b63a 100644 --- a/src/Controllers/AssetsController.cs +++ b/src/Controllers/AssetsController.cs @@ -106,20 +106,28 @@ public class AssetsController : Controller } [HttpGet("Delete")] - public async Task Delete(string cn) - { - if (cn is null) { return false; } - return await Task.Run(() => + public async Task Delete(string cn) + { + AssetsDeleteResponseModel response; + return await Task.Run(async () => { + if (cn is null) + { + response = new AssetsDeleteResponseModel(false, AssetsDeleteErrorEnum.CnIsNull); + return response; + } + try { - _ldap.DeleteAsset(cn); - return true; + await _ldap.DeleteAssetAsync(cn); + response = new AssetsDeleteResponseModel(true, AssetsDeleteErrorEnum.None); } - catch (Exception) + catch (Exception e) { - return false; + response = new AssetsDeleteResponseModel(false, AssetsDeleteErrorEnum.UnableToDeleteAsset, e.Message); } + + return response; }); } diff --git a/src/Controllers/GroupsController.cs b/src/Controllers/GroupsController.cs index 39e91df..88f93bf 100644 --- a/src/Controllers/GroupsController.cs +++ b/src/Controllers/GroupsController.cs @@ -45,11 +45,11 @@ public class GroupsController : Controller [HttpGet("Delete")] public async Task Delete(string uid) { - return await Task.Run(() => + return await Task.Run(async () => { try { - _ldap.DeleteGroup(uid); + await _ldap.DeleteGroupAsync(uid); return true; } catch (Exception) diff --git a/src/Controllers/LocationsController.cs b/src/Controllers/LocationsController.cs index 8e46229..ba4c867 100644 --- a/src/Controllers/LocationsController.cs +++ b/src/Controllers/LocationsController.cs @@ -26,11 +26,11 @@ public class LocationsController : Controller public async Task Delete(string cn) { if (cn is null) { return false; } - return await Task.Run(() => + return await Task.Run(async () => { try { - _ldap.DeleteLocation(cn); + await _ldap.DeleteLocationAsync(cn); return true; } catch (Exception) diff --git a/src/Controllers/UsersController.cs b/src/Controllers/UsersController.cs index e2091f5..229a290 100644 --- a/src/Controllers/UsersController.cs +++ b/src/Controllers/UsersController.cs @@ -45,11 +45,11 @@ public class UsersController : Controller [HttpGet("Delete")] public async Task Delete(string uid) { - return await Task.Run(() => + return await Task.Run(async () => { try { - _ldap.DeleteUser(uid); + await _ldap.DeleteUserAsync(uid); return true; } catch (Exception) diff --git a/src/Models/AssetsDeleteResponseModel.cs b/src/Models/AssetsDeleteResponseModel.cs new file mode 100644 index 0000000..590991f --- /dev/null +++ b/src/Models/AssetsDeleteResponseModel.cs @@ -0,0 +1,14 @@ +public class AssetsDeleteResponseModel(bool successful, AssetsDeleteErrorEnum errorReason, string exception = "none") +{ + public bool Success { get; set; } = successful; + public AssetsDeleteErrorEnum Reason { get; set; } = errorReason; + + public string? Exception { get; set; } = exception; +} + +public enum AssetsDeleteErrorEnum +{ + None, + CnIsNull, + UnableToDeleteAsset +} \ No newline at end of file diff --git a/src/Services/LdapService.cs b/src/Services/LdapService.cs index 32d93bc..ffcc3ed 100644 --- a/src/Services/LdapService.cs +++ b/src/Services/LdapService.cs @@ -275,28 +275,28 @@ public async Task CreateAsset(LdapAttributeSet attributeSet) }); } - public void DeleteUser(string uid) + public async Task DeleteUserAsync(string uid) { string dn = PrependRDN($"uid={uid}", UsersBaseDn); - DeleteObjectByDn(dn); + await DeleteObjectByDnAsync(dn); } - public void DeleteGroup(string cn) + public async Task DeleteGroupAsync(string cn) { string dn = PrependRDN($"cn={cn}", GroupsBaseDn); - DeleteObjectByDn(dn); + await DeleteObjectByDnAsync(dn); } - public void DeleteLocation(string cn) + public async Task DeleteLocationAsync(string cn) { string dn = PrependRDN($"cn={cn}", LocationsBaseDn); - DeleteObjectByDn(dn); + await DeleteObjectByDnAsync(dn); } - public void DeleteAsset(string cn) + public async Task DeleteAssetAsync(string cn) { string dn = PrependRDN($"cn={cn}", AssetsBaseDn); - DeleteObjectByDn(dn); + await DeleteObjectByDnAsync(dn); } public async Task UpdateUser(string uid, string attributeName, string attributeValue) @@ -337,7 +337,7 @@ public async Task CreateAsset(LdapAttributeSet attributeSet) } } - public async void DeleteObjectByDn(string dn) + public async Task DeleteObjectByDnAsync(string dn) { await _conn.DeleteAsync(dn); }