From 5d3c64d193454f1b7cf580c53f4bc1dde78efbad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Tue, 4 Aug 2020 22:47:35 +0200 Subject: [PATCH] Added very nice DataGrid control --- UserService.DatabaseLayer/DataModels/Node.cs | 17 --- UserService.DatabaseLayer/Queryable.cs | 17 +++ .../Repository/BaseRepository.cs | 5 +- .../Repository/OrganizationUnitsRepository.cs | 50 ++++++++ .../Repository/Repository.cs | 121 ------------------ .../Repository/SecurityGroupsRepository.cs | 11 ++ .../Repository/UsersRepository.cs | 11 ++ UserService.Test/UnitTest1.cs | 11 +- UserService.db | Bin 40960 -> 40960 bytes UserService/App.razor | 5 +- UserService/Pages/Directory.razor | 14 +- UserService/Pages/OrganizationUnits.razor | 65 ---------- UserService/Pages/OrganizationUnits.razor.cs | 41 ------ UserService/Pages/SecurityGroups.razor | 58 ++------- UserService/Pages/Users.razor | 73 +++-------- UserService/Pages/Users.razor.cs | 12 +- UserService/Shared/NavMenu.razor | 18 ++- UserService/Startup.cs | 1 - UserService/UserService.csproj | 1 + UserService/_Imports.razor | 2 +- 20 files changed, 157 insertions(+), 376 deletions(-) create mode 100644 UserService.DatabaseLayer/Queryable.cs create mode 100644 UserService.DatabaseLayer/Repository/OrganizationUnitsRepository.cs delete mode 100644 UserService.DatabaseLayer/Repository/Repository.cs create mode 100644 UserService.DatabaseLayer/Repository/SecurityGroupsRepository.cs create mode 100644 UserService.DatabaseLayer/Repository/UsersRepository.cs delete mode 100644 UserService/Pages/OrganizationUnits.razor delete mode 100644 UserService/Pages/OrganizationUnits.razor.cs diff --git a/UserService.DatabaseLayer/DataModels/Node.cs b/UserService.DatabaseLayer/DataModels/Node.cs index 5484f05..881ad38 100644 --- a/UserService.DatabaseLayer/DataModels/Node.cs +++ b/UserService.DatabaseLayer/DataModels/Node.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.Linq; using System.Text; namespace UserService.DatabaseLayer.DataModels @@ -9,22 +8,6 @@ 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 diff --git a/UserService.DatabaseLayer/Queryable.cs b/UserService.DatabaseLayer/Queryable.cs new file mode 100644 index 0000000..bc15ede --- /dev/null +++ b/UserService.DatabaseLayer/Queryable.cs @@ -0,0 +1,17 @@ +using System.Linq.Expressions; + +// ReSharper disable once CheckNamespace +namespace System.Linq +{ + public static class Queryable + { + /// + /// Makes a where filtering, if it is not null. + /// + public static IQueryable WhereOrDefault(this IQueryable source, Expression>? predicate) + { + if (source == null) throw new ArgumentNullException(nameof(source)); + return predicate is null ? source : source.Where(predicate); + } + } +} diff --git a/UserService.DatabaseLayer/Repository/BaseRepository.cs b/UserService.DatabaseLayer/Repository/BaseRepository.cs index c9e58b0..32065b7 100644 --- a/UserService.DatabaseLayer/Repository/BaseRepository.cs +++ b/UserService.DatabaseLayer/Repository/BaseRepository.cs @@ -22,10 +22,7 @@ namespace UserService.DatabaseLayer.Repository public virtual async Task> GetAllAsync(Expression>? predicate = null, CancellationToken token = default) { await using var db = new UserServiceDbContext(); - - IQueryable queryable = Context(db).Include(x => x.Parent); - if(predicate != null) queryable = queryable.Where(predicate); - return await queryable.ToListAsync(token); + return await Context(db).Include(x => x.Parent).WhereOrDefault(predicate).ToListAsync(token); } public async Task GetAsync(Expression> predicate, CancellationToken token = default) diff --git a/UserService.DatabaseLayer/Repository/OrganizationUnitsRepository.cs b/UserService.DatabaseLayer/Repository/OrganizationUnitsRepository.cs new file mode 100644 index 0000000..08e5222 --- /dev/null +++ b/UserService.DatabaseLayer/Repository/OrganizationUnitsRepository.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using UserService.DatabaseLayer.DataModels; + +namespace UserService.DatabaseLayer.Repository +{ + public class OrganizationUnitsRepository : BaseRepository, IOrganizationUnitsRepository + { + public OrganizationUnitsRepository() : base(x => x.OrganizationUnits) + { + } + + /// + public override async Task> GetAllAsync( + Expression>? predicate = null, CancellationToken token = default) + { + await using var db = new UserServiceDbContext(); + var result = new List(); + var rootOus = await Context(db) + .Include(x => x.Parent) + .WhereOrDefault(predicate) + .ToListAsync(cancellationToken: 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; + } + } +} \ No newline at end of file diff --git a/UserService.DatabaseLayer/Repository/Repository.cs b/UserService.DatabaseLayer/Repository/Repository.cs deleted file mode 100644 index 86afdd3..0000000 --- a/UserService.DatabaseLayer/Repository/Repository.cs +++ /dev/null @@ -1,121 +0,0 @@ -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 -{ - public class OrganizationUnitsRepository : BaseRepository, IOrganizationUnitsRepository - { - 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 - { - public SecurityGroupsRepository() : base(x=> x.SecurityGroups) - { - } - } - - - public class UsersRepository : BaseRepository, IUsersRepository - { - public UsersRepository() : base(x => x.Users) - { - } - } - - public class NodesRepository : INodesRepository - { - public NodesRepository() - { - } - - public static async IAsyncEnumerable GetNodesAsync([EnumeratorCancellation] CancellationToken token = default) - { - await using var db = new UserServiceDbContext(); - await foreach (var note in db.OrganizationUnits.AsAsyncEnumerable().WithCancellation(token)) - { - yield return note; - } - await foreach (var node in db.SecurityGroups.AsAsyncEnumerable().WithCancellation(token)) - { - yield return node; - } - - await foreach (var node in db.Users.AsAsyncEnumerable().WithCancellation(token)) - { - yield return node; - } - } - - public async Task> GetAllAsync(CancellationToken token = default) - { - - var list = new List(); - await foreach (var node in GetNodesAsync(token)) - { - list.Add(node); - } - - return list; - } - - public Task GetAsync(Expression> predicate, CancellationToken token = default) - { - throw new NotImplementedException(); - } - - public Task AddAsync(Node entity, CancellationToken token = default) - { - throw new NotImplementedException(); - } - - public Task UpdateAsync(Node entity, CancellationToken token = default) - { - throw new NotImplementedException(); - } - - public Task DeleteAsync(Node entity, CancellationToken token = default) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/UserService.DatabaseLayer/Repository/SecurityGroupsRepository.cs b/UserService.DatabaseLayer/Repository/SecurityGroupsRepository.cs new file mode 100644 index 0000000..0f4ce54 --- /dev/null +++ b/UserService.DatabaseLayer/Repository/SecurityGroupsRepository.cs @@ -0,0 +1,11 @@ +using UserService.DatabaseLayer.DataModels; + +namespace UserService.DatabaseLayer.Repository +{ + public class SecurityGroupsRepository : BaseRepository, ISecurityGroupsRepository + { + public SecurityGroupsRepository() : base(x => x.SecurityGroups) + { + } + } +} \ No newline at end of file diff --git a/UserService.DatabaseLayer/Repository/UsersRepository.cs b/UserService.DatabaseLayer/Repository/UsersRepository.cs new file mode 100644 index 0000000..95421ca --- /dev/null +++ b/UserService.DatabaseLayer/Repository/UsersRepository.cs @@ -0,0 +1,11 @@ +using UserService.DatabaseLayer.DataModels; + +namespace UserService.DatabaseLayer.Repository +{ + public class UsersRepository : BaseRepository, IUsersRepository + { + public UsersRepository() : base(x => x.Users) + { + } + } +} \ No newline at end of file diff --git a/UserService.Test/UnitTest1.cs b/UserService.Test/UnitTest1.cs index 10d9223..4fd7428 100644 --- a/UserService.Test/UnitTest1.cs +++ b/UserService.Test/UnitTest1.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using NUnit.Framework; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; using UserService.DatabaseLayer.DataModels; namespace UserService.Test @@ -45,7 +43,6 @@ namespace UserService.Test NewMethod(ous, null, 0, ref sb); var result = sb.ToString(); Assert.Pass(); - } private static void NewMethod(IEnumerable ous, Node parent, int level, ref StringBuilder sb) diff --git a/UserService.db b/UserService.db index a58f715c4fe64657c72fe9d82a82469b3d78bf70..0dafeee3a7baea65d68fcf3ce55865aec5426161 100644 GIT binary patch delta 154 zcmZoTz|?SnX#7c=tTh5aQ!s$;f|`{~P~${wMq& m_+Rng+bmJg%|9`KWAe)SXl6eCU6a`xK-#A9Z{F4KCIA2(xElfh diff --git a/UserService/App.razor b/UserService/App.razor index ece7489..9c5b0e0 100644 --- a/UserService/App.razor +++ b/UserService/App.razor @@ -1,5 +1,4 @@ - - + @@ -10,4 +9,4 @@ - + \ No newline at end of file diff --git a/UserService/Pages/Directory.razor b/UserService/Pages/Directory.razor index 39bb61c..a7eed1e 100644 --- a/UserService/Pages/Directory.razor +++ b/UserService/Pages/Directory.razor @@ -1,14 +1,6 @@ @page "/directory" -@using UserService.DatabaseLayer.Repository -@inject IOrganizationUnitsRepository OuRepository @inherits DirectoryBase -@functions { - - - -} -

TODO

@@ -29,12 +21,12 @@ else HasChildNodes="@(item => item?.Children?.Any() == true)" @bind-SelectedNode="@SelectedNode"> - + @context?.CommonName - + +
-
diff --git a/UserService/Pages/OrganizationUnits.razor b/UserService/Pages/OrganizationUnits.razor deleted file mode 100644 index ff8896c..0000000 --- a/UserService/Pages/OrganizationUnits.razor +++ /dev/null @@ -1,65 +0,0 @@ -@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 deleted file mode 100644 index 637a122..0000000 --- a/UserService/Pages/OrganizationUnits.razor.cs +++ /dev/null @@ -1,41 +0,0 @@ -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 0904a02..e21b1e7 100644 --- a/UserService/Pages/SecurityGroups.razor +++ b/UserService/Pages/SecurityGroups.razor @@ -1,59 +1,21 @@ @page "/securitygroups" @using UserService.DatabaseLayer.DataModels -@using UserService.DatabaseLayer.Repository @inherits SecurityGroupsBase

Table of all security groups

@if (SecurityGroups is null) { -

- Loading... -

+

+ Loading... +

} else { - @* - - Common Name - Description - E-Mail - Parent - - - - - @context.CommonName - @context.Description - @context.EMail - @context.Parent - edit - delete - - - - Create new group*@ -} - -@* - @if (SecurityGroupToEdit != null) - { - @(SecurityGroupToEdit.Id == 0 ? "New" : "Edit") @SecurityGroupToEdit.CommonName (@SecurityGroupToEdit.Id) - - -

- - -

- - - } - else - { - No securityGroup selected - } - - No Thanks - OK - - *@ \ No newline at end of file + + + + + + +} \ No newline at end of file diff --git a/UserService/Pages/Users.razor b/UserService/Pages/Users.razor index 77ac95a..ed95eb6 100644 --- a/UserService/Pages/Users.razor +++ b/UserService/Pages/Users.razor @@ -12,55 +12,24 @@ } else { - @* - - Common Name - Full name - Description - E-Mail - Parent - Is active - - - - - @context.CommonName - @context.FullName - @context.Description - @context.EMail - @context.Parent - @context.IsActive - edit - delete - - - - 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 + + + + + + + + + + + + + + + + + + + + +} \ No newline at end of file diff --git a/UserService/Pages/Users.razor.cs b/UserService/Pages/Users.razor.cs index 8aac50e..0c712a9 100644 --- a/UserService/Pages/Users.razor.cs +++ b/UserService/Pages/Users.razor.cs @@ -1,6 +1,7 @@ -using System.Collections.Generic; +using Microsoft.AspNetCore.Components; +using System.Collections.Generic; using System.Threading.Tasks; -using Microsoft.AspNetCore.Components; +using Blazorise.DataGrid; using UserService.DatabaseLayer.DataModels; using UserService.DatabaseLayer.Repository; @@ -40,5 +41,12 @@ namespace UserService.Pages { await UsersRepository.DeleteAsync(user).ConfigureAwait(false); } + + protected async Task RowInsertedCallback(SavedRowItem> obj) + { + var user = obj.Item; + user.ParentId = -2; + await UsersRepository.AddAsync(user); + } } } \ No newline at end of file diff --git a/UserService/Shared/NavMenu.razor b/UserService/Shared/NavMenu.razor index 5e569fa..3fb8567 100644 --- a/UserService/Shared/NavMenu.razor +++ b/UserService/Shared/NavMenu.razor @@ -9,18 +9,30 @@ { Brand = new SidebarBrandInfo { - Text = "SidebarBrand", + Text = "User service", To = "/" - }, Items = new List + }, + Items = new List { new SidebarItemInfo { Text = "Directory", Icon = IconName.Folder, To = "/directory" + }, + new SidebarItemInfo + { + Text = "Users", + Icon = IconName.Users, + To = "/users" + }, + new SidebarItemInfo + { + Text = "Security groups", + Icon = IconName.Book, + To = "/securitygroups" } } }; } - } \ No newline at end of file diff --git a/UserService/Startup.cs b/UserService/Startup.cs index b3c667e..dfd5430 100644 --- a/UserService/Startup.cs +++ b/UserService/Startup.cs @@ -1,4 +1,3 @@ -using System.Net.Http; using Blazorise; using Blazorise.Bootstrap; using Blazorise.Icons.FontAwesome; diff --git a/UserService/UserService.csproj b/UserService/UserService.csproj index 990bac0..ebd1211 100644 --- a/UserService/UserService.csproj +++ b/UserService/UserService.csproj @@ -11,6 +11,7 @@ + diff --git a/UserService/_Imports.razor b/UserService/_Imports.razor index ea8639c..8a87504 100644 --- a/UserService/_Imports.razor +++ b/UserService/_Imports.razor @@ -10,4 +10,4 @@ @using Blazorise @using Blazorise.Sidebar @using Blazorise.TreeView - +@using Blazorise.DataGrid