Add GetByIdentity method
This commit is contained in:
parent
e7e2610da1
commit
f5ee811d17
@ -16,6 +16,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nocr.Users.Host", "src\Nocr
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nocr.Users.Api.Contracts", "src\Nocr.Users.Api.Contracts\Nocr.Users.Api.Contracts.csproj", "{4DDFB05F-DFC7-4BD6-B593-AD52CF1B002E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nocr.Users.AppServices.Contracts", "src\Nocr.Users.AppServices.Contracts\Nocr.Users.AppServices.Contracts.csproj", "{51DCF618-41C0-432A-8FD0-3EA3B70B375D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -38,5 +40,9 @@ Global
|
||||
{4DDFB05F-DFC7-4BD6-B593-AD52CF1B002E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4DDFB05F-DFC7-4BD6-B593-AD52CF1B002E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4DDFB05F-DFC7-4BD6-B593-AD52CF1B002E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{51DCF618-41C0-432A-8FD0-3EA3B70B375D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{51DCF618-41C0-432A-8FD0-3EA3B70B375D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{51DCF618-41C0-432A-8FD0-3EA3B70B375D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{51DCF618-41C0-432A-8FD0-3EA3B70B375D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
@ -10,4 +10,8 @@
|
||||
<PackageReference Include="RestEase" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Nocr.Users.AppServices.Contracts\Nocr.Users.AppServices.Contracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
namespace Nocr.Users.Api.Contracts.Users.Dto;
|
||||
namespace Nocr.Users.Api.Contracts.Users.Dto.Requests;
|
||||
|
||||
public sealed class CreateUserRequest
|
||||
{
|
||||
@ -5,4 +5,6 @@ public sealed class UserData
|
||||
public long Id { get; set; }
|
||||
|
||||
public string Username { get; set; }
|
||||
|
||||
public UserIdentityData[] Identities { get; set; }
|
||||
}
|
||||
12
src/Nocr.Users.Api.Contracts/Users/Dto/UserIdentityData.cs
Normal file
12
src/Nocr.Users.Api.Contracts/Users/Dto/UserIdentityData.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
|
||||
namespace Nocr.Users.Api.Contracts.Users.Dto;
|
||||
|
||||
public sealed class UserIdentityData
|
||||
{
|
||||
public long Id { get; set; }
|
||||
|
||||
public UserIdentityType Type { get; set; }
|
||||
|
||||
public string Identity { get; set; }
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
using Nocr.Users.Api.Contracts.Users.Dto;
|
||||
using Nocr.Users.Api.Contracts.Users.Dto.Requests;
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
using RestEase;
|
||||
|
||||
namespace Nocr.Users.Api.Contracts.Users;
|
||||
@ -11,4 +13,7 @@ public interface IUsersController
|
||||
|
||||
[Get(WebRoutes.Users.ById)]
|
||||
Task<UserData?> GetById([Path] long id, CancellationToken cancellationToken = default);
|
||||
|
||||
[Get(WebRoutes.Users.ByIdentity)]
|
||||
Task<UserData?> GetByIdentity([Query] UserIdentityType identityType, [Query] string identity, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@ -9,5 +9,7 @@ public static class WebRoutes
|
||||
public const string Path = BasePath + "/" + "users";
|
||||
|
||||
public const string ById = "{id}";
|
||||
|
||||
public const string ByIdentity = "identity";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@ -1,4 +1,4 @@
|
||||
namespace Nocr.Users.AppServices.Users;
|
||||
namespace Nocr.Users.AppServices.Contracts.Users;
|
||||
|
||||
public enum UserIdentityType
|
||||
{
|
||||
@ -10,6 +10,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Nocr.Users.Api.Contracts\Nocr.Users.Api.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Nocr.Users.AppServices.Contracts\Nocr.Users.AppServices.Contracts.csproj" />
|
||||
<ProjectReference Include="..\Nocr.Users.Core\Nocr.Users.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
|
||||
namespace Nocr.Users.AppServices.Users.Repositories;
|
||||
|
||||
public interface IUsersRepository
|
||||
@ -5,4 +7,6 @@ public interface IUsersRepository
|
||||
Task<long> Create(User user, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<User?> GetUserById(long id, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<User?> GetByIdentity(UserIdentityType identityType, string identity, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@ -1,3 +1,5 @@
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
|
||||
namespace Nocr.Users.AppServices.Users.Repositories;
|
||||
|
||||
public sealed class InMemoryUsersRepository : IUsersRepository
|
||||
@ -26,4 +28,13 @@ public sealed class InMemoryUsersRepository : IUsersRepository
|
||||
{
|
||||
return Task.FromResult(_users.FirstOrDefault(x => x.Id == id));
|
||||
}
|
||||
|
||||
public Task<User?> GetByIdentity(UserIdentityType identityType, string identity,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = _users.SingleOrDefault(x => x.Identities.Any(i =>
|
||||
i.Type == identityType && i.Identity.Equals(identity, StringComparison.OrdinalIgnoreCase)));
|
||||
|
||||
return Task.FromResult(user);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Nocr.Users.Api.Contracts.Users.Dto;
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
|
||||
namespace Nocr.Users.AppServices.Users.Services;
|
||||
|
||||
@ -8,4 +9,6 @@ public interface IUsersService
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task<UserData?> GetById(long id, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<UserData?> GetByIdentity(UserIdentityType identityType, string identity, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Nocr.Users.Api.Contracts.Users.Dto;
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
using Nocr.Users.AppServices.Users.Repositories;
|
||||
|
||||
namespace Nocr.Users.AppServices.Users.Services;
|
||||
@ -35,6 +36,20 @@ public sealed class UsersService : IUsersService
|
||||
public async Task<UserData?> GetById(long id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await _repository.GetUserById(id, cancellationToken);
|
||||
|
||||
return MapToUserData(user);
|
||||
}
|
||||
|
||||
public async Task<UserData?> GetByIdentity(UserIdentityType identityType, string identity,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var user = await _repository.GetByIdentity(identityType, identity, cancellationToken);
|
||||
|
||||
return MapToUserData(user);
|
||||
}
|
||||
|
||||
private static UserData? MapToUserData(User? user)
|
||||
{
|
||||
if (user == null)
|
||||
return null;
|
||||
|
||||
@ -42,6 +57,17 @@ public sealed class UsersService : IUsersService
|
||||
{
|
||||
Id = user.Id,
|
||||
Username = user.Username,
|
||||
Identities = user.Identities.Select(MapToUserIdentityData).ToArray()
|
||||
};
|
||||
}
|
||||
|
||||
private static UserIdentityData MapToUserIdentityData(UserIdentity userIdentity)
|
||||
{
|
||||
return new UserIdentityData
|
||||
{
|
||||
Id = userIdentity.Id,
|
||||
Identity = userIdentity.Identity,
|
||||
Type = userIdentity.Type
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,5 @@
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
|
||||
namespace Nocr.Users.AppServices.Users;
|
||||
|
||||
public sealed class UserIdentity
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Nocr.Users.Api.Contracts;
|
||||
using Nocr.Users.Api.Contracts.Users.Dto;
|
||||
using Nocr.Users.Api.Contracts.Users.Dto.Requests;
|
||||
using Nocr.Users.AppServices.Contracts.Users;
|
||||
using Nocr.Users.AppServices.Users.Services;
|
||||
|
||||
namespace Nocr.Users.Host.Controllers;
|
||||
@ -28,4 +30,11 @@ public class UsersController : ControllerBase
|
||||
{
|
||||
return _usersService.GetById(id, cancellationToken);
|
||||
}
|
||||
|
||||
[HttpGet(WebRoutes.Users.ByIdentity)]
|
||||
public Task<UserData?> GetByIdentity([FromQuery] UserIdentityType identityType, [FromQuery] string identity,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return _usersService.GetByIdentity(identityType, identity, cancellationToken);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user