Skip to content

Commit f0c1a32

Browse files
authored
Get applications by TemplateId (#40)
1 parent 835c208 commit f0c1a32

File tree

10 files changed

+257
-29
lines changed

10 files changed

+257
-29
lines changed

src/DfE.ExternalApplications.Api/Controllers/ApplicationsController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ public async Task<IActionResult> AddApplicationResponseAsync(
7171
[Authorize(Policy = "CanReadAnyApplication")]
7272
public async Task<IActionResult> GetMyApplicationsAsync(
7373
CancellationToken cancellationToken,
74-
[FromQuery] bool? includeSchema = null)
74+
[FromQuery] bool? includeSchema = null,
75+
[FromQuery] Guid? templateId = null)
7576
{
76-
var query = new GetMyApplicationsQuery(includeSchema ?? false);
77+
var query = new GetMyApplicationsQuery(includeSchema ?? false, templateId);
7778
var result = await sender.Send(query, cancellationToken);
7879

7980
if (!result.IsSuccess)
@@ -93,9 +94,10 @@ public async Task<IActionResult> GetMyApplicationsAsync(
9394
public async Task<IActionResult> GetApplicationsForUserAsync(
9495
[FromRoute] string email,
9596
CancellationToken cancellationToken,
96-
[FromQuery] bool? includeSchema = null)
97+
[FromQuery] bool? includeSchema = null,
98+
[FromQuery] Guid? templateId = null)
9799
{
98-
var query = new GetApplicationsForUserQuery(email, includeSchema ?? false);
100+
var query = new GetApplicationsForUserQuery(email, includeSchema ?? false, templateId);
99101
var result = await sender.Send(query, cancellationToken);
100102

101103
if (!result.IsSuccess)

src/DfE.ExternalApplications.Application/Applications/Queries/GetApplicationsForUserByExternalProviderIdQueryHandler.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
using DfE.ExternalApplications.Application.Users.QueryObjects;
77
using DfE.ExternalApplications.Domain.Entities;
88
using DfE.ExternalApplications.Domain.Interfaces.Repositories;
9+
using DfE.ExternalApplications.Domain.ValueObjects;
910
using MediatR;
1011
using Microsoft.EntityFrameworkCore;
1112

1213
namespace DfE.ExternalApplications.Application.Applications.Queries;
1314

14-
public sealed record GetApplicationsForUserByExternalProviderIdQuery(string ExternalProviderId, bool IncludeSchema = false)
15+
public sealed record GetApplicationsForUserByExternalProviderIdQuery(string ExternalProviderId, bool IncludeSchema = false, Guid? TemplateId = null)
1516
: IRequest<Result<IReadOnlyCollection<ApplicationDto>>>;
1617

1718
public sealed class GetApplicationsForUserByExternalProviderIdQueryHandler(
@@ -49,9 +50,18 @@ public async Task<Result<IReadOnlyCollection<ApplicationDto>>> Handle(
4950
if (!ids.Any())
5051
return Result<IReadOnlyCollection<ApplicationDto>>.Success(Array.Empty<ApplicationDto>());
5152

52-
var apps = await new GetApplicationsByIdsQueryObject(ids)
53-
.Apply(appRepo.Query().AsNoTracking())
54-
.ToListAsync(cancellationToken);
53+
var query = new GetApplicationsByIdsQueryObject(ids)
54+
.Apply(appRepo.Query().AsNoTracking());
55+
56+
// Apply template filter if specified
57+
if (request.TemplateId.HasValue)
58+
{
59+
query = new GetApplicationsByTemplateIdQueryObject(new TemplateId(request.TemplateId.Value))
60+
.Apply(query);
61+
}
62+
63+
var apps = await query.ToListAsync(cancellationToken);
64+
5565

5666
var dtoList = apps.Select(a => new ApplicationDto
5767
{

src/DfE.ExternalApplications.Application/Applications/Queries/GetApplicationsForUserQueryHandler.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
using DfE.ExternalApplications.Application.Users.QueryObjects;
77
using DfE.ExternalApplications.Domain.Entities;
88
using DfE.ExternalApplications.Domain.Interfaces.Repositories;
9+
using DfE.ExternalApplications.Domain.ValueObjects;
910
using MediatR;
1011
using Microsoft.EntityFrameworkCore;
1112

1213
namespace DfE.ExternalApplications.Application.Applications.Queries;
1314

14-
public sealed record GetApplicationsForUserQuery(string Email, bool IncludeSchema = false)
15+
public sealed record GetApplicationsForUserQuery(string Email, bool IncludeSchema = false, Guid? TemplateId = null)
1516
: IRequest<Result<IReadOnlyCollection<ApplicationDto>>>;
1617

1718
public sealed class GetApplicationsForUserQueryHandler(
@@ -56,9 +57,17 @@ public async Task<Result<IReadOnlyCollection<ApplicationDto>>> Handle(
5657
if (!ids.Any())
5758
return Result<IReadOnlyCollection<ApplicationDto>>.Success(Array.Empty<ApplicationDto>());
5859

59-
var apps = await new GetApplicationsByIdsQueryObject(ids)
60-
.Apply(appRepo.Query().AsNoTracking())
61-
.ToListAsync(cancellationToken);
60+
var query = new GetApplicationsByIdsQueryObject(ids)
61+
.Apply(appRepo.Query().AsNoTracking());
62+
63+
// Apply template filter if specified
64+
if (request.TemplateId.HasValue)
65+
{
66+
query = new GetApplicationsByTemplateIdQueryObject(new TemplateId(request.TemplateId.Value))
67+
.Apply(query);
68+
}
69+
70+
var apps = await query.ToListAsync(cancellationToken);
6271

6372
var dtoList = apps.Select(a => new ApplicationDto
6473
{

src/DfE.ExternalApplications.Application/Applications/Queries/GetMyApplicationsQueryHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace DfE.ExternalApplications.Application.Applications.Queries;
99

10-
public sealed record GetMyApplicationsQuery(bool IncludeSchema = false) : IRequest<Result<IReadOnlyCollection<ApplicationDto>>>;
10+
public sealed record GetMyApplicationsQuery(bool IncludeSchema = false, Guid? TemplateId = null) : IRequest<Result<IReadOnlyCollection<ApplicationDto>>>;
1111

1212
public sealed class GetMyApplicationsQueryHandler(
1313
IHttpContextAccessor httpContextAccessor,
@@ -33,11 +33,11 @@ public async Task<Result<IReadOnlyCollection<ApplicationDto>>> Handle(
3333
Result<IReadOnlyCollection<ApplicationDto>> result;
3434
if (principalId.Contains('@'))
3535
{
36-
result = await mediator.Send(new GetApplicationsForUserQuery(principalId, request.IncludeSchema), cancellationToken);
36+
result = await mediator.Send(new GetApplicationsForUserQuery(principalId, request.IncludeSchema, request.TemplateId), cancellationToken);
3737
}
3838
else
3939
{
40-
result = await mediator.Send(new GetApplicationsForUserByExternalProviderIdQuery(principalId, request.IncludeSchema), cancellationToken);
40+
result = await mediator.Send(new GetApplicationsForUserByExternalProviderIdQuery(principalId, request.IncludeSchema, request.TemplateId), cancellationToken);
4141
}
4242

4343
return result;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using DfE.ExternalApplications.Application.Common.QueriesObjects;
2+
using DfE.ExternalApplications.Domain.Entities;
3+
using DfE.ExternalApplications.Domain.ValueObjects;
4+
using Microsoft.EntityFrameworkCore;
5+
6+
namespace DfE.ExternalApplications.Application.Applications.QueryObjects;
7+
8+
public sealed class GetApplicationsByTemplateIdQueryObject(TemplateId templateId)
9+
: IQueryObject<Domain.Entities.Application>
10+
{
11+
public IQueryable<Domain.Entities.Application> Apply(IQueryable<Domain.Entities.Application> query) =>
12+
query.Where(a => a.TemplateVersion!.TemplateId == templateId)
13+
.Include(a => a.TemplateVersion)
14+
.ThenInclude(tv => tv!.Template);
15+
}

src/GovUK.Dfe.ExternalApplications.Api.Client/Generated/Client.g.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public string BaseUrl
287287
/// </summary>
288288
/// <returns>A list of applications accessible to the user.</returns>
289289
/// <exception cref="ExternalApplicationsException">A server side error occurred.</exception>
290-
public virtual async System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetMyApplicationsAsync(bool? includeSchema = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
290+
public virtual async System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetMyApplicationsAsync(bool? includeSchema = null, System.Guid? templateId = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
291291
{
292292
var client_ = _httpClient;
293293
var disposeClient_ = false;
@@ -307,6 +307,10 @@ public string BaseUrl
307307
{
308308
urlBuilder_.Append(System.Uri.EscapeDataString("includeSchema")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(includeSchema, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
309309
}
310+
if (templateId != null)
311+
{
312+
urlBuilder_.Append(System.Uri.EscapeDataString("templateId")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(templateId, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
313+
}
310314
urlBuilder_.Length--;
311315

312316
PrepareRequest(client_, request_, urlBuilder_);
@@ -373,7 +377,7 @@ public string BaseUrl
373377
/// </summary>
374378
/// <returns>Applications for the user.</returns>
375379
/// <exception cref="ExternalApplicationsException">A server side error occurred.</exception>
376-
public virtual async System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetApplicationsForUserAsync(string email, bool? includeSchema = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
380+
public virtual async System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetApplicationsForUserAsync(string email, bool? includeSchema = null, System.Guid? templateId = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
377381
{
378382
if (email == null)
379383
throw new System.ArgumentNullException("email");
@@ -398,6 +402,10 @@ public string BaseUrl
398402
{
399403
urlBuilder_.Append(System.Uri.EscapeDataString("includeSchema")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(includeSchema, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
400404
}
405+
if (templateId != null)
406+
{
407+
urlBuilder_.Append(System.Uri.EscapeDataString("templateId")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(templateId, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
408+
}
401409
urlBuilder_.Length--;
402410

403411
PrepareRequest(client_, request_, urlBuilder_);

src/GovUK.Dfe.ExternalApplications.Api.Client/Generated/Contracts.g.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public partial interface IApplicationsClient
5252
/// </summary>
5353
/// <returns>A list of applications accessible to the user.</returns>
5454
/// <exception cref="ExternalApplicationsException">A server side error occurred.</exception>
55-
System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetMyApplicationsAsync(bool? includeSchema = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
55+
System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetMyApplicationsAsync(bool? includeSchema = null, System.Guid? templateId = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
5656

5757
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
5858
/// <summary>
5959
/// Returns all applications for the user by {email}.
6060
/// </summary>
6161
/// <returns>Applications for the user.</returns>
6262
/// <exception cref="ExternalApplicationsException">A server side error occurred.</exception>
63-
System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetApplicationsForUserAsync(string email, bool? includeSchema = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
63+
System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<ApplicationDto>> GetApplicationsForUserAsync(string email, bool? includeSchema = null, System.Guid? templateId = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
6464

6565
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
6666
/// <summary>

src/GovUK.Dfe.ExternalApplications.Api.Client/Generated/swagger.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@
118118
"nullable": true
119119
},
120120
"x-position": 1
121+
},
122+
{
123+
"name": "templateId",
124+
"in": "query",
125+
"schema": {
126+
"type": "string",
127+
"format": "guid",
128+
"nullable": true
129+
},
130+
"x-position": 2
121131
}
122132
],
123133
"responses": {
@@ -165,6 +175,16 @@
165175
"nullable": true
166176
},
167177
"x-position": 2
178+
},
179+
{
180+
"name": "templateId",
181+
"in": "query",
182+
"schema": {
183+
"type": "string",
184+
"format": "guid",
185+
"nullable": true
186+
},
187+
"x-position": 3
168188
}
169189
],
170190
"responses": {

0 commit comments

Comments
 (0)