Refactor interaction and user deletion logic

Consolidated repeated methods for interaction/user deletion into reusable components to improve maintainability. Introduced a `ConfirmDialog` for consistent confirmation UI and streamlined associated logic across pages. Removed redundant methods and enhanced admin-specific page security checks.
This commit is contained in:
Andre Beging
2025-03-28 23:55:12 +01:00
parent 83257d1d2a
commit 027a36ce17
12 changed files with 174 additions and 234 deletions

View File

@@ -1,4 +1,3 @@
using Blazorise;
using Blazorise.DataGrid;
using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Helper;
@@ -63,6 +62,7 @@ namespace FoodsharingSiegen.Server.Pages
/// <inheritdoc />
protected override async Task InitializeDataAsync()
{
if (!CurrentUser.IsAdmin()) NavigationManager.NavigateTo("/");
await LoadUsers();
}
@@ -98,6 +98,33 @@ namespace FoodsharingSiegen.Server.Pages
#endregion
#region Private Method RemoveUserAsync
/// <summary>
/// Removes the specified user if they are not an admin, after confirming the action with a dialog.
/// </summary>
/// <param name="user">The user to be removed.</param>
/// <returns>A task that represents the asynchronous remove operation.</returns>
private async Task RemoveUserAsync(User user)
{
if (user.IsAdmin())
{
await Notification.Error("Admins können nicht gelöscht werden!");
return;
}
await ConfirmDialog.ShowAsync(ModalService, "Bestätigen", $"User {user.Mail} löschen?", async () =>
{
var removeR = await UserService.RemoveAsync(user.Id);
if (!removeR.Success)
await Notification.Error($"Löschen: {removeR.ErrorMessage}")!;
else
await LoadUsers();
});
}
#endregion
#region Private Method RowInserted
/// <summary>
@@ -115,51 +142,6 @@ namespace FoodsharingSiegen.Server.Pages
#endregion
#region Private Method RowRemoved
/// <summary>
/// Rows the removed using the specified arg (a. beging, 08.02.2023)
/// </summary>
/// <param name="arg">The arg</param>
private async Task RowRemoved(User arg)
{
var removeR = await UserService.RemoveAsync(arg.Id);
if (!removeR.Success)
await Notification.Error($"Löschen: {removeR.ErrorMessage}")!;
else
await LoadUsers();
}
#endregion
#region Private Method RowRemoving
/// <summary>
/// Rows the removing using the specified arg (a. beging, 08.02.2023)
/// </summary>
/// <param name="arg">The arg</param>
private async Task RowRemoving(CancellableRowChange<User> arg)
{
if (arg.Item.IsAdmin())
{
await Notification.Error("Admins können nicht gelöscht werden!");
arg.Cancel = true;
return;
}
var confirm = await Message.Confirm($"User {arg.Item.Mail} löschen?", "Bestätigen", o =>
{
o.ConfirmButtonText = "Löschen";
o.CancelButtonText = "Abbrechen";
o.ShowMessageIcon = false;
o.ConfirmButtonColor = Color.Danger;
});
arg.Cancel = !confirm;
}
#endregion
#region Private Method RowUpdated
/// <summary>