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 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 GetByIdAsync(Guid roleId, CancellationToken ct = default) { return await _dbContext.Roles.FirstOrDefaultAsync(r => r.RoleId == roleId, ct); } public async Task GetByCodeAsync(string code, CancellationToken ct = default) { return await _dbContext.Roles.FirstOrDefaultAsync(r => r.Code == code, ct); } public async Task> ListAsync(CancellationToken ct = default) { return await _dbContext.Roles .AsNoTracking() .OrderBy(r => r.Code) .ToListAsync(ct); } public async Task 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; } } }