Browse Source

Implemented SearchAsync for the user lookup

pull/3950/head
Halil İbrahim Kalkan 6 years ago
parent
commit
23f3042536
  1. 3
      modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserLookupAppService.cs
  2. 11
      modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/UserLookupSearchInputDto.cs
  3. 17
      modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserLookupAppService.cs
  4. 19
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserRepositoryExternalUserLookupServiceProvider.cs
  5. 19
      modules/identity/src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/HttpClientIdentityUserLookupService.cs
  6. 8
      modules/identity/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUserLookupController.cs
  7. 35
      modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserLookupAppService_Tests.cs
  8. 9
      modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/IExternalUserLookupServiceProvider.cs
  9. 7
      modules/users/src/Volo.Abp.Users.Domain/Volo/Abp/Users/IUserLookupService.cs
  10. 8
      modules/users/src/Volo.Abp.Users.Domain/Volo/Abp/Users/IUserRepository.cs
  11. 13
      modules/users/src/Volo.Abp.Users.Domain/Volo/Abp/Users/UserLookupService.cs
  12. 20
      modules/users/src/Volo.Abp.Users.EntityFrameworkCore/Volo/Abp/Users/EntityFrameworkCore/EfCoreAbpUserRepositoryBase.cs
  13. 17
      modules/users/src/Volo.Abp.Users.MongoDB/Volo/Abp/Users/MongoDB/MongoUserRepositoryBase.cs

3
modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/IIdentityUserLookupAppService.cs

@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
@ -10,5 +11,7 @@ namespace Volo.Abp.Identity
Task<UserData> FindByIdAsync(Guid id);
Task<UserData> FindByUserNameAsync(string userName);
Task<ListResultDto<UserData>> SearchAsync(UserLookupSearchInputDto input);
}
}

11
modules/identity/src/Volo.Abp.Identity.Application.Contracts/Volo/Abp/Identity/UserLookupSearchInputDto.cs

@ -0,0 +1,11 @@
using Volo.Abp.Application.Dtos;
namespace Volo.Abp.Identity
{
public class UserLookupSearchInputDto : LimitedResultRequestDto, ISortedResultRequest
{
public string Sorting { get; set; }
public string Filter { get; set; }
}
}

17
modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserLookupAppService.cs

@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Users;
namespace Volo.Abp.Identity
@ -37,5 +39,20 @@ namespace Volo.Abp.Identity
return new UserData(userData);
}
public async Task<ListResultDto<UserData>> SearchAsync(UserLookupSearchInputDto input)
{
var users = await UserLookupServiceProvider.SearchAsync(
input.Sorting,
input.Filter,
input.MaxResultCount
);
return new ListResultDto<UserData>(
users
.Select(u => new UserData(u))
.ToList()
);
}
}
}

19
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserRepositoryExternalUserLookupServiceProvider.cs

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
@ -45,5 +47,22 @@ namespace Volo.Abp.Identity
)
)?.ToAbpUserData();
}
public virtual async Task<List<IUserData>> SearchAsync(
string sorting,
string filter,
int maxResultCount,
CancellationToken cancellationToken = default)
{
var users = await UserRepository.GetListAsync(
sorting: sorting,
maxResultCount: maxResultCount,
filter: filter,
includeDetails: false,
cancellationToken: cancellationToken
);
return users.Select(u => u.ToAbpUserData()).ToList();
}
}
}

19
modules/identity/src/Volo.Abp.Identity.HttpApi.Client/Volo/Abp/Identity/HttpClientIdentityUserLookupService.cs

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
@ -25,5 +27,22 @@ namespace Volo.Abp.Identity
{
return await UserLookupAppService.FindByUserNameAsync(userName);
}
public async Task<List<IUserData>> SearchAsync(
string sorting,
string filter,
int maxResultCount,
CancellationToken cancellationToken = default)
{
var result = await UserLookupAppService.SearchAsync(
new UserLookupSearchInputDto
{
Filter = filter,
MaxResultCount = maxResultCount
}
);
return result.Items.Cast<IUserData>().ToList();
}
}
}

8
modules/identity/src/Volo.Abp.Identity.HttpApi/Volo/Abp/Identity/IdentityUserLookupController.cs

@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Users;
@ -32,5 +33,12 @@ namespace Volo.Abp.Identity
{
return LookupAppService.FindByUserNameAsync(userName);
}
[HttpGet]
[Route("search")]
public Task<ListResultDto<UserData>> SearchAsync(UserLookupSearchInputDto input)
{
return LookupAppService.SearchAsync(input);
}
}
}

35
modules/identity/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserLookupAppService_Tests.cs

@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Shouldly;
@ -52,5 +50,38 @@ namespace Volo.Abp.Identity
var user = await _identityUserLookupAppService.FindByUserNameAsync(Guid.NewGuid().ToString());
user.ShouldBeNull();
}
[Fact]
public async Task Search_Without_Filter_And_Sorting()
{
var result = await _identityUserLookupAppService.SearchAsync(new UserLookupSearchInputDto());
result.Items.Count.ShouldBeGreaterThanOrEqualTo(3);
result.Items.ShouldContain(u => u.UserName == "john.nash");
}
[Fact]
public async Task Search_With_Filter()
{
var result = await _identityUserLookupAppService.SearchAsync(
new UserLookupSearchInputDto
{
Filter = "a"
}
);
result.Items.Count.ShouldBeGreaterThanOrEqualTo(2);
result.Items.ShouldContain(u => u.UserName == "john.nash");
result.Items.ShouldContain(u => u.UserName == "david");
result = await _identityUserLookupAppService.SearchAsync(
new UserLookupSearchInputDto
{
Filter = "neo"
}
);
result.Items.Count.ShouldBeGreaterThanOrEqualTo(1);
result.Items.ShouldContain(u => u.UserName == "neo");
}
}
}

9
modules/users/src/Volo.Abp.Users.Abstractions/Volo/Abp/Users/IExternalUserLookupServiceProvider.cs

@ -1,13 +1,20 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Volo.Abp.Users
{
public interface IExternalUserLookupServiceProvider //TODO: Consider to inherit from IUserLookupService
public interface IExternalUserLookupServiceProvider
{
Task<IUserData> FindByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task<IUserData> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default);
Task<List<IUserData>> SearchAsync(
string sorting,
string filter,
int maxResultCount,
CancellationToken cancellationToken = default);
}
}

7
modules/users/src/Volo.Abp.Users.Domain/Volo/Abp/Users/IUserLookupService.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
@ -11,6 +12,10 @@ namespace Volo.Abp.Users
Task<TUser> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default);
//TODO: More...
Task<List<IUserData>> SearchAsync(
string sorting,
string filter,
int maxResultCount,
CancellationToken cancellationToken = default);
}
}

8
modules/users/src/Volo.Abp.Users.Domain/Volo/Abp/Users/IUserRepository.cs

@ -13,5 +13,13 @@ namespace Volo.Abp.Users
Task<TUser> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default);
Task<List<TUser>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default);
Task<List<TUser>> SearchAsync(
string sorting = null,
int maxResultCount = int.MaxValue,
int skipCount = 0,
string filter = null,
CancellationToken cancellationToken = default
);
}
}

13
modules/users/src/Volo.Abp.Users.Domain/Volo/Abp/Users/UserLookupService.cs

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
@ -138,6 +140,17 @@ namespace Volo.Abp.Users
return await _userRepository.FindAsync(externalUser.Id, cancellationToken: cancellationToken);
}
public async Task<List<IUserData>> SearchAsync(string sorting, string filter, int maxResultCount, CancellationToken cancellationToken = default)
{
if (ExternalUserLookupServiceProvider != null)
{
return await ExternalUserLookupServiceProvider.SearchAsync(sorting, filter, maxResultCount, cancellationToken);
}
var localUsers = await _userRepository.SearchAsync(sorting, maxResultCount, 0, filter, cancellationToken);
return localUsers.Cast<IUserData>().ToList();
}
protected abstract TUser CreateUser(IUserData externalUser);
private async Task WithNewUowAsync(Func<Task> func)

20
modules/users/src/Volo.Abp.Users.EntityFrameworkCore/Volo/Abp/Users/EntityFrameworkCore/EfCoreAbpUserRepositoryBase.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -28,5 +29,24 @@ namespace Volo.Abp.Users.EntityFrameworkCore
{
return await DbSet.Where(u => ids.Contains(u.Id)).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<List<TUser>> SearchAsync(
string sorting = null,
int maxResultCount = Int32.MaxValue,
int skipCount = 0,
string filter = null,
CancellationToken cancellationToken = default)
{
return await DbSet
.WhereIf(
!filter.IsNullOrWhiteSpace(),
u =>
u.UserName.Contains(filter) ||
u.Email.Contains(filter)
)
.OrderBy(sorting ?? nameof(IUser.UserName))
.PageBy(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
}
}

17
modules/users/src/Volo.Abp.Users.MongoDB/Volo/Abp/Users/MongoDB/MongoUserRepositoryBase.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
@ -29,5 +30,21 @@ namespace Volo.Abp.Users.MongoDB
{
return await GetMongoQueryable().Where(u => ids.Contains(u.Id)).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<List<TUser>> SearchAsync(string sorting = null, int maxResultCount = Int32.MaxValue, int skipCount = 0, string filter = null,
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
.WhereIf<TUser, IMongoQueryable<TUser>>(
!filter.IsNullOrWhiteSpace(),
u =>
u.UserName.Contains(filter) ||
u.Email.Contains(filter)
)
.OrderBy(sorting ?? nameof(IUserData.UserName))
.As<IMongoQueryable<TUser>>()
.PageBy<TUser, IMongoQueryable<TUser>>(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken));
}
}
}
Loading…
Cancel
Save