53 lines
2.0 KiB
C#
53 lines
2.0 KiB
C#
#nullable enable
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using UserService.DatabaseLayer.DataModels;
|
|
using UserService.Infrastructure.DataModels;
|
|
|
|
namespace UserService.DatabaseLayer.Repositories
|
|
{
|
|
public class SecurityGroupsRepository : BaseRepository<SecurityGroup>, ISecurityGroupsRepository
|
|
{
|
|
public SecurityGroupsRepository() : base(x => x.SecurityGroups)
|
|
{
|
|
}
|
|
|
|
public override async Task<IReadOnlyList<SecurityGroup>> GetAllAsync(Expression<Func<SecurityGroup, bool>>? predicate = null, CancellationToken token = default)
|
|
{
|
|
await using var db = new UserServiceDbContext();
|
|
return await Context(db)
|
|
.Include(x => x.Parent)
|
|
.Include(x => x.Members)
|
|
.ThenInclude(x=> x.AttachedMember)
|
|
.WhereOrDefault(predicate)
|
|
.ToListAsync(token).ConfigureAwait(false);
|
|
}
|
|
|
|
public override async Task<SecurityGroup?> GetAsync(Expression<Func<SecurityGroup, bool>> predicate, CancellationToken token = default)
|
|
{
|
|
await using var db = new UserServiceDbContext();
|
|
return await Context(db)
|
|
.Include(x => x.Parent)
|
|
.Include(x => x.Members)
|
|
.ThenInclude(x=> x.AttachedMember)
|
|
.FirstOrDefaultAsync(predicate, token).ConfigureAwait(false);
|
|
}
|
|
|
|
public override async Task<bool> UpdateAsync(SecurityGroup entity, CancellationToken token = default)
|
|
{
|
|
if (entity == null) throw new ArgumentNullException(nameof(entity));
|
|
|
|
await using var db = new UserServiceDbContext();
|
|
Context(db).Update(entity);
|
|
db.UserMembers.UpdateRange(entity.Members);
|
|
var items = await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
return items > 0;
|
|
}
|
|
}
|
|
} |