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(); } } }