feat: ReadAsync-Methode zum GroupOfUserRepository für flexible Abfragen hinzufügen

This commit is contained in:
Developer 02 2024-10-29 16:58:40 +01:00
parent 25995e8d48
commit e80ec2cf8d
4 changed files with 37 additions and 4 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

@ -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

@ -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

@ -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;