Bring the autocomplete bo to live
This commit is contained in:
57
UserService.DatabaseLayer/Repositories/BaseRepository.cs
Normal file
57
UserService.DatabaseLayer/Repositories/BaseRepository.cs
Normal file
@ -0,0 +1,57 @@
|
||||
#nullable enable
|
||||
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 BaseRepository<T> where T : Node
|
||||
{
|
||||
protected Func<UserServiceDbContext, DbSet<T>> Context { get; }
|
||||
|
||||
protected BaseRepository(Func<UserServiceDbContext, DbSet<T>> context)
|
||||
{
|
||||
Context = context;
|
||||
}
|
||||
|
||||
public virtual async Task<IReadOnlyList<T>> GetAllAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken token = default)
|
||||
{
|
||||
await using var db = new UserServiceDbContext();
|
||||
return await Context(db).Include(x => x.Parent).WhereOrDefault(predicate).ToListAsync(token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<T?> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken token = default)
|
||||
{
|
||||
await using var db = new UserServiceDbContext();
|
||||
return await Context(db).FirstOrDefaultAsync(predicate, token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task AddAsync(T entity, CancellationToken token = default)
|
||||
{
|
||||
await using var db = new UserServiceDbContext();
|
||||
await Context(db).AddAsync(@entity, token).ConfigureAwait(false);
|
||||
await db.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(T entity, CancellationToken token = default)
|
||||
{
|
||||
await using var db = new UserServiceDbContext();
|
||||
Context(db).Update(entity);
|
||||
var items= await db.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
return items > 0;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(T entity, CancellationToken token = default)
|
||||
{
|
||||
await using var db = new UserServiceDbContext();
|
||||
Context(db).Remove(entity);
|
||||
await db.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
39
UserService.DatabaseLayer/Repositories/IRepository.cs
Normal file
39
UserService.DatabaseLayer/Repositories/IRepository.cs
Normal file
@ -0,0 +1,39 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.DatabaseLayer.Repositories
|
||||
{
|
||||
public interface IRepository<T> where T : Node
|
||||
{
|
||||
Task<IReadOnlyList<T>> GetAllAsync(Expression<Func<T, bool>>? predicate = null, CancellationToken token = default);
|
||||
Task<T?> GetAsync(Expression<Func<T, bool>> predicate, CancellationToken token = default);
|
||||
Task AddAsync(T entity, CancellationToken token = default);
|
||||
Task<bool> UpdateAsync(T entity, CancellationToken token = default);
|
||||
Task DeleteAsync(T entity, CancellationToken token = default);
|
||||
}
|
||||
|
||||
public interface IOrganizationUnitsRepository : IRepository<OrganizationUnit>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface ISecurityGroupsRepository : IRepository<SecurityGroup>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface IUsersRepository : IRepository<User>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface IMembersRepository : IRepository<Member>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
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(cancellationToken: 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.DatabaseLayer.Repositories
|
||||
{
|
||||
public class SecurityGroupsRepository : BaseRepository<SecurityGroup>, ISecurityGroupsRepository
|
||||
{
|
||||
public SecurityGroupsRepository() : base(x => x.SecurityGroups)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
11
UserService.DatabaseLayer/Repositories/UsersRepository.cs
Normal file
11
UserService.DatabaseLayer/Repositories/UsersRepository.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using UserService.Infrastructure.DataModels;
|
||||
|
||||
namespace UserService.DatabaseLayer.Repositories
|
||||
{
|
||||
public class UsersRepository : BaseRepository<User>, IUsersRepository
|
||||
{
|
||||
public UsersRepository() : base(x => x.Users)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user