more code behind. and more orga power

This commit is contained in:
2020-07-30 22:35:22 +02:00
parent 114b52c963
commit 61b32841bd
12 changed files with 231 additions and 91 deletions

View File

@ -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; }
/// <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
@ -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;
/// <inheritdoc />
public virtual object Clone() => MemberwiseClone();

View File

@ -12,14 +12,14 @@ namespace UserService.DatabaseLayer.Repository
{
public class BaseRepository<T> where T : Node
{
private readonly Func<UserServiceDbContext, DbSet<T>> _context;
protected readonly Func<UserServiceDbContext, DbSet<T>> _context;
protected BaseRepository(Func<UserServiceDbContext, DbSet<T>> context)
{
_context = context;
}
public async Task<IReadOnlyList<T>> GetAllAsync(CancellationToken token = default)
public virtual async Task<IReadOnlyList<T>> GetAllAsync(CancellationToken token = default)
{
await using var db = new UserServiceDbContext();
return await _context(db).Include(x => x.Parent).ToListAsync(token);

View File

@ -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)
{
}
/// <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