-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveUserFromGroupHandler.cs
More file actions
50 lines (38 loc) · 1.72 KB
/
Copy pathRemoveUserFromGroupHandler.cs
File metadata and controls
50 lines (38 loc) · 1.72 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
40
41
42
43
44
45
46
47
48
49
50
namespace Vinder.Federation.Application.Handlers.User;
public sealed class RemoveUserFromGroupHandler(IUserCollection userCollection, IGroupCollection groupCollection) :
IMessageHandler<RemoveUserFromGroupScheme, Result>
{
public async Task<Result> HandleAsync(RemoveUserFromGroupScheme parameters, CancellationToken cancellation = default)
{
var userFilters = UserFilters.WithSpecifications()
.WithIdentifier(parameters.UserId)
.Build();
var groupFilters = GroupFilters.WithSpecifications()
.WithIdentifier(parameters.GroupId)
.Build();
var users = await userCollection.GetUsersAsync(userFilters, cancellation);
var user = users.FirstOrDefault();
var groups = await groupCollection.GetGroupsAsync(groupFilters, cancellation);
var group = groups.FirstOrDefault();
if (user is null)
{
return Result.Failure(UserErrors.UserDoesNotExist);
}
if (group is null)
{
return Result.Failure(GroupErrors.GroupDoesNotExist);
}
#pragma warning disable S1764
// sonar suggests changing the lambda parameter name to avoid confusion,
// but we prefer keeping it as 'group' for readability. it does not interfere
// with the outer variable because C# differentiates the lambda parameter from the argument.
var groupToRemove = user.Groups.FirstOrDefault(group => group.Id == group.Id);
if (groupToRemove is null)
{
return Result.Failure(UserErrors.UserNotInGroup);
}
user.Groups.Remove(groupToRemove);
await userCollection.UpdateAsync(user, cancellation);
return Result.Success();
}
}