Skip to content

Commit 2cde2e3

Browse files
committed
Split out Performance and Destinations repositories
1 parent fd30284 commit 2cde2e3

14 files changed

Lines changed: 356 additions & 224 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using SAPSec.Core.Model.Generated;
2+
3+
namespace SAPSec.Core.Features.Ks4HeadlineMeasures;
4+
5+
public interface IKs4DestinationsRepository
6+
{
7+
Task<Ks4DestinationsData?> GetByUrnAsync(string urn);
8+
}
9+
10+
public record Ks4DestinationsData(
11+
EstablishmentDestinations? EstablishmentDestinations,
12+
LADestinations? LocalAuthorityDestinations,
13+
EnglandDestinations? EnglandDestinations);

SAPSec.Core/Features/Ks4HeadlineMeasures/IKs4PerformanceRepository.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ public interface IKs4PerformanceRepository
1010
public record Ks4HeadlineMeasuresData(
1111
EstablishmentPerformance? EstablishmentPerformance,
1212
LAPerformance? LocalAuthorityPerformance,
13-
EnglandPerformance? EnglandPerformance,
14-
EstablishmentDestinations? EstablishmentDestinations,
15-
LADestinations? LocalAuthorityDestinations,
16-
EnglandDestinations? EnglandDestinations);
13+
EnglandPerformance? EnglandPerformance);
14+

SAPSec.Core/Features/Ks4HeadlineMeasures/UseCases/GetKs4HeadlineMeasures.cs

Lines changed: 112 additions & 110 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using SAPSec.Core.Features.Ks4HeadlineMeasures;
2+
using SAPSec.Core.Interfaces.Repositories;
3+
using SAPSec.Core.Model.Generated;
4+
5+
namespace SAPSec.Infrastructure.Json;
6+
7+
public class JsonKs4DestinationsRepository(
8+
IEstablishmentRepository establishmentRepository,
9+
IJsonFile<EstablishmentDestinations> establishmentDestinationsRepository,
10+
IJsonFile<LADestinations> localAuthorityDestinationsRepository,
11+
IJsonFile<EnglandDestinations> englandDestinationsRepository) : IKs4DestinationsRepository
12+
{
13+
public async Task<Ks4DestinationsData?> GetByUrnAsync(string urn)
14+
{
15+
var establishment = await establishmentRepository.GetEstablishmentAsync(urn);
16+
if (string.IsNullOrWhiteSpace(establishment?.URN))
17+
{
18+
return null;
19+
}
20+
21+
var establishmentDestinations = (await establishmentDestinationsRepository.ReadAllAsync())
22+
.FirstOrDefault(p => p.Id == urn);
23+
24+
var localAuthorityDestinations = (await localAuthorityDestinationsRepository.ReadAllAsync())
25+
.FirstOrDefault(p => p.Id == establishment.LAId);
26+
27+
var englandDestinations = (await englandDestinationsRepository.ReadAllAsync())
28+
.FirstOrDefault();
29+
30+
return new(
31+
establishmentDestinations,
32+
localAuthorityDestinations,
33+
englandDestinations);
34+
}
35+
}

SAPSec.Infrastructure/Json/JsonKs4PerformanceRepository.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ public class JsonKs4PerformanceRepository(
88
IEstablishmentRepository establishmentRepository,
99
IJsonFile<EstablishmentPerformance> establishmentPerformanceRepository,
1010
IJsonFile<LAPerformance> localAuthorityPerformanceRepository,
11-
IJsonFile<EnglandPerformance> englandPerformanceRepository,
12-
IJsonFile<EstablishmentDestinations> establishmentDestinationsRepository,
13-
IJsonFile<LADestinations> localAuthorityDestinationsRepository,
14-
IJsonFile<EnglandDestinations> englandDestinationsRepository) : IKs4PerformanceRepository
11+
IJsonFile<EnglandPerformance> englandPerformanceRepository) : IKs4PerformanceRepository
1512
{
1613
public async Task<Ks4HeadlineMeasuresData?> GetByUrnAsync(string urn)
1714
{
@@ -30,21 +27,9 @@ public class JsonKs4PerformanceRepository(
3027
var englandPerformance = (await englandPerformanceRepository.ReadAllAsync())
3128
.FirstOrDefault();
3229

33-
var establishmentDestinations = (await establishmentDestinationsRepository.ReadAllAsync())
34-
.FirstOrDefault(p => p.Id == urn);
35-
36-
var localAuthorityDestinations = (await localAuthorityDestinationsRepository.ReadAllAsync())
37-
.FirstOrDefault(p => p.Id == establishment.LAId);
38-
39-
var englandDestinations = (await englandDestinationsRepository.ReadAllAsync())
40-
.FirstOrDefault();
41-
4230
return new(
4331
establishmentPerformance,
4432
localAuthorityPerformance,
45-
englandPerformance,
46-
establishmentDestinations,
47-
localAuthorityDestinations,
48-
englandDestinations);
33+
englandPerformance);
4934
}
5035
}

SAPSec.Infrastructure/Postgres/PostgresDependenciesExtensions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,24 @@ public static IServiceCollection AddPostgresqlDependencies(this IServiceCollecti
1717
services.AddSingleton<IEstablishmentRepository, PostgresEstablishmentRepository>();
1818
services.AddSingleton<ISimilarSchoolsSecondaryRepository, PostgresSimilarSchoolsSecondaryRepository>();
1919
services.AddSingleton<IKs4PerformanceRepository, PostgresKs4PerformanceRepository>();
20-
services.AddSingleton<IAttendanceRepository, PostgresAttendanceRepository>();
20+
services.AddSingleton<IKs4DestinationsRepository, PostgresKs4DestinationsRepository>();
21+
services.AddSingleton<IAttendanceRepository, PostgresAttendanceRepository>();
22+
23+
//services.AddSingleton<IJsonFile<SimilarSchoolsSecondaryGroupsEntry>, JsonFile<SimilarSchoolsSecondaryGroupsEntry>>();
24+
//services.AddSingleton<IJsonFile<SimilarSchoolsSecondaryValuesEntry>, JsonFile<SimilarSchoolsSecondaryValuesEntry>>();
25+
//services.AddSingleton<IJsonFile<Establishment>, JsonFile<Establishment>>();
26+
//services.AddSingleton<IJsonFile<EstablishmentEmail>, JsonFile<EstablishmentEmail>>();
27+
//services.AddSingleton<IJsonFile<EstablishmentPerformance>, JsonFile<EstablishmentPerformance>>();
28+
//services.AddSingleton<IJsonFile<LAPerformance>, JsonFile<LAPerformance>>();
29+
//services.AddSingleton<IJsonFile<EnglandPerformance>, JsonFile<EnglandPerformance>>();
30+
//services.AddSingleton<IJsonFile<EstablishmentDestinations>, JsonFile<EstablishmentDestinations>>();
31+
//services.AddSingleton<IJsonFile<LADestinations>, JsonFile<LADestinations>>();
32+
//services.AddSingleton<IJsonFile<EnglandDestinations>, JsonFile<EnglandDestinations>>();
33+
//services.AddSingleton<IJsonFile<SimilarSchoolsSecondaryStandardDeviations>, JsonFile<SimilarSchoolsSecondaryStandardDeviations>>();
34+
//services.AddSingleton<IEstablishmentRepository, JsonEstablishmentRepository>();
35+
//services.AddSingleton<ISimilarSchoolsSecondaryRepository, JsonSimilarSchoolsSecondaryRepository>();
36+
//services.AddSingleton<IKs4PerformanceRepository, JsonKs4PerformanceRepository>();
37+
//services.AddSingleton<IKs4DestinationsRepository, JsonKs4DestinationsRepository>();
2138

2239
return services;
2340
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Dapper;
2+
using Microsoft.Extensions.Logging;
3+
using SAPSec.Core.Features.Ks4HeadlineMeasures;
4+
using SAPSec.Core.Model.Generated;
5+
6+
namespace SAPSec.Infrastructure.Postgres;
7+
8+
public class PostgresKs4DestinationsRepository(
9+
ILogger<PostgresKs4DestinationsRepository> logger,
10+
NpgsqlDataSourceFactory factory) : IKs4DestinationsRepository
11+
{
12+
private readonly ILogger<PostgresKs4DestinationsRepository> _logger = logger;
13+
private readonly NpgsqlDataSourceFactory _factory = factory;
14+
15+
public async Task<Ks4DestinationsData?> GetByUrnAsync(string urn)
16+
{
17+
using var conn = await _factory.Create().OpenConnectionAsync();
18+
19+
const string sql = """
20+
SELECT *
21+
FROM public.v_establishment_destinations
22+
WHERE "Id" = @urn;
23+
24+
SELECT d.*
25+
FROM public.v_la_destinations d
26+
INNER JOIN public.v_establishment e on e."LAId" = d."Id"
27+
WHERE e."URN" = @urn;
28+
29+
SELECT *
30+
FROM public.v_england_destinations
31+
WHERE "Id" = 'National';
32+
""";
33+
34+
using var results = await conn.QueryMultipleAsync(sql, new { urn });
35+
36+
var establishmentDestinations = await results.ReadSingleOrDefaultAsync<EstablishmentDestinations>();
37+
var localAuthorityDestinations = await results.ReadSingleOrDefaultAsync<LADestinations>();
38+
var englandDestinations = await results.ReadSingleOrDefaultAsync<EnglandDestinations>();
39+
40+
if (establishmentDestinations is null
41+
&& localAuthorityDestinations is null
42+
&& englandDestinations is null)
43+
{
44+
return null;
45+
}
46+
47+
return new(
48+
establishmentDestinations,
49+
localAuthorityDestinations,
50+
englandDestinations);
51+
}
52+
}

SAPSec.Infrastructure/Postgres/PostgresKs4PerformanceRepository.cs

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,83 +17,36 @@ public class PostgresKs4PerformanceRepository(
1717
using var conn = await _factory.Create().OpenConnectionAsync();
1818

1919
const string sql = """
20-
SELECT "LAId"
21-
FROM public.v_establishment
22-
WHERE "URN" = @urn
23-
LIMIT 1;
24-
2520
SELECT *
2621
FROM public.v_establishment_performance
2722
WHERE "Id" = @urn;
2823
24+
SELECT p.*
25+
FROM public.v_la_performance p
26+
INNER JOIN public.v_establishment e on e."LAId" = p."Id"
27+
WHERE e."URN" = @urn;
28+
2929
SELECT *
3030
FROM public.v_england_performance
31-
WHERE "Id" = 'National'
32-
LIMIT 1;
33-
34-
SELECT *
35-
FROM public.v_establishment_destinations
36-
WHERE "Id" = @urn;
37-
38-
SELECT *
39-
FROM public.v_england_destinations
40-
WHERE "Id" = 'National'
41-
LIMIT 1;
31+
WHERE "Id" = 'National';
4232
""";
4333

4434
using var results = await conn.QueryMultipleAsync(sql, new { urn });
4535

46-
var establishmentInfo = await results.ReadSingleOrDefaultAsync<EstablishmentInfo>();
4736
var establishmentPerformance = await results.ReadSingleOrDefaultAsync<EstablishmentPerformance>();
37+
var localAuthorityPerformance = await results.ReadSingleOrDefaultAsync<LAPerformance>();
4838
var englandPerformance = await results.ReadSingleOrDefaultAsync<EnglandPerformance>();
49-
var establishmentDestinations = await results.ReadSingleOrDefaultAsync<EstablishmentDestinations>();
50-
var englandDestinations = await results.ReadSingleOrDefaultAsync<EnglandDestinations>();
51-
52-
LAPerformance? localAuthorityPerformance = null;
53-
LADestinations? localAuthorityDestinations = null;
54-
if (!string.IsNullOrWhiteSpace(establishmentInfo?.LAId))
55-
{
56-
const string laSql = """
57-
SELECT *
58-
FROM public.v_la_performance
59-
WHERE "Id" = @laId
60-
LIMIT 1;
61-
62-
SELECT *
63-
FROM public.v_la_destinations
64-
WHERE "Id" = @laId
65-
LIMIT 1;
66-
""";
67-
using var laResults = await conn.QueryMultipleAsync(laSql, new { laId = establishmentInfo!.LAId });
68-
localAuthorityPerformance = await laResults.ReadSingleOrDefaultAsync<LAPerformance>();
69-
localAuthorityDestinations = await laResults.ReadSingleOrDefaultAsync<LADestinations>();
70-
}
71-
else
72-
{
73-
_logger.LogWarning("No LAId found for URN {Urn} when loading KS4 performance.", urn);
74-
}
7539

7640
if (establishmentPerformance is null
7741
&& localAuthorityPerformance is null
78-
&& englandPerformance is null
79-
&& establishmentDestinations is null
80-
&& localAuthorityDestinations is null
81-
&& englandDestinations is null)
42+
&& englandPerformance is null)
8243
{
8344
return null;
8445
}
8546

8647
return new(
8748
establishmentPerformance,
8849
localAuthorityPerformance,
89-
englandPerformance,
90-
establishmentDestinations,
91-
localAuthorityDestinations,
92-
englandDestinations);
93-
}
94-
95-
private sealed class EstablishmentInfo
96-
{
97-
public string? LAId { get; set; }
50+
englandPerformance);
9851
}
9952
}

0 commit comments

Comments
 (0)