Working on users view, added infrastructure lib
This commit is contained in:
@ -2,8 +2,8 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using UserService.DatabaseLayer.DataModels;
|
||||
using UserService.DatabaseLayer.Repository;
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.Pages
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
@page "/securitygroups"
|
||||
@using UserService.DatabaseLayer.DataModels
|
||||
@using UserService.Infrastructure.DataModels
|
||||
@inherits SecurityGroupsBase
|
||||
|
||||
<h1>Table of all security groups</h1>
|
||||
|
@ -1,8 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using UserService.DatabaseLayer.DataModels;
|
||||
using UserService.DatabaseLayer.Repository;
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.Pages
|
||||
{
|
||||
|
@ -1,5 +1,5 @@
|
||||
@page "/users"
|
||||
@using UserService.DatabaseLayer.DataModels
|
||||
@using UserService.Infrastructure.DataModels
|
||||
@inherits UsersBase
|
||||
|
||||
<h1>List of all users</h1>
|
||||
@ -12,7 +12,9 @@
|
||||
}
|
||||
else
|
||||
{
|
||||
<DataGrid TItem="User" RowSelectable="@(u => false)" Editable="true" EditMode="DataGridEditMode.Popup" RowRemoving="RowDeletingCallback" Data="@Users" RowInserted="RowInsertedCallback" RowUpdating="RowUpdatingCallback">
|
||||
<TextEdit Placeholder="Search" Size="Size.Large" @bind-Text="@CustomFilterValue" />
|
||||
|
||||
<DataGrid TItem="User" RowSelectable="@(u => false)" CustomFilter="@OnCustomFilter" Sortable="true" Editable="true" EditMode="DataGridEditMode.Popup" RowRemoving="RowDeletingCallback" Data="@Users" RowInserted="RowInsertedCallback" RowInserting="RowInsertingCallback" RowUpdating="RowUpdatingCallback">
|
||||
<DataGridCommandColumn TItem="User">
|
||||
<NewCommandTemplate>
|
||||
<Button Color="Color.Success" Clicked="@context.Clicked" title="Create user">
|
||||
@ -54,7 +56,7 @@ else
|
||||
</Validation>
|
||||
</EditTemplate>
|
||||
</DataGridColumn>
|
||||
<DataGridColumn TItem="User" Field="@nameof(User.FirstName)" Caption="First Name" Editable="true"/>
|
||||
<DataGridColumn TItem="User" Field="@nameof(User.FirstName)" Caption="First Name" Editable="true" />
|
||||
<DataGridColumn TItem="User" Field="@nameof(User.LastName)" Caption="Last Name" Editable="true"/>
|
||||
<DataGridColumn TItem="User" Field="@nameof(User.EMail)" Caption="EMail" Editable="true">
|
||||
<EditTemplate>
|
||||
@ -78,17 +80,14 @@ else
|
||||
|
||||
</DisplayTemplate>
|
||||
<EditTemplate>
|
||||
|
||||
<Select TValue="int?" SelectedValue="@((int?)(context.CellValue))" SelectedValueChanged="@(v => context.CellValue = v)" >
|
||||
@foreach (var item in OrganizationUnits)
|
||||
{
|
||||
<SelectItem TValue="int" Value="@(item.Id)">@item.CommonName</SelectItem>
|
||||
}
|
||||
</Select>
|
||||
|
||||
|
||||
</EditTemplate>
|
||||
</DataGridSelectColumn>
|
||||
<DataGridCheckColumn TItem="User" Field="@nameof(User.IsActive)" Caption="Active" Editable="true"/>
|
||||
<DataGridCheckColumn TItem="User" Field="@nameof(User.IsActive)" Caption="Active" Editable="true" />
|
||||
</DataGrid>
|
||||
}
|
@ -1,14 +1,14 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Blazorise;
|
||||
using Blazorise.DataGrid;
|
||||
using Microsoft.JSInterop;
|
||||
using UserService.DatabaseLayer.DataModels;
|
||||
using UserService.DatabaseLayer.Repository;
|
||||
using UserService.Infrastructure;
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.Pages
|
||||
{
|
||||
@ -21,6 +21,7 @@ namespace UserService.Pages
|
||||
|
||||
protected IReadOnlyList<User>? Users { get; private set; }
|
||||
protected IReadOnlyList<OrganizationUnit>? OrganizationUnits { get; private set; }
|
||||
protected string? CustomFilterValue { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
@ -28,14 +29,24 @@ namespace UserService.Pages
|
||||
OrganizationUnits = await OrganizationUnitsRepository.GetAllAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected async Task RowInsertedCallback(SavedRowItem<User, Dictionary<string, object>> arg)
|
||||
protected async Task RowInsertingCallback(CancellableRowChange<User, Dictionary<string, object>> arg)
|
||||
{
|
||||
if (arg == null) throw new ArgumentNullException(nameof(arg));
|
||||
var user = arg.Item;
|
||||
user.ParentId = -2;
|
||||
await UsersRepository.AddAsync(user).ConfigureAwait(false);
|
||||
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(), Users);
|
||||
if (mailValidation == true && commonNameValidation == true) return;
|
||||
|
||||
await JsRuntime.InvokeVoidAsync("alert", "User could not be added").ConfigureAwait(false);
|
||||
arg.Cancel = true;
|
||||
}
|
||||
|
||||
protected async Task RowInsertedCallback(SavedRowItem<User, Dictionary<string, object>> arg)
|
||||
{
|
||||
if (arg is null) throw new ArgumentNullException(nameof(arg));
|
||||
var user = arg.Item;
|
||||
user.Parent = OrganizationUnits?.FirstOrDefault(x => x.Id == user.ParentId);
|
||||
await UsersRepository.AddAsync(user).ConfigureAwait(false);
|
||||
}
|
||||
protected async Task RowDeletingCallback(CancellableRowChange<User> arg)
|
||||
{
|
||||
if (arg == null) throw new ArgumentNullException(nameof(arg));
|
||||
@ -50,38 +61,46 @@ namespace UserService.Pages
|
||||
arg.Cancel = true;
|
||||
}
|
||||
|
||||
protected static void ValidateCommonName(ValidatorEventArgs e)
|
||||
protected void ValidateCommonName(ValidatorEventArgs e)
|
||||
{
|
||||
if (e == null) throw new ArgumentNullException(nameof(e));
|
||||
var commonName = e.Value?.ToString();
|
||||
|
||||
if (string.IsNullOrEmpty(commonName))
|
||||
e.Status = ValidationStatus.Error;
|
||||
else
|
||||
e.Status = ValidationStatus.Success;
|
||||
var validationResult = Validators.ValidateCommonName(commonName, Users);
|
||||
e.Status = validationResult == false ? ValidationStatus.Error : ValidationStatus.Success;
|
||||
}
|
||||
|
||||
protected static void ValidateEmail(ValidatorEventArgs e)
|
||||
{
|
||||
if (e == null) throw new ArgumentNullException(nameof(e));
|
||||
var email = e.Value?.ToString();
|
||||
var validationResult = Validators.ValidateEmail(email);
|
||||
|
||||
if (string.IsNullOrEmpty(email))
|
||||
e.Status = ValidationStatus.None;
|
||||
else if (!new EmailAddressAttribute().IsValid(email))
|
||||
e.Status = validationResult switch
|
||||
{
|
||||
e.Status = ValidationStatus.Error;
|
||||
}
|
||||
else
|
||||
e.Status = ValidationStatus.Success;
|
||||
null => ValidationStatus.None,
|
||||
false => ValidationStatus.Error,
|
||||
_ => ValidationStatus.Success
|
||||
};
|
||||
}
|
||||
protected async Task RowUpdatingCallback(CancellableRowChange<User, Dictionary<string, object>> arg)
|
||||
{
|
||||
if (arg == null) throw new ArgumentNullException(nameof(arg));
|
||||
var user = arg.Item;
|
||||
user.Parent = OrganizationUnits.FirstOrDefault(x => x.Id == (int?) arg.Values[nameof(Node.ParentId)]);
|
||||
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 bool OnCustomFilter(User 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) == true;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -11,6 +12,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UserService.DatabaseLayer\UserService.DatabaseLayer.csproj" />
|
||||
<ProjectReference Include="..\UserService.Infrastructure\UserService.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
Reference in New Issue
Block a user