diff --git a/src/Controllers/AssetsController.cs b/src/Controllers/AssetsController.cs index c541b86..feefbaf 100644 --- a/src/Controllers/AssetsController.cs +++ b/src/Controllers/AssetsController.cs @@ -108,20 +108,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 736fe3e..08b37e0 100644 --- a/src/Controllers/GroupsController.cs +++ b/src/Controllers/GroupsController.cs @@ -47,11 +47,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 3018c77..912bc2a 100644 --- a/src/Controllers/LocationsController.cs +++ b/src/Controllers/LocationsController.cs @@ -28,11 +28,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 d68b363..8630b08 100644 --- a/src/Controllers/UsersController.cs +++ b/src/Controllers/UsersController.cs @@ -47,11 +47,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); }