From 61b32841bdc7cd2504f781e7951a0479804b03e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Thu, 30 Jul 2020 22:35:22 +0200 Subject: [PATCH] more code behind. and more orga power --- UserService.DatabaseLayer/DataModels/Node.cs | 25 +++++-- .../Repository/BaseRepository.cs | 4 +- .../Repository/Repository.cs | 32 +++++++++ UserService.Test/Startup.DevExpress.cs | 27 -------- UserService.db | Bin 40960 -> 40960 bytes UserService/Pages/OrganizationUnits.razor | 62 ++++++++++++++++++ UserService/Pages/OrganizationUnits.razor.cs | 41 ++++++++++++ UserService/Pages/SecurityGroups.razor | 60 ++++------------- UserService/Pages/SecurityGroups.razor.cs | 47 +++++++++++++ UserService/Pages/Users.razor | 14 ++-- UserService/Pages/Users.razor.cs | 2 - UserService/Shared/NavMenu.razor | 8 ++- 12 files changed, 231 insertions(+), 91 deletions(-) delete mode 100644 UserService.Test/Startup.DevExpress.cs create mode 100644 UserService/Pages/OrganizationUnits.razor create mode 100644 UserService/Pages/OrganizationUnits.razor.cs create mode 100644 UserService/Pages/SecurityGroups.razor.cs diff --git a/UserService.DatabaseLayer/DataModels/Node.cs b/UserService.DatabaseLayer/DataModels/Node.cs index ff84ef4..5484f05 100644 --- a/UserService.DatabaseLayer/DataModels/Node.cs +++ b/UserService.DatabaseLayer/DataModels/Node.cs @@ -1,12 +1,30 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; namespace UserService.DatabaseLayer.DataModels { public class OrganizationUnit : Node { public Member? Manager { get; set; } + + /// + public override string ToString() + { + var sb = new StringBuilder(); + if (Level != 0) + { + sb.Append("|"); + } + + sb.Append('-', Level * 4); + + return sb + CommonName; + } + + } public class SecurityGroup : Member @@ -51,10 +69,9 @@ namespace UserService.DatabaseLayer.DataModels public Node? Parent { get; set; } //Parent public int? ParentId { get; set; } - public override string ToString() - { - return $"[{GetType().Name}] {Id:D5} {CommonName}"; - } + public override string ToString() => $"[{GetType().Name}] {Id:D5} {CommonName}"; + + public int Level => Parent?.Level + 1 ?? 0; /// public virtual object Clone() => MemberwiseClone(); diff --git a/UserService.DatabaseLayer/Repository/BaseRepository.cs b/UserService.DatabaseLayer/Repository/BaseRepository.cs index d5364ca..aeb0dd7 100644 --- a/UserService.DatabaseLayer/Repository/BaseRepository.cs +++ b/UserService.DatabaseLayer/Repository/BaseRepository.cs @@ -12,14 +12,14 @@ namespace UserService.DatabaseLayer.Repository { public class BaseRepository where T : Node { - private readonly Func> _context; + protected readonly Func> _context; protected BaseRepository(Func> context) { _context = context; } - public async Task> GetAllAsync(CancellationToken token = default) + public virtual async Task> GetAllAsync(CancellationToken token = default) { await using var db = new UserServiceDbContext(); return await _context(db).Include(x => x.Parent).ToListAsync(token); diff --git a/UserService.DatabaseLayer/Repository/Repository.cs b/UserService.DatabaseLayer/Repository/Repository.cs index fbde753..2be88fd 100644 --- a/UserService.DatabaseLayer/Repository/Repository.cs +++ b/UserService.DatabaseLayer/Repository/Repository.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using UserService.DatabaseLayer.DataModels; namespace UserService.DatabaseLayer.Repository @@ -13,6 +15,36 @@ namespace UserService.DatabaseLayer.Repository public OrganizationUnitsRepository() : base(x => x.OrganizationUnits) { } + + /// + public override async Task> GetAllAsync(CancellationToken token = default) + { + + + await using var db = new UserServiceDbContext(); + var result = new List(); + var rootOus = await _context(db) + .Include(x => x.Parent) + .ToListAsync(token); + + IEnumerable Rec(Node node) + { + if (!(node is OrganizationUnit organizationUnit)) yield break; + yield return organizationUnit; + foreach (var ouChild in rootOus.Where(x=>x.ParentId != null && x.ParentId == organizationUnit.Id)) + { + foreach (var unit in Rec(ouChild)) + { + yield return unit; + } + } + } + foreach (var ou in rootOus.Where(x=> x.ParentId is null)) + { + result.AddRange(Rec(ou)); + } + return result; + } } public class SecurityGroupsRepository : BaseRepository, ISecurityGroupsRepository diff --git a/UserService.Test/Startup.DevExpress.cs b/UserService.Test/Startup.DevExpress.cs deleted file mode 100644 index dc1ede5..0000000 --- a/UserService.Test/Startup.DevExpress.cs +++ /dev/null @@ -1,27 +0,0 @@ -//------------------------------------------------------------------------------ -// Generated by the DevExpress.Blazor package. -// To prevent this operation, add the DxExtendStartupHost property to the project and set this property to False. -// -// UserService.Test.csproj: -// -// -// -// netcoreapp3.1 -// False -// -//------------------------------------------------------------------------------ -using System; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.AspNetCore.Hosting; - -[assembly: HostingStartup(typeof(UserService.Test.DevExpressHostingStartup))] - -namespace UserService.Test { - public partial class DevExpressHostingStartup : IHostingStartup { - void IHostingStartup.Configure(IWebHostBuilder builder) { - builder.ConfigureServices((serviceCollection) => { - serviceCollection.AddDevExpressBlazor(); - }); - } - } -} diff --git a/UserService.db b/UserService.db index ba9c441cb216208724172bfc40ec9079937a258a..a58f715c4fe64657c72fe9d82a82469b3d78bf70 100644 GIT binary patch delta 437 zcmZ9I%_~Gv7>CcfpE2F@8w<%oT~ivLWv9rDQL@ocKK3^mrZiXA$j8Q{td;vmXre|l z*|0E5G>cJ)Sg?>%vS5Q|nCqS!8)xx!dVkM*I`5mx3#q)YD|7~eglSQcl~@crtcVmQ zaHVCK&Dykg%|g2t&DHo@H6x(8(r26e9<;+vbLSB(BI(HX?6}k1Sttjvsz|5-rfUuF>bi;0Dqk@FW=_y>sm_ZLk529v+|S(q6Z{{wAg{Rd+G1Bw3r s!vz%J=0C~E|C|3K{~P}2{EztW0xkK@f0_Rr|4E=ltNAyd>^Bzx0DcZuMF0Q* diff --git a/UserService/Pages/OrganizationUnits.razor b/UserService/Pages/OrganizationUnits.razor new file mode 100644 index 0000000..c17c79e --- /dev/null +++ b/UserService/Pages/OrganizationUnits.razor @@ -0,0 +1,62 @@ +@page "/organizationUnits" +@using UserService.DatabaseLayer.DataModels +@inherits OrganizationUnitsBase + +

List of all organization units

+ +@if (OrganizationUnits is null) +{ +

+ Loading... +

+} +else +{ + + + Common Name + Description + Parent + Manager + + + + + @context.CommonName + @context.Description + @context.Parent + @context.Manager + + edit + + + delete + + + + + Create organization unit +} + + + @if (OuToEdit != null) + { + @(OuToEdit.Id == 0 ? "New" : "Edit") @OuToEdit.CommonName (@OuToEdit.Id) + + +

+ + @**@ +

+ + + } + else + { + No securityGroup selected + } + + No Thanks + OK + + \ No newline at end of file diff --git a/UserService/Pages/OrganizationUnits.razor.cs b/UserService/Pages/OrganizationUnits.razor.cs new file mode 100644 index 0000000..637a122 --- /dev/null +++ b/UserService/Pages/OrganizationUnits.razor.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using UserService.DatabaseLayer.DataModels; +using UserService.DatabaseLayer.Repository; + +namespace UserService.Pages +{ + public class OrganizationUnitsBase : ComponentBase + { + [Inject] private IOrganizationUnitsRepository OrganizationUnitsRepository { get; set; } = null!; + + protected bool DialogIsOpen { get; set; } + protected OrganizationUnit? OuToEdit { get; private set; } + + protected IReadOnlyList? OrganizationUnits { get; private set; } + + protected override async Task OnInitializedAsync() + { + OrganizationUnits = await OrganizationUnitsRepository.GetAllAsync().ConfigureAwait(false); + } + + protected void Edit(OrganizationUnit organizationUnit) + { + DialogIsOpen = true; + OuToEdit = organizationUnit; + } + + protected async Task OkClick() + { + if (OuToEdit is null) return; + await OrganizationUnitsRepository.UpdateAsync(OuToEdit).ConfigureAwait(false); + DialogIsOpen = false; + } + + protected async Task Delete(OrganizationUnit organizationUnit) + { + await OrganizationUnitsRepository.DeleteAsync(organizationUnit).ConfigureAwait(false); + } + } +} \ No newline at end of file diff --git a/UserService/Pages/SecurityGroups.razor b/UserService/Pages/SecurityGroups.razor index 4b3ac83..fdbfba0 100644 --- a/UserService/Pages/SecurityGroups.razor +++ b/UserService/Pages/SecurityGroups.razor @@ -1,13 +1,11 @@ @page "/securitygroups" @using UserService.DatabaseLayer.DataModels @using UserService.DatabaseLayer.Repository -@inject ISecurityGroupsRepository SecurityGroupsRepository -@inject IOrganizationUnitsRepository OrganizationUnits - +@inherits SecurityGroupsBase

Table of all security groups

-@if (_securityGroups == null) +@if (SecurityGroups is null) {

Loading... @@ -15,7 +13,7 @@ } else { - + Common Name Description @@ -37,17 +35,17 @@ else Create new group } - - @if (_securityGroupToEdit != null) + + @if (SecurityGroupToEdit != null) { - @(_securityGroupToEdit.Id == 0 ? "New" : "Edit") @_securityGroupToEdit.CommonName (@_securityGroupToEdit.Id) + @(SecurityGroupToEdit.Id == 0 ? "New" : "Edit") @SecurityGroupToEdit.CommonName (@SecurityGroupToEdit.Id) - +

- - + +

- + } else @@ -55,41 +53,7 @@ else No securityGroup selected } - No Thanks + No Thanks OK - - - -@code { - bool _dialogIsOpen; - SecurityGroup _securityGroupToEdit; - - private IReadOnlyList _securityGroups; - private IReadOnlyList _organizationUnits; - - protected override async Task OnInitializedAsync() - { - _securityGroups = await SecurityGroupsRepository.GetAllAsync(); - _organizationUnits = await OrganizationUnits.GetAllAsync().ConfigureAwait(false); - } - - private void EditSecurityGroup(SecurityGroup securityGroup) - { - _dialogIsOpen = true; - _securityGroupToEdit = (SecurityGroup)securityGroup.Clone(); - } - - async Task OkClick() - { - await SecurityGroupsRepository.UpdateAsync(_securityGroupToEdit).ConfigureAwait(false); - await OnInitializedAsync().ConfigureAwait(false); - _dialogIsOpen = false; - } - - private async Task DeleteSecurityGroup(SecurityGroup securityGroup) - { - await SecurityGroupsRepository.DeleteAsync(securityGroup).ConfigureAwait(false); - await OnInitializedAsync().ConfigureAwait(false); - } -} \ No newline at end of file + \ No newline at end of file diff --git a/UserService/Pages/SecurityGroups.razor.cs b/UserService/Pages/SecurityGroups.razor.cs new file mode 100644 index 0000000..b1269ba --- /dev/null +++ b/UserService/Pages/SecurityGroups.razor.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Components; +using UserService.DatabaseLayer.DataModels; +using UserService.DatabaseLayer.Repository; + +namespace UserService.Pages +{ + public class SecurityGroupsBase : ComponentBase + { + [Inject] private ISecurityGroupsRepository? SecurityGroupsRepository { get; set; } + [Inject] private IOrganizationUnitsRepository? OrganizationUnitsRepository { get; set; } + + protected bool DialogIsOpen; + protected SecurityGroup? SecurityGroupToEdit; + + protected IReadOnlyList? SecurityGroups; + protected IReadOnlyList? OrganizationUnits; + + protected override async Task OnInitializedAsync() + { + SecurityGroups = await SecurityGroupsRepository.GetAllAsync().ConfigureAwait(false); + OrganizationUnits = await OrganizationUnitsRepository.GetAllAsync().ConfigureAwait(false); + } + + protected void EditSecurityGroup(SecurityGroup securityGroup) + { + SecurityGroupToEdit = securityGroup; + DialogIsOpen = true; + } + + protected async Task OkClick() + { + if (!(SecurityGroupToEdit is null)) + { + await SecurityGroupsRepository.UpdateAsync(SecurityGroupToEdit).ConfigureAwait(false); + } + + DialogIsOpen = false; + } + + protected async Task DeleteSecurityGroup(SecurityGroup securityGroup) + { + await SecurityGroupsRepository.DeleteAsync(securityGroup).ConfigureAwait(false); + } + } +} \ No newline at end of file diff --git a/UserService/Pages/Users.razor b/UserService/Pages/Users.razor index 09d15e9..6bc2fd3 100644 --- a/UserService/Pages/Users.razor +++ b/UserService/Pages/Users.razor @@ -43,16 +43,16 @@ else { @(UserToEdit.Id == 0 ? "New" : "Edit") @UserToEdit.CommonName (@UserToEdit.Id) - - + +

- - + +

- - + +

- + } else diff --git a/UserService/Pages/Users.razor.cs b/UserService/Pages/Users.razor.cs index e7b9666..8aac50e 100644 --- a/UserService/Pages/Users.razor.cs +++ b/UserService/Pages/Users.razor.cs @@ -33,14 +33,12 @@ namespace UserService.Pages { if (UserToEdit is null) return; await UsersRepository.UpdateAsync(UserToEdit).ConfigureAwait(false); - await OnInitializedAsync().ConfigureAwait(false); DialogIsOpen = false; } protected async Task DeleteUser(User user) { await UsersRepository.DeleteAsync(user).ConfigureAwait(false); - await OnInitializedAsync().ConfigureAwait(false); } } } \ No newline at end of file diff --git a/UserService/Shared/NavMenu.razor b/UserService/Shared/NavMenu.razor index 86cbcad..d1a68c0 100644 --- a/UserService/Shared/NavMenu.razor +++ b/UserService/Shared/NavMenu.razor @@ -23,6 +23,11 @@ Security groups +