-
-
+
- EditUser(new User()))">Create user
-}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
- @if (UserToEdit != null)
- {
- @(UserToEdit.Id == 0 ? "New" : "Edit") @UserToEdit.CommonName (@UserToEdit.Id)
-
-
-
-
-
-
-
-
-
-
-
-
- }
- else
- {
- No securityGroup selected
- }
-
- No Thanks
- OK
-
-
\ No newline at end of file
+
+
+
+
+
+
+ Please enter a valid common name!
+
+
+
+
+
+
+
+
+
+
+
+
+ Please enter the email.
+ Email is ok.
+ Enter valid email!
+
+
+
+
+
+
+
+ @{
+ var name = ((User) context ).Parent?.CommonName ?? "-";
+ @name
+ }
+
+
+
+
+
+
+
+}
\ No newline at end of file
diff --git a/UserService/Pages/Users.razor.cs b/UserService/Pages/Users.razor.cs
index 8aac50e..ac0916b 100644
--- a/UserService/Pages/Users.razor.cs
+++ b/UserService/Pages/Users.razor.cs
@@ -1,44 +1,99 @@
-using System.Collections.Generic;
-using System.Threading.Tasks;
+using System;
using Microsoft.AspNetCore.Components;
-using UserService.DatabaseLayer.DataModels;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Blazorise;
+using Blazorise.DataGrid;
+using Microsoft.JSInterop;
using UserService.DatabaseLayer.Repository;
+using UserService.Infrastructure;
+using UserService.Infrastructure.DataModels;
namespace UserService.Pages
{
- public class UsersBase : ComponentBase
+ public class UsersBase : MembersBase
{
[Inject] private IUsersRepository UsersRepository { get; set; } = null!;
[Inject] private IOrganizationUnitsRepository OrganizationUnitsRepository { get; set; } = null!;
- protected bool DialogIsOpen { get; set; }
- protected User? UserToEdit { get; private set; }
-
- protected IReadOnlyList? Users { get; private set; }
- protected IReadOnlyList? OrganizationUnits { get; private set; }
+ [Inject] private IJSRuntime JsRuntime { get; set; } = null!;
protected override async Task OnInitializedAsync()
{
- Users = await UsersRepository.GetAllAsync();
+ Members = await UsersRepository.GetAllAsync().ConfigureAwait(false);
OrganizationUnits = await OrganizationUnitsRepository.GetAllAsync().ConfigureAwait(false);
}
- protected void EditUser(User user)
+ protected override async Task RowInsertingCallback(CancellableRowChange> arg)
{
- DialogIsOpen = true;
- UserToEdit = user;
+ if (arg is null) throw new ArgumentNullException(nameof(arg));
+ var mailValidation = Validators.ValidateEmail(arg.Values[nameof(User.EMail)]?.ToString());
+ var commonNameValidation = Validators.ValidateCommonName(arg.Values[nameof(User.CommonName)]?.ToString(),
+ Members ?? Enumerable.Empty());
+ if (mailValidation == true && commonNameValidation == true) return;
+
+ await JsRuntime.InvokeVoidAsync("alert", "User could not be added").ConfigureAwait(false);
+ arg.Cancel = true;
}
- protected async Task OkClick()
+ protected override async Task RowInsertedCallback(SavedRowItem> arg)
{
- if (UserToEdit is null) return;
- await UsersRepository.UpdateAsync(UserToEdit).ConfigureAwait(false);
- DialogIsOpen = false;
+ if (arg is null) throw new ArgumentNullException(nameof(arg));
+ var user = arg.Item;
+ await UsersRepository.AddAsync(user).ConfigureAwait(false);
+ user.Parent = OrganizationUnits?.FirstOrDefault(x => x.Id == user.ParentId);
}
- protected async Task DeleteUser(User user)
+ protected override async Task RowDeletingCallback(CancellableRowChange arg)
{
- await UsersRepository.DeleteAsync(user).ConfigureAwait(false);
+ if (arg == null) throw new ArgumentNullException(nameof(arg));
+ var confirmed = await JsRuntime.InvokeAsync("confirm",
+ $"You are about to delete the user {arg.Item.FullName}. Are you sure?").ConfigureAwait(false);
+ if (confirmed)
+ {
+ await UsersRepository.DeleteAsync(arg.Item).ConfigureAwait(false);
+ return;
+ }
+
+ arg.Cancel = true;
+ }
+
+ protected static void ValidateEmail(ValidatorEventArgs e)
+ {
+ if (e == null) throw new ArgumentNullException(nameof(e));
+ var email = e.Value?.ToString();
+ var validationResult = Validators.ValidateEmail(email);
+
+ e.Status = validationResult switch
+ {
+ null => ValidationStatus.None,
+ false => ValidationStatus.Error,
+ _ => ValidationStatus.Success
+ };
+ }
+
+ protected override async Task RowUpdatingCallback(CancellableRowChange> arg)
+ {
+ if (arg == null) throw new ArgumentNullException(nameof(arg));
+ var user = arg.Item;
+ user.MapFields(arg.Values);
+ user.Parent = OrganizationUnits?.FirstOrDefault(x => x.Id == (int?) arg.Values[nameof(Node.ParentId)]);
+ var result = await UsersRepository.UpdateAsync(user).ConfigureAwait(false);
+ arg.Cancel = !result;
+ }
+
+ protected override bool OnCustomFilter(User model)
+ {
+ if (model == null) throw new ArgumentNullException(nameof(model));
+ // We want to accept empty value as valid or otherwise
+ // datagrid will not show anything.
+ if (string.IsNullOrEmpty(CustomFilterValue) || CustomFilterValue.Length < 3) return true;
+
+ return
+ model.FirstName?.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase) == true
+ || model.LastName?.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase) == true
+ || model.CommonName.Contains(CustomFilterValue, StringComparison.OrdinalIgnoreCase);
}
}
}
\ No newline at end of file
diff --git a/UserService/Pages/_Host.cshtml b/UserService/Pages/_Host.cshtml
index 8b3f80a..b5f61a9 100644
--- a/UserService/Pages/_Host.cshtml
+++ b/UserService/Pages/_Host.cshtml
@@ -8,33 +8,41 @@
-
-
+
+
UserService
-
+
+
-
-
-
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
- An error has occurred. This application may no longer respond until reloaded.
-
-
- An unhandled exception has occurred. See browser dev tools for details.
-
- Reload
- 🗙
-
-
-
-
-
+
+
+ An error has occurred. This application may no longer respond until reloaded.
+
+
+ An unhandled exception has occurred. See browser dev tools for details.
+
+ Reload
+ 🗙
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/UserService/Shared/NavMenu.razor b/UserService/Shared/NavMenu.razor
index d1a68c0..3fb8567 100644
--- a/UserService/Shared/NavMenu.razor
+++ b/UserService/Shared/NavMenu.razor
@@ -1,49 +1,38 @@
-