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.
162 lines
5.3 KiB
C#
162 lines
5.3 KiB
C#
using Blazorise.DataGrid;
|
|
using FoodsharingSiegen.Contracts.Entity;
|
|
using FoodsharingSiegen.Contracts.Helper;
|
|
using FoodsharingSiegen.Server.Data.Service;
|
|
using FoodsharingSiegen.Server.Dialogs;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace FoodsharingSiegen.Server.Pages
|
|
{
|
|
/// <summary>
|
|
/// The users class (a. beging, 01.04.2022)
|
|
/// </summary>
|
|
public partial class Users
|
|
{
|
|
#region Dependencies
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
/// <summary> Gets or sets the user service. </summary>
|
|
/// <value> The user service. </value>
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
[Inject]
|
|
public UserService UserService { get; set; } = null!;
|
|
|
|
#endregion
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the password modal (ab)
|
|
/// </summary>
|
|
private SetPasswordModal? PasswordModal { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the selected company texts (ab)
|
|
/// </summary>
|
|
private List<string> SelectedCompanyTexts { get; set; } = new();
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the selected user (ab)
|
|
/// </summary>
|
|
private User? SelectedUser { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the user data grid (ab)
|
|
/// </summary>
|
|
private DataGrid<User>? UserDataGrid { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets the value of the user groups (ab)
|
|
/// </summary>
|
|
private IEnumerable<UserGroup> UserGroups => Enum.GetValues<UserGroup>();
|
|
|
|
/// <summary>
|
|
/// Gets or sets the value of the user list (ab)
|
|
/// </summary>
|
|
private List<User>? UserList { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region Override InitializeDataAsync
|
|
|
|
/// <inheritdoc />
|
|
protected override async Task InitializeDataAsync()
|
|
{
|
|
if (!CurrentUser.IsAdmin()) NavigationManager.NavigateTo("/");
|
|
await LoadUsers();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Method LoadUsers
|
|
|
|
/// <summary>
|
|
/// Loads the users (a. beging, 01.04.2022)
|
|
/// </summary>
|
|
private async Task LoadUsers()
|
|
{
|
|
var usersR = await UserService.GetUsersAsync();
|
|
if (usersR.Success) UserList = usersR.Data;
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Method OnPasswordSet
|
|
|
|
/// <summary>
|
|
/// Ons the password set using the specified user (a. beging, 23.05.2022)
|
|
/// </summary>
|
|
/// <param name="user">The user</param>
|
|
private async Task OnPasswordSet(User user)
|
|
{
|
|
var setPasswordR = await UserService.SetPassword(user);
|
|
if (setPasswordR.Success)
|
|
await Notification.Success("Passwort gespeichert");
|
|
}
|
|
|
|
#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>
|
|
/// Rows the inserted using the specified arg (a. beging, 01.04.2022)
|
|
/// </summary>
|
|
/// <param name="arg">The arg</param>
|
|
private async Task RowInserted(SavedRowItem<User, Dictionary<string, object>> arg)
|
|
{
|
|
var addUserR = await UserService.AddUserAsync(arg.Item);
|
|
if (!addUserR.Success)
|
|
await Notification.Error($"Fehler beim Anlegen: {addUserR.ErrorMessage}")!;
|
|
else
|
|
await LoadUsers();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private Method RowUpdated
|
|
|
|
/// <summary>
|
|
/// Rows the updated using the specified arg (a. beging, 01.04.2022)
|
|
/// </summary>
|
|
/// <param name="arg">The arg</param>
|
|
private async Task RowUpdated(SavedRowItem<User, Dictionary<string, object>> arg)
|
|
{
|
|
if (arg.Item?.Id == null || arg.Item.Id.Equals(Guid.Empty) || arg.Values?.Any() != true) return;
|
|
|
|
var updateR = await UserService.Update(arg.Item);
|
|
if (!updateR.Success)
|
|
await Notification.Error($"Fehler beim Speichern: {updateR.ErrorMessage}")!;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |