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 contextFactory, return await context.Groups.Where(g => g.Id == groupId).Include(g => g.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken); } + public async Task AddUsersToGroupAsync(List 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> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default) { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); @@ -72,22 +95,21 @@ public class GroupRepository(IDbContextFactory 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 DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + public async Task DeleteGroupsAsync(List 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 contextFactory, ILogger logger) : RepositoryBase, IUserRepository { - public async Task> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) + public async Task> 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 contextFactory, return QueryItems.Create(selected, totalCount, limit, offset); } - public async Task GetAuthorityUserByIdAsync(Guid id, CancellationToken cancellationToken = default) + public async Task 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>> 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> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); public Task GetGroupByIdAsync(string id, CancellationToken cancellationToken = default); + public Task AddUsersToGroupAsync(List users, AuthorityGroup group, CancellationToken cancellationToken = default); public Task> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default); public Task CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); public Task UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); - public Task DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); + public Task DeleteGroupsAsync(List 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> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); - public Task GetAuthorityUserByIdAsync(Guid id, CancellationToken cancellationToken = default); + public Task> GetUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); + public Task GetUserByIdAsync(Guid id, CancellationToken cancellationToken = default); public Task CreateUserAsync(AuthorityUser user, CancellationToken cancellationToken = default); public Task UpdateUserAsync(AuthorityUser user, CancellationToken cancellationToken = default); public Task DeleteUsersAsync(List users, CancellationToken cancellationToken = default);