-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCreationHandler.cs
More file actions
29 lines (22 loc) · 1.19 KB
/
Copy pathClientCreationHandler.cs
File metadata and controls
29 lines (22 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace HttpsRichardy.Federation.Application.Handlers.Client;
public sealed class ClientCreationHandler(IClientCredentialsGenerator credentialsGenerator, IRealmProvider realmProvider, IClientCollection clientCollection) :
IDispatchHandler<ClientCreationScheme, Result<ClientScheme>>
{
public async Task<Result<ClientScheme>> HandleAsync(ClientCreationScheme parameters, CancellationToken cancellation = default)
{
var filters = ClientFilters.WithSpecifications()
.WithName(parameters.Name)
.Build();
var clients = await clientCollection.GetClientsAsync(filters, cancellation: cancellation);
var existingClient = clients.FirstOrDefault();
if (existingClient is not null)
{
return Result<ClientScheme>.Failure(ClientErrors.ClientAlreadyExists);
}
var realm = realmProvider.GetCurrentRealm();
var credentials = await credentialsGenerator.GenerateAsync(parameters.Name, cancellation);
var client = parameters.AsClient(credentials, realm);
await clientCollection.InsertAsync(client, cancellation: cancellation);
return Result<ClientScheme>.Success(client.AsResponse());
}
}