121 lines
3.7 KiB
C#
121 lines
3.7 KiB
C#
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();
|
|
}
|
|
}
|
|
} |