From 8e72d123fde180f35a37d848739f7e6763c802a7 Mon Sep 17 00:00:00 2001 From: max <max.holleman@hotmail.nl> Date: Sat, 19 Apr 2025 23:46:29 +0200 Subject: [PATCH] [CHANGE] Small updates repositories --- .../Repositories/GroupRepository.cs | 40 ++++++++++++++----- .../Repositories/UserRepository.cs | 4 +- .../Managers/AuthorityUserManager.cs | 2 +- .../Repositories/IGroupRepository.cs | 3 +- .../Repositories/IUserRepository.cs | 4 +- 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs b/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs index 3e3afcc..fbcf0da 100644 --- a/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs +++ b/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs @@ -1,3 +1,4 @@ +using DotBased.AspNet.Authority.EFCore.Models; using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Repositories; @@ -37,6 +38,28 @@ public class GroupRepository(IDbContextFactory<AuthorityContext> contextFactory, return await context.Groups.Where(g => g.Id == groupId).Include(g => g.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken); } + public async Task<bool> AddUsersToGroupAsync(List<AuthorityUser> users, AuthorityGroup group, CancellationToken cancellationToken = default) + { + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + if (!context.Groups.Any(g => g.Id == group.Id)) + { + return false; + } + + var usersToAdd = users.Where(u => !context.UserGroups.Any(ug => ug.UserId == u.Id)).ToList(); + if (usersToAdd.Count == 0) + { + return false; + } + + foreach (var user in usersToAdd) + { + context.UserGroups.Add(new UserGroups() { UserId = user.Id, GroupId = group.Id }); + } + var saveResult = await context.SaveChangesAsync(cancellationToken); + return saveResult > 0; + } + public async Task<List<AuthorityGroup>> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default) { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); @@ -72,22 +95,21 @@ public class GroupRepository(IDbContextFactory<AuthorityContext> contextFactory, logger.LogError("Group version validation failed."); return null; } - + var entry = context.Groups.Update(group); var saveResult = await context.SaveChangesAsync(cancellationToken); return saveResult != 0 ? entry.Entity : null; } - public async Task<bool> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + public async Task<bool> DeleteGroupsAsync(List<AuthorityGroup> groups, CancellationToken cancellationToken = default) { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var currentGroup = await context.Groups.FirstOrDefaultAsync(g => g.Id == group.Id, cancellationToken); - if (currentGroup == null) - { - logger.LogError("Group with id {groupId} not found.", group.Id); - return false; - } - context.Groups.Remove(currentGroup); + var groupIds = groups.Select(g => g.Id).ToList(); + + context.Groups.RemoveRange(groups); + context.UserGroups.RemoveRange(context.UserGroups.Where(ug => groupIds.Contains(ug.GroupId))); + context.RoleLinks.RemoveRange(context.RoleLinks.Where(rl => groupIds.Contains(rl.LinkId))); + var saveResult = await context.SaveChangesAsync(cancellationToken); return saveResult != 0; } diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/UserRepository.cs b/DotBased.AspNet.Authority.EFCore/Repositories/UserRepository.cs index c70fadc..8bfe0ea 100644 --- a/DotBased.AspNet.Authority.EFCore/Repositories/UserRepository.cs +++ b/DotBased.AspNet.Authority.EFCore/Repositories/UserRepository.cs @@ -8,7 +8,7 @@ namespace DotBased.AspNet.Authority.EFCore.Repositories; public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory, ILogger<UserRepository> logger) : RepositoryBase, IUserRepository { - public async Task<QueryItems<AuthorityUserItem>> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) + public async Task<QueryItems<AuthorityUserItem>> GetUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); var query = context.Users.AsQueryable(); @@ -29,7 +29,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory, return QueryItems<AuthorityUserItem>.Create(selected, totalCount, limit, offset); } - public async Task<AuthorityUser?> GetAuthorityUserByIdAsync(Guid id, CancellationToken cancellationToken = default) + public async Task<AuthorityUser?> GetUserByIdAsync(Guid id, CancellationToken cancellationToken = default) { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); if (id == Guid.Empty) diff --git a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs index 3a15a4c..abc3b4e 100755 --- a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs @@ -38,7 +38,7 @@ public partial class AuthorityManager public async Task<Result<QueryItems<AuthorityUserItem>>> SearchUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken cancellationToken = default) { - var result = await UserRepository.GetAuthorityUsersAsync(maxResults, offset, query, cancellationToken); + var result = await UserRepository.GetUsersAsync(maxResults, offset, query, cancellationToken); return result; } diff --git a/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs b/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs index 66ff9c2..4b6a4e6 100755 --- a/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs @@ -7,8 +7,9 @@ public interface IGroupRepository { public Task<QueryItems<AuthorityGroupItem>> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); public Task<AuthorityGroup?> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default); + public Task<bool> AddUsersToGroupAsync(List<AuthorityUser> users, AuthorityGroup group, CancellationToken cancellationToken = default); public Task<List<AuthorityGroup>> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default); public Task<AuthorityGroup?> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); public Task<AuthorityGroup?> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); - public Task<bool> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); + public Task<bool> DeleteGroupsAsync(List<AuthorityGroup> groups, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Repositories/IUserRepository.cs b/DotBased.AspNet.Authority/Repositories/IUserRepository.cs index 809c02a..c284b46 100755 --- a/DotBased.AspNet.Authority/Repositories/IUserRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IUserRepository.cs @@ -5,8 +5,8 @@ namespace DotBased.AspNet.Authority.Repositories; public interface IUserRepository { - public Task<QueryItems<AuthorityUserItem>> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); - public Task<AuthorityUser?> GetAuthorityUserByIdAsync(Guid id, CancellationToken cancellationToken = default); + public Task<QueryItems<AuthorityUserItem>> GetUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); + public Task<AuthorityUser?> GetUserByIdAsync(Guid id, CancellationToken cancellationToken = default); public Task<AuthorityUser?> CreateUserAsync(AuthorityUser user, CancellationToken cancellationToken = default); public Task<AuthorityUser?> UpdateUserAsync(AuthorityUser user, CancellationToken cancellationToken = default); public Task<bool> DeleteUsersAsync(List<AuthorityUser> users, CancellationToken cancellationToken = default);