-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeCreationHandler.cs
More file actions
30 lines (23 loc) · 1.22 KB
/
ScopeCreationHandler.cs
File metadata and controls
30 lines (23 loc) · 1.22 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
namespace HttpsRichardy.Federation.Application.Handlers.Scope;
public sealed class ScopeCreationHandler(IScopeCollection scopeCollection, IRealmCollection realmCollection, IRealmProvider realmProvider) :
IDispatchHandler<ScopeCreationScheme, Result<ScopeDetailsScheme>>
{
public async Task<Result<ScopeDetailsScheme>> HandleAsync(ScopeCreationScheme parameters, CancellationToken cancellation = default)
{
var realm = realmProvider.GetCurrentRealm();
var filters = ScopeFilters.WithSpecifications()
.WithName(parameters.Name)
.Build();
var scopes = await scopeCollection.GetScopesAsync(filters, cancellation: cancellation);
var existingScope = scopes.FirstOrDefault();
if (existingScope is not null)
{
return Result<ScopeDetailsScheme>.Failure(ScopeErrors.ScopeAlreadyExists);
}
var scope = await scopeCollection.InsertAsync(ScopeMapper.AsScope(parameters, realm), cancellation: cancellation);
var response = ScopeMapper.AsResponse(scope);
realm.Scopes.Add(scope);
await realmCollection.UpdateAsync(realm, cancellation: cancellation);
return Result<ScopeDetailsScheme>.Success(response);
}
}