Compare commits

...

4 Commits

Author SHA1 Message Date
Developer 02
e80ec2cf8d feat: ReadAsync-Methode zum GroupOfUserRepository für flexible Abfragen hinzufügen 2024-10-29 16:58:40 +01:00
Developer 02
25995e8d48 feat: Unterstützung für die Filterung nach RepGroupId in der ReadAllAsync-Methode hinzufügen 2024-10-29 16:33:41 +01:00
Developer 02
9e11463ef2 feat: readOnly-Parameter zur ReadAllAsync-Methode im UserRepRepository hinzufügen 2024-10-29 16:27:29 +01:00
Developer 02
622cb1f702 feat: Implementiere kaskadierende Löschung für die User-Entität im UserRepository
- Überriding der DeleteAsync-Methode hinzugefügt, um verwandte ModuleOfUser- und GroupOfUser-Entitäten zu behandeln.
- Sicherstellung der Entfernung verwandter Datensätze bei der Löschung eines Benutzers.
- TODO-Kommentar hinzugefügt, um zu empfehlen, .OnDelete(DeleteBehavior.ClientCascade) im DbContext für ein verbessertes Verhalten zu verwenden.
2024-10-29 16:13:58 +01:00
8 changed files with 77 additions and 13 deletions

View File

@@ -1,5 +1,4 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DigitalData.UserManager.Domain.Entities

View File

@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace DigitalData.UserManager.Domain.Entities
{

View File

@@ -18,5 +18,11 @@ namespace DigitalData.UserManager.Infrastructure.Contracts
Task<IEnumerable<GroupOfUser>> ReadByUsernameAsync(string username);
Task<IEnumerable<GroupOfUser>> ReadByUserIdAsync(int userId);
// merge all GroupOfUserRepository-methods conditionally under this method
Task<IEnumerable<GroupOfUser>> ReadAsync(
bool readOnly = true,
bool withGroup = true, bool withUser = true,
int? id = null, int? groupId = null, int? userId = null, string? username = null);
}
}

View File

@@ -5,6 +5,8 @@ namespace DigitalData.UserManager.Infrastructure.Contracts
{
public interface IUserRepRepository : ICRUDRepository<UserRep, int>
{
Task<IEnumerable<UserRep>> ReadAllAsync(bool withUser = false, bool withRepGroup = false, bool withGroup = false, bool withRepUser = false, int? userId = null, int? groupId = null);
Task<IEnumerable<UserRep>> ReadAllAsync(
bool withUser = false, bool withRepGroup = false, bool withGroup = false, bool withRepUser = false,
int? userId = null, int? groupId = null, int? repGroupId = null, bool readOnly = true);
}
}

View File

@@ -32,4 +32,19 @@ namespace DigitalData.UserManager.Infrastructure
return modelBuilder;
}
}
}
}
//TODO: DeleteBehavior.Restrict (see example comment below) is not working, probably due to table relationships. Because it tries to delete, for example, User first and then GroupOfUser. However, since GroupOfUser is not deleted while Users is deleted, the error is thrown because of the table relationship. Apply this approach after finding the solution.
/*
modelBuilder.Entity<User>()
.HasMany<GroupOfUser>()
.WithOne(gou => gou.User)
.HasForeignKey(gou => gou.UserId)
.OnDelete(DeleteBehavior.ClientCascade);
modelBuilder.Entity<User>()
.HasMany<ModuleOfUser>()
.WithOne(mou => mou.User)
.HasForeignKey(mou => mou.UserId)
.OnDelete(DeleteBehavior.ClientCascade);
*/

View File

@@ -42,5 +42,33 @@ namespace DigitalData.UserManager.Infrastructure.Repositories
public async Task<IEnumerable<GroupOfUser>> ReadByUsernameAsync(string username) => await ReadByUsername(username).ToListAsync();
public async Task<IEnumerable<GroupOfUser>> ReadByUserIdAsync(int userId) => await ReadByUserId(userId).ToListAsync();
public async Task<IEnumerable<GroupOfUser>> ReadAsync(
bool readOnly = true,
bool withGroup = true, bool withUser = true,
int? id = null, int? groupId = null, int? userId = null, string? username = null)
{
var query = readOnly ? _dbSet.AsNoTracking() : _dbSet.AsQueryable();
if (withGroup)
query = query.Include(gou => gou.Group);
if (withUser)
query = query.Include(gou => gou.User);
if (id is not null)
query = query.Where(gou => gou.Id == id);
if (groupId is not null)
query = query.Where(gou => gou.GroupId == groupId);
if (userId is not null)
query = query.Where(gou => gou.UserId == userId);
if (username is not null)
query = query.Where(gou => gou.User!.Username == username);
return await query.ToListAsync();
}
}
}

View File

@@ -12,9 +12,11 @@ namespace DigitalData.UserManager.Infrastructure.Repositories
{
}
public async Task<IEnumerable<UserRep>> ReadAllAsync(bool withUser = false, bool withRepGroup = false, bool withGroup = false, bool withRepUser = false, int? userId = null, int? groupId = null)
public async Task<IEnumerable<UserRep>> ReadAllAsync(
bool withUser = false, bool withRepGroup = false, bool withGroup = false, bool withRepUser = false,
int? userId = null, int? groupId = null, int? repGroupId = null, bool readOnly = true)
{
var query = _dbSet.AsNoTracking();
var query = readOnly ? _dbSet.AsNoTracking() : _dbSet.AsQueryable();
if (withUser)
query = query.Include(ur => ur.User);
@@ -29,14 +31,13 @@ namespace DigitalData.UserManager.Infrastructure.Repositories
query = query.Include(ur => ur.RepUser);
if(userId is not null)
{
query = query.Where(ur => ur.UserId == userId);
}
if (groupId is not null)
{
query = query.Where(ur => ur.GroupId == groupId);
}
if (repGroupId is not null)
query = query.Where(ur => ur.RepGroupId == repGroupId);
return await query.ToListAsync();
}

View File

@@ -8,8 +8,8 @@ namespace DigitalData.UserManager.Infrastructure.Repositories
public class UserRepository<TDbContext> : CRUDRepository<User, int, TDbContext>, IUserRepository
where TDbContext : DbContext, IUserManagerDbContext
{
private IModuleOfUserRepository _moduleOfUserRepo;
private IGroupOfUserRepository _groupOfUserRepo;
private readonly IModuleOfUserRepository _moduleOfUserRepo;
private readonly IGroupOfUserRepository _groupOfUserRepo;
public UserRepository(TDbContext dbContext, IModuleOfUserRepository moduleOfUserRepo, IGroupOfUserRepository groupOfUserRepo) : base(dbContext, dbContext.Users)
{
_moduleOfUserRepo = moduleOfUserRepo;
@@ -44,5 +44,19 @@ namespace DigitalData.UserManager.Infrastructure.Repositories
{
return await _dbSet.Where(user => user.Username == username).FirstOrDefaultAsync();
}
//TODO: instead of this implmenet .OnDelete(DeleteBehavior.ClientCascade) in DbContext
public override async Task<bool> DeleteAsync(User user)
{
IEnumerable<ModuleOfUser> mou = await _moduleOfUserRepo.ReadByUserAsync(user.Username);
if(mou.Any())
_dbContext.RemoveRange(mou);
IEnumerable<GroupOfUser> gou = await _groupOfUserRepo.ReadByUsernameAsync(user.Username);
if(gou.Any())
_dbContext.RemoveRange(gou);
return await base.DeleteAsync(user);
}
}
}