Added very nice DataGrid control

This commit is contained in:
2020-08-04 22:47:35 +02:00
parent 8eace42aaa
commit 5d3c64d193
20 changed files with 157 additions and 376 deletions

View File

@ -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; }
/// <inheritdoc />
public override string ToString()
{
var sb = new StringBuilder();
if (Level != 0)
{
sb.Append("|");
}
sb.Append('-', Level * 4);
return sb + CommonName;
}
}
public class SecurityGroup : Member

View File

@ -0,0 +1,17 @@
using System.Linq.Expressions;
// ReSharper disable once CheckNamespace
namespace System.Linq
{
public static class Queryable
{
/// <summary>
/// Makes a where filtering, if it is not null.
/// </summary>
public static IQueryable<TSource> WhereOrDefault<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>>? predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return predicate is null ? source : source.Where(predicate);
}
}
}

View File

@ -22,10 +22,7 @@ namespace UserService.DatabaseLayer.Repository
public virtual async Task<IReadOnlyList<T>> GetAllAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
IQueryable<T> 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<T?> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken token = default)

View File

@ -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<OrganizationUnit>, IOrganizationUnitsRepository
{
public OrganizationUnitsRepository() : base(x => x.OrganizationUnits)
{
}
/// <inheritdoc cref="GetAllAsync" />
public override async Task<IReadOnlyList<OrganizationUnit>> GetAllAsync(
Expression<Func<OrganizationUnit, bool>>? predicate = null, CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
var result = new List<OrganizationUnit>();
var rootOus = await Context(db)
.Include(x => x.Parent)
.WhereOrDefault(predicate)
.ToListAsync(cancellationToken: token);
IEnumerable<OrganizationUnit> 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;
}
}
}

View File

@ -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<OrganizationUnit>, IOrganizationUnitsRepository
{
public OrganizationUnitsRepository() : base(x => x.OrganizationUnits)
{
}
/// <inheritdoc />
public override async Task<IReadOnlyList<OrganizationUnit>> GetAllAsync(CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
var result = new List<OrganizationUnit>();
var rootOus = await Context(db)
.Include(x => x.Parent)
.ToListAsync(token);
IEnumerable<OrganizationUnit> 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<SecurityGroup>, ISecurityGroupsRepository
{
public SecurityGroupsRepository() : base(x=> x.SecurityGroups)
{
}
}
public class UsersRepository : BaseRepository<User>, IUsersRepository
{
public UsersRepository() : base(x => x.Users)
{
}
}
public class NodesRepository : INodesRepository
{
public NodesRepository()
{
}
public static async IAsyncEnumerable<Node> 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<IReadOnlyList<Node>> GetAllAsync(CancellationToken token = default)
{
var list = new List<Node>();
await foreach (var node in GetNodesAsync(token))
{
list.Add(node);
}
return list;
}
public Task<Node?> GetAsync(Expression<Func<Node, bool>> 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();
}
}
}

View File

@ -0,0 +1,11 @@
using UserService.DatabaseLayer.DataModels;
namespace UserService.DatabaseLayer.Repository
{
public class SecurityGroupsRepository : BaseRepository<SecurityGroup>, ISecurityGroupsRepository
{
public SecurityGroupsRepository() : base(x => x.SecurityGroups)
{
}
}
}

View File

@ -0,0 +1,11 @@
using UserService.DatabaseLayer.DataModels;
namespace UserService.DatabaseLayer.Repository
{
public class UsersRepository : BaseRepository<User>, IUsersRepository
{
public UsersRepository() : base(x => x.Users)
{
}
}
}