29 lines
1.5 KiB
C#
29 lines
1.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using Mws.Application.Common.Repositories;
|
||
|
|
using Mws.Domain.Roles;
|
||
|
|
|
||
|
|
namespace Mws.Infrastructure.Persistence.Repositories;
|
||
|
|
|
||
|
|
public class RoleRepository(AppDbContext db) : RepositoryBase<Role>(db), IRoleRepository
|
||
|
|
{
|
||
|
|
public Task<List<Role>> GetAllWithPermissionsAsync(CancellationToken ct = default) =>
|
||
|
|
Set.Include(r => r.Permissions).OrderBy(r => r.Name).ToListAsync(ct);
|
||
|
|
|
||
|
|
public Task<Role?> GetByIdWithPermissionsAsync(Guid id, CancellationToken ct = default) =>
|
||
|
|
Set.Include(r => r.Permissions).FirstOrDefaultAsync(r => r.Id == id, ct);
|
||
|
|
|
||
|
|
public Task<Role?> GetByIdAsync(Guid id, CancellationToken ct = default) =>
|
||
|
|
Set.FirstOrDefaultAsync(r => r.Id == id, ct);
|
||
|
|
|
||
|
|
public Task<bool> ExistsByNameAsync(string name, Guid? excludeId, CancellationToken ct = default) =>
|
||
|
|
Set.AnyAsync(r => r.Name == name && (excludeId == null || r.Id != excludeId), ct);
|
||
|
|
|
||
|
|
public Task<RolePermission?> GetPermissionAsync(Guid roleId, string screen, CancellationToken ct = default) =>
|
||
|
|
Db.RolePermissions.FirstOrDefaultAsync(p => p.RoleId == roleId && p.Screen == screen, ct);
|
||
|
|
|
||
|
|
public Task<Dictionary<string, RolePermission>> GetPermissionsAsync(Guid roleId, CancellationToken ct = default) =>
|
||
|
|
Db.RolePermissions.Where(p => p.RoleId == roleId).ToDictionaryAsync(p => p.Screen, ct);
|
||
|
|
|
||
|
|
public void RemovePermissions(IEnumerable<RolePermission> permissions) => Db.RolePermissions.RemoveRange(permissions);
|
||
|
|
}
|