-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetApplicationRolesUseCase.cs
More file actions
52 lines (48 loc) · 1.82 KB
/
Copy pathGetApplicationRolesUseCase.cs
File metadata and controls
52 lines (48 loc) · 1.82 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
51
52
using System.Data;
using Dfe.SignIn.Base.Framework;
using Dfe.SignIn.Core.Contracts.Applications;
using Dfe.SignIn.Gateways.EntityFramework;
using Microsoft.EntityFrameworkCore;
namespace Dfe.SignIn.Core.UseCases.Applications;
/// <summary>
/// Use case responsible for obtaining information about an application.
/// </summary>
public sealed class GetApplicationRolesUseCase(DbOrganisationsContext uowOrganisations) : Interactor<GetApplicationRolesRequest, GetApplicationRolesResponse>
{
/// <inheritdoc/>
public override async Task<GetApplicationRolesResponse> InvokeAsync(
InteractionContext<GetApplicationRolesRequest> context,
CancellationToken cancellationToken = default)
{
context.ThrowIfHasValidationErrors();
var roles = await uowOrganisations.Roles
.Include(r => r.Parent)
.Where(r => r.ApplicationId == context.Request.ApplicationId)
.OrderBy(r => r.Name)
.Select(x => new {
x.Id,
x.Name,
x.Code,
x.NumericId,
x.Status,
x.Parent
})
.ToListAsync(cancellationToken);
return new GetApplicationRolesResponse {
Roles = roles?.Select(r => new ApplicationRole {
Id = r.Id,
Name = r.Name,
Code = r.Code,
NumericId = r.NumericId,
Status = (ApplicationRoleStatus)r.Status,
Parent = r.Parent != null ? new ApplicationRole {
Id = r.Parent.Id,
Name = r.Parent.Name,
Code = r.Parent.Code,
NumericId = r.Parent.NumericId,
Status = (ApplicationRoleStatus)r.Parent.Status
} : null
}) ?? []
};
}
}