add role api
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using AuthHex.Interfaces;
|
||||
using AuthHex.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace AuthHex.Repos
|
||||
{
|
||||
public class RoleManageRepository : IRoleManageRepository
|
||||
{
|
||||
private readonly AppDBContext _dbContext;
|
||||
|
||||
public RoleManageRepository(AppDBContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public Task<Role> AddRoleAsync(Role role, CancellationToken ct = default)
|
||||
{
|
||||
_dbContext.Roles.Add(role);
|
||||
return Task.FromResult(role);
|
||||
}
|
||||
|
||||
public async Task<Role?> GetRoleByIdAsync(Guid roleId, CancellationToken ct = default)
|
||||
{
|
||||
return await _dbContext.Roles.FirstOrDefaultAsync(r => r.RoleId == roleId, ct);
|
||||
}
|
||||
|
||||
public async Task<List<Role>> ListRolesAsync(CancellationToken ct = default)
|
||||
{
|
||||
return await _dbContext.Roles.OrderBy(r => r.Code).ToListAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<bool> RoleInUseAsync(Guid roleId, CancellationToken ct = default)
|
||||
{
|
||||
return await _dbContext.Users.AnyAsync(u => u.RoleId == roleId, ct);
|
||||
}
|
||||
|
||||
public Task DeleteRoleAsync(Role role, CancellationToken ct = default)
|
||||
{
|
||||
_dbContext.Roles.Remove(role);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user