Implemented Groups delete buttons

This commit is contained in:
2025-10-11 13:48:23 +02:00
parent 0ab76d895e
commit dfe1a2b2b1
3 changed files with 31 additions and 25 deletions

View File

@@ -44,19 +44,19 @@ public class GroupsController : Controller
return groups; return groups;
} }
[HttpGet("Delete")] [HttpDelete("Delete")]
public async Task<bool> Delete(string uid) public async Task<GroupsDeleteResponseModel> Delete(string uid)
{ {
return await Task.Run(async () => return await Task.Run(async () =>
{ {
try try
{ {
await _ldap.DeleteGroupAsync(uid); await _ldap.DeleteGroupAsync(uid);
return true; return new GroupsDeleteResponseModel(true);
} }
catch (Exception) catch (Exception)
{ {
return false; return new GroupsDeleteResponseModel(false);
} }
}); });
} }

View File

@@ -0,0 +1,6 @@
namespace Berufsschule_HAM.Models;
public class GroupsDeleteResponseModel(bool successful, string exception = "None")
{
public bool Success { get; set; } = successful;
public string? Exception { get; set; } = exception;
}

View File

@@ -32,20 +32,20 @@
</thead> </thead>
<tbody> <tbody>
@{ @{
foreach (GroupsTableViewModel userTableViewModel in Model.GroupsTableViewModels) foreach (GroupsTableViewModel groupTableViewModel in Model.GroupsTableViewModels)
{ {
<tr> <tr>
<td style="text-align: center">@userTableViewModel.Group</td> <td style="text-align: center">@groupTableViewModel.Group</td>
<td style="text-align: center">@(userTableViewModel.CanInventorize ? "☑️" : "❌")</td> <td style="text-align: center">@(groupTableViewModel.CanInventorize ? "☑️" : "❌")</td>
<td style="text-align: center">@(userTableViewModel.CanManageUsers ? "☑️" : "❌")</td> <td style="text-align: center">@(groupTableViewModel.CanManageUsers ? "☑️" : "❌")</td>
<td style="text-align: center">@(userTableViewModel.CanManageLocations ? "☑️" : "❌")</td> <td style="text-align: center">@(groupTableViewModel.CanManageLocations ? "☑️" : "❌")</td>
<td style="text-align: center">@(userTableViewModel.CanManageAssets ? "☑️" : "❌")</td> <td style="text-align: center">@(groupTableViewModel.CanManageAssets ? "☑️" : "❌")</td>
<td style="text-align: center">@(userTableViewModel.CanManageGroups ? "☑️" : "❌")</td> <td style="text-align: center">@(groupTableViewModel.CanManageGroups ? "☑️" : "❌")</td>
<td style="text-align: center"> <td style="text-align: center">
<div class="d-flex gap-2 justify-content-center"> <div class="d-flex gap-2 justify-content-center">
<button class="btn btn-sm btn-primary">Update</button> <button class="btn btn-sm btn-primary">Update</button>
<button class="btn btn-sm btn-danger btn-delete" <button class="btn btn-sm btn-danger btn-delete"
data-user-id="@userTableViewModel.Cn" data-group-id="@groupTableViewModel.Cn"
data-bs-toggle="modal" data-bs-toggle="modal"
data-bs-target="#deleteModal"> data-bs-target="#deleteModal">
Delete Delete
@@ -59,8 +59,8 @@
</table> </table>
</div> </div>
</div> </div>
@*
<!-- User Delete Confirmation Modal --> <!-- Group Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true"> <div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered"> <div class="modal-dialog modal-dialog-centered">
<div class="modal-content"> <div class="modal-content">
@@ -69,7 +69,7 @@
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button> <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<p>Are you sure you want to delete the user <strong id="userName"></strong> (ID: <span id="userId"></span>)?</p> <p>Are you sure you want to delete the group <strong id="groupName"></strong> (ID: <span id="groupId"></span>)?</p>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
@@ -89,14 +89,14 @@
deleteModal.addEventListener('show.bs.modal', event => { deleteModal.addEventListener('show.bs.modal', event => {
currentButton = event.relatedTarget; // Button that triggered the modal currentButton = event.relatedTarget; // Button that triggered the modal
const userId = currentButton.getAttribute('data-user-id'); const groupId = currentButton.getAttribute('data-group-id');
const userName = currentButton.getAttribute('data-user-name'); const groupName = currentButton.getAttribute('data-group-name');
deleteModal.querySelector('#userId').textContent = userId; deleteModal.querySelector('#groupId').textContent = groupId;
deleteModal.querySelector('#userName').textContent = userName; deleteModal.querySelector('#groupName').textContent = groupName;
// Store the delete URL for later use // Store the delete URL for later use
deleteModal.querySelector('#deleteForm').dataset.url = `/Users/Delete?uid=${userId}`; deleteModal.querySelector('#deleteForm').dataset.url = `/Groups/Delete?uid=${groupId}`;
}); });
// Handle submit of deleteForm via fetch() // Handle submit of deleteForm via fetch()
@@ -105,16 +105,16 @@
e.preventDefault(); e.preventDefault();
const url = deleteForm.dataset.url; const url = deleteForm.dataset.url;
const userId = deleteModal.querySelector('#userId').textContent; const groupId = deleteModal.querySelector('#groupId').textContent;
try { try {
const response = await fetch(url, { const response = await fetch(url, {
method: 'GET', method: 'DELETE',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Accept': 'application/json' 'Accept': 'application/json'
}//, }//,
//body: JSON.stringify({ id: userId }) // Use this for Post requests with [FromBody] parameters like in /Groups/Update //body: JSON.stringify({ id: groupId }) // Use this for Post requests with [FromBody] parameters like in /Groups/Update
}); });
const result = await response.json(); const result = await response.json();
@@ -129,7 +129,7 @@
row.classList.add('table-danger'); row.classList.add('table-danger');
setTimeout(() => row.remove(), 300); setTimeout(() => row.remove(), 300);
showToast('User deleted successfully', 'success'); showToast('Group deleted successfully', 'success');
} else { } else {
showToast(`❌ ${result.reason}: ${result.exception || 'Unknown error'}`, 'danger'); showToast(`❌ ${result.reason}: ${result.exception || 'Unknown error'}`, 'danger');
} }
@@ -165,4 +165,4 @@
return container; return container;
} }
}); });
</script> *@ </script>