fix: filter soft-deleted roles and soft delete on remove

This commit is contained in:
2025-12-06 15:40:57 +08:00
parent 2b569d3d6f
commit 33a3b349c4

View File

@@ -10,20 +10,20 @@ namespace TakeoutSaaS.Infrastructure.Identity.Persistence;
public sealed class EfRoleRepository(IdentityDbContext dbContext) : IRoleRepository public sealed class EfRoleRepository(IdentityDbContext dbContext) : IRoleRepository
{ {
public Task<Role?> FindByIdAsync(long roleId, long tenantId, CancellationToken cancellationToken = default) public Task<Role?> FindByIdAsync(long roleId, long tenantId, CancellationToken cancellationToken = default)
=> dbContext.Roles.AsNoTracking().FirstOrDefaultAsync(x => x.Id == roleId && x.TenantId == tenantId, cancellationToken); => dbContext.Roles.AsNoTracking().FirstOrDefaultAsync(x => x.Id == roleId && x.TenantId == tenantId && x.DeletedAt == null, cancellationToken);
public Task<Role?> FindByCodeAsync(string code, long tenantId, CancellationToken cancellationToken = default) public Task<Role?> FindByCodeAsync(string code, long tenantId, CancellationToken cancellationToken = default)
=> dbContext.Roles.AsNoTracking().FirstOrDefaultAsync(x => x.Code == code && x.TenantId == tenantId, cancellationToken); => dbContext.Roles.AsNoTracking().FirstOrDefaultAsync(x => x.Code == code && x.TenantId == tenantId && x.DeletedAt == null, cancellationToken);
public Task<IReadOnlyList<Role>> GetByIdsAsync(long tenantId, IEnumerable<long> roleIds, CancellationToken cancellationToken = default) public Task<IReadOnlyList<Role>> GetByIdsAsync(long tenantId, IEnumerable<long> roleIds, CancellationToken cancellationToken = default)
=> dbContext.Roles.AsNoTracking() => dbContext.Roles.AsNoTracking()
.Where(x => x.TenantId == tenantId && roleIds.Contains(x.Id)) .Where(x => x.TenantId == tenantId && roleIds.Contains(x.Id) && x.DeletedAt == null)
.ToListAsync(cancellationToken) .ToListAsync(cancellationToken)
.ContinueWith(t => (IReadOnlyList<Role>)t.Result, cancellationToken); .ContinueWith(t => (IReadOnlyList<Role>)t.Result, cancellationToken);
public Task<IReadOnlyList<Role>> SearchAsync(long tenantId, string? keyword, CancellationToken cancellationToken = default) public Task<IReadOnlyList<Role>> SearchAsync(long tenantId, string? keyword, CancellationToken cancellationToken = default)
{ {
var query = dbContext.Roles.AsNoTracking().Where(x => x.TenantId == tenantId); var query = dbContext.Roles.AsNoTracking().Where(x => x.TenantId == tenantId && x.DeletedAt == null);
if (!string.IsNullOrWhiteSpace(keyword)) if (!string.IsNullOrWhiteSpace(keyword))
{ {
var normalized = keyword.Trim(); var normalized = keyword.Trim();
@@ -51,7 +51,8 @@ public sealed class EfRoleRepository(IdentityDbContext dbContext) : IRoleReposit
var entity = await dbContext.Roles.FirstOrDefaultAsync(x => x.Id == roleId && x.TenantId == tenantId, cancellationToken); var entity = await dbContext.Roles.FirstOrDefaultAsync(x => x.Id == roleId && x.TenantId == tenantId, cancellationToken);
if (entity != null) if (entity != null)
{ {
dbContext.Roles.Remove(entity); entity.DeletedAt = DateTime.UtcNow;
dbContext.Roles.Update(entity);
} }
} }