Add GetCountAsync method

pull/17377/head
liangshiwei 1 year ago
parent 0a6cef2301
commit cd587699b9

@ -119,6 +119,11 @@ public interface IIdentityUserRepository : IBasicRepository<IdentityUser, Guid>
CancellationToken cancellationToken = default
);
Task<List<RoleWithUserCount>> GetCountAsync(
Guid[] roleIds,
CancellationToken cancellationToken = default
);
Task<IdentityUser> FindByTenantIdAndUserNameAsync(
[NotNull] string userName,
Guid? tenantId,

@ -0,0 +1,10 @@
using System;
namespace Volo.Abp.Identity;
public class RoleWithUserCount
{
public Guid RoleId { get; set; }
public long UserCount { get; set; }
}

@ -263,6 +263,25 @@ public class EfCoreIdentityUserRepository : EfCoreRepository<IIdentityDbContext,
.LongCountAsync(GetCancellationToken(cancellationToken));
}
public async Task<List<RoleWithUserCount>> GetCountAsync(Guid[] roleIds, CancellationToken cancellationToken = default)
{
var users = await (await GetDbSetAsync())
.AsNoTracking()
.Where(user => user.Roles.Any(role => roleIds.Contains(role.RoleId))).IncludeDetails().ToListAsync(GetCancellationToken(cancellationToken));
var result = new List<RoleWithUserCount>();
foreach (var roleId in roleIds)
{
result.Add(new RoleWithUserCount
{
RoleId = roleId,
UserCount = users.Count(t => t.Roles.Any(role => role.RoleId == roleId))
});
}
return result;
}
public virtual async Task<List<OrganizationUnit>> GetOrganizationUnitsAsync(
Guid id,
bool includeDetails = false,

@ -269,6 +269,25 @@ public class MongoIdentityUserRepository : MongoDbRepository<IAbpIdentityMongoDb
.LongCountAsync(GetCancellationToken(cancellationToken));
}
public async Task<List<RoleWithUserCount>> GetCountAsync(Guid[] roleIds, CancellationToken cancellationToken = default)
{
var users = await (await GetMongoQueryableAsync(cancellationToken))
.Where(user => user.Roles.Any(role => roleIds.Contains(role.RoleId)))
.ToListAsync(GetCancellationToken(cancellationToken));
var result = new List<RoleWithUserCount>();
foreach (var roleId in roleIds)
{
result.Add(new RoleWithUserCount
{
RoleId = roleId,
UserCount = users.Count(t => t.Roles.Any(role => role.RoleId == roleId))
});
}
return result;
}
public virtual async Task<List<IdentityUser>> GetUsersInOrganizationUnitAsync(
Guid organizationUnitId,
CancellationToken cancellationToken = default)

@ -80,7 +80,7 @@ public class AbpIdentityTestDataBuilder : ITransientDependency
_moderatorRole.AddClaim(_guidGenerator, new Claim("test-claim", "test-value"));
await _roleRepository.InsertAsync(_moderatorRole);
_supporterRole = new IdentityRole(_guidGenerator.Create(), "supporter");
_supporterRole = new IdentityRole(_testData.RoleSupporterId, "supporter");
await _roleRepository.InsertAsync(_supporterRole);
_managerRole = new IdentityRole(_guidGenerator.Create(), "manager");

@ -6,6 +6,8 @@ namespace Volo.Abp.Identity;
public class IdentityTestData : ISingletonDependency
{
public Guid RoleModeratorId { get; } = Guid.NewGuid();
public Guid RoleSupporterId { get; } = Guid.NewGuid();
public Guid UserJohnId { get; } = Guid.NewGuid();
public Guid UserDavidId { get; } = Guid.NewGuid();

@ -135,6 +135,16 @@ public abstract class IdentityUserRepository_Tests<TStartupModule> : AbpIdentity
(await UserRepository.GetCountAsync("n")).ShouldBeGreaterThan(1);
(await UserRepository.GetCountAsync("undefined-username")).ShouldBe(0);
}
[Fact]
public async Task GetCountAsync_With_RoleIds()
{
var roleWithUserCounts = await UserRepository.GetCountAsync(roleIds: new []{ TestData.RoleModeratorId, TestData.RoleSupporterId });
roleWithUserCounts.Count.ShouldBe(2);
roleWithUserCounts.ShouldContain(e => e.RoleId == TestData.RoleModeratorId && e.UserCount == 1);
roleWithUserCounts.ShouldContain(e => e.RoleId == TestData.RoleSupporterId && e.UserCount == 2);
}
[Fact]
public async Task GetUsersInOrganizationUnitAsync()

Loading…
Cancel
Save