From 58f1959cb3365f610ae4f8c6db365aba3a62f5f5 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Tue, 21 Apr 2020 22:21:06 +0300 Subject: [PATCH] added GetList method for sorting and filtering --- .../Identity/IOrganizationUnitRepository.cs | 12 +++-- .../EfCoreIdentityUserRepository.cs | 36 ++++++------- .../EfCoreOrganizationUnitRepository.cs | 29 +++++++++-- .../MongoDB/MongoIdentityRoleRepository.cs | 11 ++-- .../MongoOrganizationUnitRepository.cs | 51 ++++++++++++------- 5 files changed, 89 insertions(+), 50 deletions(-) diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs index c7783cb949..891f4a7519 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IOrganizationUnitRepository.cs @@ -19,11 +19,17 @@ namespace Volo.Abp.Identity Guid? parentId, bool includeDetails = false, CancellationToken cancellationToken = default + ); + Task> GetListAsync( + IEnumerable ids, + bool includeDetails = false, + CancellationToken cancellationToken = default ); - Task> GetListAsync( - IEnumerable ids, - bool includeDetails = false, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + bool includeDetails = true, CancellationToken cancellationToken = default ); diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs index 46a5ef341a..e52ebf7b49 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs @@ -6,7 +6,7 @@ using System.Security.Claims; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Internal; +using Microsoft.EntityFrameworkCore.Internal; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; @@ -20,7 +20,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public virtual async Task FindByNormalizedUserNameAsync( - string normalizedUserName, + string normalizedUserName, bool includeDetails = true, CancellationToken cancellationToken = default) { @@ -33,15 +33,15 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public virtual async Task> GetRoleNamesAsync( - Guid id, + Guid id, CancellationToken cancellationToken = default) { var query = from userRole in DbContext.Set() join role in DbContext.Roles on userRole.RoleId equals role.Id where userRole.UserId == id - select role.Name; - var organizationUnitIds = DbContext.Set().Where(q => q.UserId == id).Select(q => q.OrganizationUnitId).ToArray(); - var organizationRoleIds = DbContext.Set().Where(our => organizationUnitIds.Contains(our.OrganizationUnitId)).Select(r => r.RoleId).ToArray(); + select role.Name; + var organizationUnitIds = DbContext.Set().Where(q => q.UserId == id).Select(q => q.OrganizationUnitId).ToArray(); + var organizationRoleIds = DbContext.Set().Where(our => organizationUnitIds.Contains(our.OrganizationUnitId)).Select(r => r.RoleId).ToArray(); var orgUnitRoleNameQuery = DbContext.Roles.Where(r => organizationRoleIds.Contains(r.Id)).Select(n => n.Name); var resultQuery = query.Union(orgUnitRoleNameQuery); return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken)); @@ -61,8 +61,8 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public virtual async Task FindByLoginAsync( - string loginProvider, - string providerKey, + string loginProvider, + string providerKey, bool includeDetails = true, CancellationToken cancellationToken = default) { @@ -94,7 +94,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public virtual async Task> GetListByNormalizedRoleNameAsync( - string normalizedRoleName, + string normalizedRoleName, bool includeDetails = false, CancellationToken cancellationToken = default) { @@ -114,10 +114,10 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public virtual async Task> GetListAsync( - string sorting = null, + string sorting = null, int maxResultCount = int.MaxValue, - int skipCount = 0, - string filter = null, + int skipCount = 0, + string filter = null, bool includeDetails = false, CancellationToken cancellationToken = default) { @@ -142,9 +142,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore var query = from userRole in DbContext.Set() join role in DbContext.Roles.IncludeDetails(includeDetails) on userRole.RoleId equals role.Id where userRole.UserId == id - select role; - - //TODO: Needs improvement + select role; + + //TODO: Needs improvement var userOrganizationsQuery = from userOrg in DbContext.Set() join ou in DbContext.OrganizationUnits.IncludeDetails(includeDetails) on userOrg.OrganizationUnitId equals ou.Id where userOrg.UserId == id @@ -156,13 +156,13 @@ namespace Volo.Abp.Identity.EntityFrameworkCore var orgRoles = DbContext.Roles.Where(q => orgUserRoleQuery.Contains(q.Id)); var resultQuery = query.Union(orgRoles); - return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken)); - + return await resultQuery.ToListAsync(GetCancellationToken(cancellationToken)); + //return await query.ToListAsync(GetCancellationToken(cancellationToken)); } public virtual async Task GetCountAsync( - string filter = null, + string filter = null, CancellationToken cancellationToken = default) { return await this.WhereIf( diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs index 2de8cb3f52..4796e409f9 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreOrganizationUnitRepository.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.Linq.Dynamic.Core; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -43,8 +44,21 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public async Task> GetListAsync( + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + bool includeDetails = true, + CancellationToken cancellationToken = default) + { + return await DbSet + .IncludeDetails(includeDetails) + .OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName)) + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + public async Task> GetListAsync( IEnumerable ids, - bool includeDetails = false, + bool includeDetails = false, CancellationToken cancellationToken = default) { return await DbSet @@ -54,8 +68,8 @@ namespace Volo.Abp.Identity.EntityFrameworkCore } public async Task GetOrganizationUnitAsync( - string displayName, - bool includeDetails = false, + string displayName, + bool includeDetails = false, CancellationToken cancellationToken = default) { return await DbSet @@ -64,9 +78,9 @@ namespace Volo.Abp.Identity.EntityFrameworkCore ou => ou.DisplayName == displayName, GetCancellationToken(cancellationToken) ); - } + } public async Task> GetOrganizationUnitRoles( - Guid organizationUnitId, bool includeDetails = false, + Guid organizationUnitId, bool includeDetails = false, CancellationToken cancellationToken = default) { var query = from organizationRole in DbContext.Set() @@ -81,6 +95,11 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { return GetQueryable().IncludeDetails(); } + } + public class OrganizationUnitRoleWithIdentityRole + { + public IdentityRole IdentityRole { get; set; } + public OrganizationUnitRole OrganizationUnitRole { get; set; } } } diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs index 7acb9b4e4b..b38a3433d9 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs @@ -7,20 +7,19 @@ using System.Linq.Dynamic.Core; using MongoDB.Driver; using MongoDB.Driver.Linq; using Volo.Abp.Domain.Repositories.MongoDB; -using Volo.Abp.Guids; using Volo.Abp.MongoDB; namespace Volo.Abp.Identity.MongoDB { public class MongoIdentityRoleRepository : MongoDbRepository, IIdentityRoleRepository { - public MongoIdentityRoleRepository(IMongoDbContextProvider dbContextProvider) + public MongoIdentityRoleRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) { } public async Task FindByNormalizedNameAsync( - string normalizedRoleName, + string normalizedRoleName, bool includeDetails = true, CancellationToken cancellationToken = default) { @@ -28,9 +27,9 @@ namespace Volo.Abp.Identity.MongoDB } public async Task> GetListAsync( - string sorting = null, - int maxResultCount = int.MaxValue, - int skipCount = 0, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, bool includeDetails = false, CancellationToken cancellationToken = default) { diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs index a86bbc1194..ef689df45d 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoOrganizationUnitRepository.cs @@ -3,6 +3,7 @@ using MongoDB.Driver.Linq; using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.MongoDB; @@ -19,7 +20,7 @@ namespace Volo.Abp.Identity.MongoDB } public async Task> GetChildrenAsync( - Guid? parentId, + Guid? parentId, bool includeDetails = false, CancellationToken cancellationToken = default) { @@ -29,29 +30,43 @@ namespace Volo.Abp.Identity.MongoDB } public async Task> GetAllChildrenWithParentCodeAsync( - string code, - Guid? parentId, + string code, + Guid? parentId, bool includeDetails = false, CancellationToken cancellationToken = default) { return await GetMongoQueryable() .Where(ou => ou.Code.StartsWith(code) && ou.Id != parentId.Value) .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task> GetListAsync( + IEnumerable ids, + bool includeDetails = false, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .Where(t => ids.Contains(t.Id)) + .ToListAsync(GetCancellationToken(cancellationToken)); } public async Task> GetListAsync( - IEnumerable ids, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, bool includeDetails = false, CancellationToken cancellationToken = default) { return await GetMongoQueryable() - .Where(t => ids.Contains(t.Id)) + .OrderBy(sorting ?? nameof(OrganizationUnit.DisplayName)) + .As>() + .PageBy>(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)); } public async Task GetOrganizationUnitAsync( - string displayName, - bool includeDetails = false, + string displayName, + bool includeDetails = false, CancellationToken cancellationToken = default) { return await GetMongoQueryable() @@ -59,16 +74,16 @@ namespace Volo.Abp.Identity.MongoDB ou => ou.DisplayName == displayName, GetCancellationToken(cancellationToken) ); - } - - public async Task> GetOrganizationUnitRoles( - Guid organizationUnitId, - bool includeDetails = false, - CancellationToken cancellationToken = default) - { - var organizationUnit = await GetAsync(organizationUnitId, includeDetails, cancellationToken); - var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); - return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).ToListAsync(cancellationToken); - } + } + + public async Task> GetOrganizationUnitRoles( + Guid organizationUnitId, + bool includeDetails = false, + CancellationToken cancellationToken = default) + { + var organizationUnit = await GetAsync(organizationUnitId, includeDetails, cancellationToken); + var roleIds = organizationUnit.Roles.Select(r => r.RoleId).ToArray(); + return await DbContext.Roles.AsQueryable().Where(r => roleIds.Contains(r.Id)).ToListAsync(cancellationToken); + } } }