54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using AuthHex.Interfaces;
|
|
using AuthHex.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace AuthHex.Repos
|
|
{
|
|
public class RoleRepository : IRoleRepository
|
|
{
|
|
private readonly AppDBContext _dbContext;
|
|
|
|
public RoleRepository(AppDBContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public Task<Role> AddRoleAsync(Role role, CancellationToken ct = default)
|
|
{
|
|
// SaveChanges is deferred to the Unit of Work commit, mirroring UserManageRepository.
|
|
_dbContext.Roles.Add(role);
|
|
return Task.FromResult(role);
|
|
}
|
|
|
|
public async Task<Role?> GetByIdAsync(Guid roleId, CancellationToken ct = default)
|
|
{
|
|
return await _dbContext.Roles.FirstOrDefaultAsync(r => r.RoleId == roleId, ct);
|
|
}
|
|
|
|
public async Task<Role?> GetByCodeAsync(string code, CancellationToken ct = default)
|
|
{
|
|
return await _dbContext.Roles.FirstOrDefaultAsync(r => r.Code == code, ct);
|
|
}
|
|
|
|
public async Task<List<Role>> ListAsync(CancellationToken ct = default)
|
|
{
|
|
return await _dbContext.Roles
|
|
.AsNoTracking()
|
|
.OrderBy(r => r.Code)
|
|
.ToListAsync(ct);
|
|
}
|
|
|
|
public async Task<bool> IsAssignedToAnyUserAsync(Guid roleId, CancellationToken ct = default)
|
|
{
|
|
return await _dbContext.Users.AnyAsync(u => u.RoleId == roleId, ct);
|
|
}
|
|
|
|
public Task DeleteRoleAsync(Role role, CancellationToken ct = default)
|
|
{
|
|
// SaveChanges is deferred to the Unit of Work commit.
|
|
_dbContext.Roles.Remove(role);
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
}
|