51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
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;
|
|
using UserService.Infrastructure.DataModels;
|
|
|
|
namespace UserService.DatabaseLayer.Repositories
|
|
{
|
|
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 rootOus = await Context(db)
|
|
.Include(x => x.Parent)
|
|
.WhereOrDefault(predicate)
|
|
.ToListAsync(token).ConfigureAwait(false);
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
var result = new List<OrganizationUnit>();
|
|
foreach (var ou in rootOus.Where(x => x.ParentId is null))
|
|
{
|
|
result.AddRange(Rec(ou));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
} |