Compare commits
4 Commits
ae14f5842e
...
e80ec2cf8d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e80ec2cf8d | ||
|
|
25995e8d48 | ||
|
|
9e11463ef2 | ||
|
|
622cb1f702 |
@@ -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
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DigitalData.UserManager.Domain.Entities
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
*/
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user