-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUpdateHandler.cs
More file actions
39 lines (30 loc) · 1.44 KB
/
Copy pathClientUpdateHandler.cs
File metadata and controls
39 lines (30 loc) · 1.44 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
30
31
32
33
34
35
36
37
38
39
namespace HttpsRichardy.Federation.Application.Handlers.Client;
public sealed class ClientUpdateHandler(IClientCollection collection) :
IDispatchHandler<ClientUpdateScheme, Result<ClientScheme>>
{
public async Task<Result<ClientScheme>> HandleAsync(
ClientUpdateScheme parameters, CancellationToken cancellation = default)
{
var filters = ClientFilters.WithSpecifications()
.WithIdentifier(parameters.Id)
.Build();
var clients = await collection.GetClientsAsync(filters, cancellation: cancellation);
var client = clients.FirstOrDefault();
if (client is null)
{
return Result<ClientScheme>.Failure(ClientErrors.ClientDoesNotExist);
}
var nameFilter = ClientFilters.WithSpecifications()
.WithName(parameters.Name)
.Build();
var clientsWithSameName = await collection.GetClientsAsync(nameFilter, cancellation: cancellation);
var existingClient = clientsWithSameName.FirstOrDefault(existing => existing.Id != parameters.Id);
if (existingClient is not null)
{
return Result<ClientScheme>.Failure(ClientErrors.ClientAlreadyExists);
}
client = ClientMapper.AsClient(parameters, client);
var updatedClient = await collection.UpdateAsync(client, cancellation: cancellation);
return Result<ClientScheme>.Success(ClientMapper.AsResponse(updatedClient));
}
}