diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs index 0e952bded9..0b803760fb 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIDentityClaimTypeRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -15,8 +16,24 @@ namespace Volo.Abp.Identity /// An Id value to ignore on checking. /// If there is an entity with given it's ignored. /// - Task AnyAsync(string name, Guid? ignoredId = null); + /// Cancel token + Task AnyAsync( + string name, + Guid? ignoredId = null, + CancellationToken cancellationToken = default + ); - Task> GetListAsync(string sorting, int maxResultCount, int skipCount, string filter); + Task> GetListAsync( + string sorting, + int maxResultCount, + int skipCount, + string filter, + CancellationToken cancellationToken = default + ); + + Task GetCountAsync( + string filter = null, + CancellationToken cancellationToken = default + ); } -} +} \ No newline at end of file diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs index 590003ca7d..e4d9ed55de 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs @@ -21,15 +21,20 @@ namespace Volo.Abp.Identity string filter = null, bool includeDetails = false, CancellationToken cancellationToken = default - ); - Task> GetListAsync( - IEnumerable ids, - CancellationToken cancellationToken = default + ); + Task> GetListAsync( + IEnumerable ids, + CancellationToken cancellationToken = default ); Task> GetDefaultOnesAsync( bool includeDetails = false, CancellationToken cancellationToken = default ); + + Task GetCountAsync( + string filter = null, + CancellationToken cancellationToken = default + ); } } \ No newline at end of file diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs index 6d6dcf3d50..0d0ff499c1 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityClaimTypeRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; +using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; @@ -11,20 +12,27 @@ namespace Volo.Abp.Identity.EntityFrameworkCore { public class EfCoreIdentityClaimTypeRepository : EfCoreRepository, IIdentityClaimTypeRepository { - public EfCoreIdentityClaimTypeRepository(IDbContextProvider dbContextProvider) + public EfCoreIdentityClaimTypeRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) { - } - public virtual async Task AnyAsync(string name, Guid? ignoredId = null) + public virtual async Task AnyAsync( + string name, + Guid? ignoredId = null, + CancellationToken cancellationToken = default) { return await DbSet - .WhereIf(ignoredId != null, ct => ct.Id != ignoredId) - .CountAsync(ct => ct.Name == name) > 0; + .WhereIf(ignoredId != null, ct => ct.Id != ignoredId) + .CountAsync(ct => ct.Name == name, GetCancellationToken(cancellationToken)) > 0; } - public virtual async Task> GetListAsync(string sorting, int maxResultCount, int skipCount, string filter) + public virtual async Task> GetListAsync( + string sorting, + int maxResultCount, + int skipCount, + string filter, + CancellationToken cancellationToken = default) { var identityClaimTypes = await DbSet .WhereIf( @@ -34,9 +42,21 @@ namespace Volo.Abp.Identity.EntityFrameworkCore ) .OrderBy(sorting ?? "name desc") .PageBy(skipCount, maxResultCount) - .ToListAsync(); + .ToListAsync(GetCancellationToken(cancellationToken)); return identityClaimTypes; } + + public async Task GetCountAsync( + string filter = null, + CancellationToken cancellationToken = default) + { + return await DbSet + .WhereIf( + !filter.IsNullOrWhiteSpace(), + u => + u.Name.Contains(filter) + ).LongCountAsync(GetCancellationToken(cancellationToken)); + } } -} +} \ No newline at end of file diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs index 2814abd891..c0271fa9e9 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs @@ -60,6 +60,17 @@ namespace Volo.Abp.Identity.EntityFrameworkCore return await DbSet.IncludeDetails(includeDetails).Where(r => r.IsDefault).ToListAsync(GetCancellationToken(cancellationToken)); } + public async Task GetCountAsync( + string filter = null, + CancellationToken cancellationToken = default) + { + return await DbSet + .WhereIf(!filter.IsNullOrWhiteSpace(), + x => x.Name.Contains(filter) || + x.NormalizedName.Contains(filter)) + .LongCountAsync(GetCancellationToken(cancellationToken)); + } + public override IQueryable WithDetails() { return GetQueryable().IncludeDetails(); diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs index 8880bcaf1c..a123868799 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityClaimTypeRepository.cs @@ -5,6 +5,7 @@ using MongoDB.Driver; using MongoDB.Driver.Linq; using System.Linq; using System.Linq.Dynamic.Core; +using System.Threading; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; @@ -16,23 +17,31 @@ namespace Volo.Abp.Identity.MongoDB { } - public virtual async Task AnyAsync(string name, Guid? ignoredId = null) + public virtual async Task AnyAsync( + string name, + Guid? ignoredId = null, + CancellationToken cancellationToken = default) { if (ignoredId == null) { return await GetMongoQueryable() .Where(ct => ct.Name == name) - .AnyAsync(); + .AnyAsync(GetCancellationToken(cancellationToken)); } else { return await GetMongoQueryable() .Where(ct => ct.Id != ignoredId && ct.Name == name) - .AnyAsync(); + .AnyAsync(GetCancellationToken(cancellationToken)); } } - public virtual async Task> GetListAsync(string sorting, int maxResultCount, int skipCount, string filter) + public virtual async Task> GetListAsync( + string sorting, + int maxResultCount, + int skipCount, + string filter, + CancellationToken cancellationToken = default) { return await GetMongoQueryable() .WhereIf>( @@ -43,7 +52,21 @@ namespace Volo.Abp.Identity.MongoDB .OrderBy(sorting ?? nameof(IdentityClaimType.Name)) .As>() .PageBy>(skipCount, maxResultCount) - .ToListAsync(); + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public async Task GetCountAsync( + string filter = null, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .WhereIf>( + !filter.IsNullOrWhiteSpace(), + u => + u.Name.Contains(filter) + ) + .As>() + .LongCountAsync(GetCancellationToken(cancellationToken)); } } } 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 2651fbfa6c..f4b0a365c1 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 @@ -54,9 +54,22 @@ namespace Volo.Abp.Identity.MongoDB } public virtual async Task> GetDefaultOnesAsync( - bool includeDetails = false, CancellationToken cancellationToken = default) + bool includeDetails = false, + CancellationToken cancellationToken = default) { return await GetMongoQueryable().Where(r => r.IsDefault).ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); } + + public async Task GetCountAsync( + string filter = null, + CancellationToken cancellationToken = default) + { + return await GetMongoQueryable() + .WhereIf(!filter.IsNullOrWhiteSpace(), + x => x.Name.Contains(filter) || + x.NormalizedName.Contains(filter)) + .As>() + .LongCountAsync(GetCancellationToken(cancellationToken)); + } } } \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs index 3250c0b04a..c4483aeefa 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityClaimTypeRepository_Tests.cs @@ -36,5 +36,11 @@ namespace Volo.Abp.Identity result2.ShouldBe(false); } + + [Fact] + public async Task GetCountAsync_With_Filter() + { + (await ClaimTypeRepository.GetCountAsync("Age")).ShouldBe(1); + } } } diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs index 1f65dd821f..d170a5b838 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs @@ -53,6 +53,12 @@ namespace Volo.Abp.Identity (await RoleRepository.GetCountAsync()).ShouldBeGreaterThan(0); } + [Fact] + public async Task GetCountAsync_With_Filter() + { + (await RoleRepository.GetCountAsync("admin")).ShouldBe(1); + } + [Fact] public async Task Should_Eager_Load_Role_Collections() {