From 9a1b0cc5ad9ef68e1564565bd71e193e2f085a57 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Wed, 8 Nov 2023 10:57:00 +0000 Subject: [PATCH 1/8] Get Establishment by Trust Repository method --- .../MstrContext.cs | 9 +++++++- .../Repositories/EstablishmentRepository.cs | 18 +++++++++++++++ .../EducationEstablishmentTrust.cs | 22 +++++++++++++++++++ .../Establishment/IEstablishmentRepository.cs | 1 + 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs diff --git a/Dfe.Academies.Api.Infrastructure/MstrContext.cs b/Dfe.Academies.Api.Infrastructure/MstrContext.cs index 147e99509..d143709a2 100644 --- a/Dfe.Academies.Api.Infrastructure/MstrContext.cs +++ b/Dfe.Academies.Api.Infrastructure/MstrContext.cs @@ -19,6 +19,7 @@ public MstrContext(DbContextOptions options) : base(options) public DbSet TrustTypes { get; set; } = null!; public DbSet Establishments { get; set; } = null!; public DbSet EstablishmentTypes { get; set; } = null!; + public DbSet EducationEstablishmentTrusts { get; set; } = null!; public DbSet LocalAuthorities { get; set; } = null!; public DbSet IfdPipelines { get; set; } = null!; @@ -31,6 +32,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity(ConfigureEstablishment); modelBuilder.Entity(ConfigureEstablishmentType); + modelBuilder.Entity(ConfigureEducationEstablishmentTrust); modelBuilder.Entity(ConfigureLocalAuthority); modelBuilder.Entity(ConfigureIfdPipeline); @@ -223,7 +225,12 @@ void ConfigureTrustType(EntityTypeBuilder trustTypeConfiguration) { trustTypeConfiguration.ToTable("Ref_TrustType", DEFAULT_SCHEMA); } - + private void ConfigureEducationEstablishmentTrust(EntityTypeBuilder entityBuilder) + { + entityBuilder.HasKey(e => e.SK); + entityBuilder.ToTable("EducationEstablishmentTrust", DEFAULT_SCHEMA); + + } void ConfigureLocalAuthority(EntityTypeBuilder localAuthorityConfiguration) { localAuthorityConfiguration.HasKey(e => e.SK).HasName("SK"); diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index 6181e5084..b830da110 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -62,6 +62,24 @@ public async Task> GetByUrns(int[] urns) .ToListAsync(); } + public async Task> GetByTrust(int trustId, CancellationToken cancellationToken) + { + var establishmentIds = await context.EducationEstablishmentTrusts + .Where(eet => eet.FK_Trust == trustId) + .Select(eet => eet.FK_EducationEstablishment) + .ToListAsync(cancellationToken) + .ConfigureAwait(false); + + var establishments = await DefaultIncludes() + .Where(e => establishmentIds.Contains((int)e.SK)) + .ToListAsync(cancellationToken) + .ConfigureAwait(false); + + return establishments; + } + + + private IQueryable DefaultIncludes() { var x = dbSet diff --git a/Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs b/Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs new file mode 100644 index 000000000..648342b39 --- /dev/null +++ b/Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Dfe.Academies.Domain.Establishment +{ + public class EducationEstablishmentTrust + { + public int SK { get; set; } + + // Foreign keys + public int FK_Trust { get; set; } + public int FK_EducationEstablishment { get; set; } + + // Navigation properties + public virtual Domain.Trust.Trust Trust { get; set; } + public virtual Establishment Establishment { get; set; } + } + +} diff --git a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs index b1f1d6ea1..3decc8d47 100644 --- a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs +++ b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs @@ -10,5 +10,6 @@ Task> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken); Task> GetURNsByRegion(string[] regions, CancellationToken cancellationToken); Task> GetByUrns(int[] Urns); + Task> GetByTrust(int trustId, CancellationToken cancellationToken); } } From cefd20adee67732de2923167f54b9d923e1d6c61 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Wed, 8 Nov 2023 16:06:32 +0000 Subject: [PATCH 2/8] Added trust by companies house number --- .../Repositories/EstablishmentRepository.cs | 8 ++++-- .../Repositories/TrustRepository.cs | 8 ++++++ .../Establishment/EstablishmentQueries.cs | 11 +++++++- .../Establishment/IEstablishmentQueries.cs | 1 + .../Queries/Trust/ITrustQueries.cs | 1 + .../Queries/Trust/TrustQueries.cs | 5 ++++ .../EducationEstablishmentTrust.cs | 2 +- .../Establishment/IEstablishmentRepository.cs | 2 +- .../Trust/ITrustRepository.cs | 1 + .../V4/EstablishmentsController.cs | 28 +++++++++++++++++++ .../Controllers/V4/TrustsController.cs | 26 +++++++++++++++++ 11 files changed, 87 insertions(+), 6 deletions(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index b830da110..fde207b72 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -2,6 +2,7 @@ using Dfe.Academies.Academisation.Data.Repositories; using Dfe.Academies.Domain.Establishment; using Microsoft.EntityFrameworkCore; +using System.ComponentModel; using static System.Net.Mime.MediaTypeNames; namespace Dfe.Academies.Infrastructure.Repositories @@ -62,8 +63,9 @@ public async Task> GetByUrns(int[] urns) .ToListAsync(); } - public async Task> GetByTrust(int trustId, CancellationToken cancellationToken) - { + public async Task> GetByTrust(long? trustId, CancellationToken cancellationToken) + { + var establishmentIds = await context.EducationEstablishmentTrusts .Where(eet => eet.FK_Trust == trustId) .Select(eet => eet.FK_EducationEstablishment) @@ -71,7 +73,7 @@ public async Task> GetByTrust(int trustId, CancellationToken .ConfigureAwait(false); var establishments = await DefaultIncludes() - .Where(e => establishmentIds.Contains((int)e.SK)) + .Where(e => establishmentIds.Contains(Convert.ToInt32(e.SK))) .ToListAsync(cancellationToken) .ConfigureAwait(false); diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs index 25a245bbd..3647ea4ae 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs @@ -19,6 +19,14 @@ public TrustRepository(MstrContext context) : base(context) return trust; } + public async Task GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken) + { + var trust = await dbSet + .Include(x => x.TrustType) + .SingleOrDefaultAsync(x => x.CompaniesHouseNumber == companiesHouseNumber).ConfigureAwait(false); + + return trust; + } public async Task> GetTrustsByUkprns(string[] ukprns, CancellationToken cancellationToken) { diff --git a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs index f8bb04c3d..b5486da27 100644 --- a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs @@ -4,16 +4,19 @@ using System.Threading; using System; using Dfe.Academies.Application.Builders; +using Dfe.Academies.Domain.Trust; namespace Dfe.Academies.Application.Queries.Establishment { public class EstablishmentQueries : IEstablishmentQueries { private readonly IEstablishmentRepository _establishmentRepository; + private readonly ITrustRepository _trustRepository; - public EstablishmentQueries(IEstablishmentRepository establishmentRepository) + public EstablishmentQueries(IEstablishmentRepository establishmentRepository, ITrustRepository trustRepository) { _establishmentRepository = establishmentRepository; + _trustRepository = trustRepository; } public async Task GetByUkprn(string ukprn, CancellationToken cancellationToken) { @@ -37,6 +40,12 @@ public async Task> GetURNsByRegion(string[] regions, Cancellati return URNs; } + public async Task> GetByTrust(string trustUkprn, CancellationToken cancellationToken) + { + var trust = await _trustRepository.GetTrustByUkprn(trustUkprn, cancellationToken); + var establishments = await _establishmentRepository.GetByTrust(trust.SK, cancellationToken).ConfigureAwait(false); + return establishments.Select(MapToEstablishmentDto).ToList(); + } public async Task> GetByUrns(int[] Urns) { var establishments = await _establishmentRepository.GetByUrns(Urns).ConfigureAwait(false); diff --git a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs index b3a056c1f..b66c6b9f2 100644 --- a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs @@ -15,5 +15,6 @@ public interface IEstablishmentQueries Task<(List, int)> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken); Task> GetURNsByRegion(string[] regions, CancellationToken cancellationToken); Task> GetByUrns(int[] Urns); + Task> GetByTrust(string trustUkprn, CancellationToken cancellationToken); } } diff --git a/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs b/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs index 3b03a8875..7f0836cf4 100644 --- a/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs +++ b/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs @@ -5,6 +5,7 @@ namespace Dfe.Academies.Application.Queries.Trust public interface ITrustQueries { Task GetByUkprn(string ukprn, CancellationToken cancellationToken); + Task GetByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken); Task> GetByUkprns(string[] ukprns, CancellationToken cancellationToken); Task<(List, int)> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken); } diff --git a/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs b/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs index 251b26e49..139a3bb1a 100644 --- a/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs +++ b/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs @@ -16,6 +16,11 @@ public TrustQueries(ITrustRepository trustRepository) var trust = await _trustRepository.GetTrustByUkprn(ukprn, cancellationToken).ConfigureAwait(false); return trust == null ? null : MapToTrustDto(trust); } + public async Task GetByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken) + { + var trust = await _trustRepository.GetTrustByCompaniesHouseNumber(companiesHouseNumber, cancellationToken).ConfigureAwait(false); + return trust == null ? null : MapToTrustDto(trust); + } public async Task<(List, int)> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken) { diff --git a/Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs b/Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs index 648342b39..4df672b92 100644 --- a/Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs +++ b/Dfe.Academies.Domain/Establishment/EducationEstablishmentTrust.cs @@ -15,7 +15,7 @@ public class EducationEstablishmentTrust public int FK_EducationEstablishment { get; set; } // Navigation properties - public virtual Domain.Trust.Trust Trust { get; set; } + public virtual Trust.Trust Trust { get; set; } public virtual Establishment Establishment { get; set; } } diff --git a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs index 3decc8d47..cdee5a4e0 100644 --- a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs +++ b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs @@ -10,6 +10,6 @@ Task> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken); Task> GetURNsByRegion(string[] regions, CancellationToken cancellationToken); Task> GetByUrns(int[] Urns); - Task> GetByTrust(int trustId, CancellationToken cancellationToken); + Task> GetByTrust(long? trustId, CancellationToken cancellationToken); } } diff --git a/Dfe.Academies.Domain/Trust/ITrustRepository.cs b/Dfe.Academies.Domain/Trust/ITrustRepository.cs index 6908e5c9f..ad2dcc9a5 100644 --- a/Dfe.Academies.Domain/Trust/ITrustRepository.cs +++ b/Dfe.Academies.Domain/Trust/ITrustRepository.cs @@ -5,6 +5,7 @@ namespace Dfe.Academies.Domain.Trust public interface ITrustRepository : IGenericRepository { Task GetTrustByUkprn(string ukprn, CancellationToken cancellationToken); + Task GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken); Task> GetTrustsByUkprns(string[] ukprns, CancellationToken cancellationToken); Task> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken); diff --git a/TramsDataApi/Controllers/V4/EstablishmentsController.cs b/TramsDataApi/Controllers/V4/EstablishmentsController.cs index 704734c8a..0f3e733bf 100644 --- a/TramsDataApi/Controllers/V4/EstablishmentsController.cs +++ b/TramsDataApi/Controllers/V4/EstablishmentsController.cs @@ -168,5 +168,33 @@ public async Task>> GetByUrns([FromQuery] in var response = new List(establishments); return Ok(response); } + /// + /// Retrieves a list of establishments by their Trust UK Provider Reference Number (UKPRN) identifier. + /// + /// Contains the Trust UK Provider Reference Number (UKPRN) identifier of the establishments. + /// /// + /// List of establishments or NotFound if none are available. + [HttpGet] + [Route("establishments/trust")] + [SwaggerOperation(Summary = "Get Establishments by Trust", Description = "Returns a list of establishments specified by Trust UKPRN.")] + [SwaggerResponse(200, "Successfully found and returned the establishments.")] + [SwaggerResponse(404, "Establishments with specified Trust UKPRN not found.")] + public async Task>> GetByTrust([FromQuery] string trustUkprn, CancellationToken cancellationToken) + { + var commaSeparatedRequestTrust = string.Join(",", trustUkprn); + _logger.LogInformation($"Attemping to get establishments by Trust UKPRN : {commaSeparatedRequestTrust}"); + + var establishments = await _establishmentQueries.GetByTrust(trustUkprn, cancellationToken).ConfigureAwait(false); + + if (establishments == null) + { + _logger.LogInformation($"No establishment was found with the requested Trust UKPRN : {commaSeparatedRequestTrust}"); + return NotFound(); + } + + _logger.LogInformation($"Returning Establishments for Trust with specific UKPRN : {commaSeparatedRequestTrust}"); + var response = new List(establishments); + return Ok(response); + } } } \ No newline at end of file diff --git a/TramsDataApi/Controllers/V4/TrustsController.cs b/TramsDataApi/Controllers/V4/TrustsController.cs index 6a8b46247..ac4fe04d1 100644 --- a/TramsDataApi/Controllers/V4/TrustsController.cs +++ b/TramsDataApi/Controllers/V4/TrustsController.cs @@ -55,6 +55,32 @@ public async Task> GetTrustByUkprn(string ukprn, Cancella _logger.LogDebug(JsonSerializer.Serialize(trust)); return Ok(trust); } + /// + /// Retrieves a Trust by its Companies House Number. + /// + /// The Companies House Number identifier. + /// + /// A Trust or NotFound if not available. + [HttpGet] + [Route("trust/{ukprn}")] + [SwaggerOperation(Summary = "Retrieve Trust by Companies House Number", Description = "Returns a Trust identified by Companies House Number.")] + [SwaggerResponse(200, "Successfully found and returned the Trust.")] + [SwaggerResponse(404, "Trust with specified Companies House Number not found.")] + public async Task> GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken) + { + _logger.LogInformation($"Attempting to get trust by Companies House Number {companiesHouseNumber}"); + var trust = await _trustQueries.GetByCompaniesHouseNumber(companiesHouseNumber, cancellationToken).ConfigureAwait(false); + + if (trust == null) + { + _logger.LogInformation($"No trust found for Companies House Number {companiesHouseNumber}"); + return NotFound(); + } + + _logger.LogInformation($"Returning trust found by Companies House Number {companiesHouseNumber}"); + _logger.LogDebug(JsonSerializer.Serialize(trust)); + return Ok(trust); + } /// /// Searches for Trusts based on query parameters. From de4f5ff67fc05153125e574bfdf475ef34224d26 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Wed, 8 Nov 2023 16:09:27 +0000 Subject: [PATCH 3/8] Readd cancellation token to GetByUrns --- .../Queries/Establishment/EstablishmentQueries.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs index bce8ecf79..a2509a9ba 100644 --- a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs @@ -46,7 +46,7 @@ public async Task> GetByTrust(string trustUkprn, Cancella var establishments = await _establishmentRepository.GetByTrust(trust.SK, cancellationToken).ConfigureAwait(false); return establishments.Select(MapToEstablishmentDto).ToList(); } - public async Task> GetByUrns(int[] Urns) + public async Task> GetByUrns(int[] Urns, CancellationToken cancellationToken) { var establishments = await _establishmentRepository.GetByUrns(Urns, cancellationToken).ConfigureAwait(false); From f96d781912f8e36332fccdde6a7d96bafbba87c9 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Wed, 8 Nov 2023 16:16:10 +0000 Subject: [PATCH 4/8] Updated tests to reflect addition of trust repo --- .../Establishment/EstablishmentQueriesTests.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs b/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs index b2aa15f92..df6a9421a 100644 --- a/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs +++ b/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs @@ -27,11 +27,13 @@ public async Task GetByUkprn_WhenEstablishmentReturnedFromRepo_EstablishmentDtoI // Arrange var establishment = _fixture.Create(); var mockRepo = new Mock(); + var mockTrustRepo = new Mock(); + string ukprn = "1010101"; mockRepo.Setup(x => x.GetEstablishmentByUkprn(It.Is(v => v == ukprn), It.IsAny())).Returns(Task.FromResult(establishment)); var establishmentQueries = new EstablishmentQueries( - mockRepo.Object); + mockRepo.Object, mockTrustRepo.Object); CancellationToken cancellationToken = default(global::System.Threading.CancellationToken); @@ -51,11 +53,12 @@ public async Task GetByUrn_WhenEstablishmentReturnedFromRepo_EstablishmentDtoIsR // Arrange var establishment = _fixture.Create(); var mockRepo = new Mock(); + var mockTrustRepo = new Mock(); string urn = "1010101"; mockRepo.Setup(x => x.GetEstablishmentByUrn(It.Is(v => v == urn), It.IsAny())).Returns(Task.FromResult(establishment)); var establishmentQueries = new EstablishmentQueries( - mockRepo.Object); + mockRepo.Object, mockTrustRepo.Object); CancellationToken cancellationToken = default(global::System.Threading.CancellationToken); @@ -75,13 +78,14 @@ public async Task Search_WhenEstablishmentsReturnedFromRepo_EstablishmentDtoList // Arrange var establishments = _fixture.Create>(); var mockRepo = new Mock(); + var mockTrustRepo = new Mock(); string urn = "1010101"; string name = "Test name"; string ukPrn = "Test UkPrn"; mockRepo.Setup(x => x.Search(It.Is(v => v == name), It.Is(v => v == ukPrn), It.Is(v => v == urn), It.IsAny())).Returns(Task.FromResult(establishments)); var establishmentQueries = new EstablishmentQueries( - mockRepo.Object); + mockRepo.Object, mockTrustRepo.Object); CancellationToken cancellationToken = default(global::System.Threading.CancellationToken); @@ -107,10 +111,11 @@ public async Task GetURNsByRegion_WhenEstablishmentUrnsReturnedFromRepo_IEnumebr string[] regions = _fixture.Create(); var establishmentUrns = _fixture.Create>().AsEnumerable(); var mockRepo = new Mock(); + var mockTrustRepo = new Mock(); mockRepo.Setup(x => x.GetURNsByRegion(It.Is(v => v == regions), It.IsAny())).Returns(Task.FromResult(establishmentUrns)); var establishmentQueries = new EstablishmentQueries( - mockRepo.Object); + mockRepo.Object, mockTrustRepo.Object); CancellationToken cancellationToken = default(global::System.Threading.CancellationToken); // Act @@ -131,10 +136,11 @@ public async Task GetByUrns_WhenEstablishmentsReturnedFromRepo_ListOfEstablishme int[] Urns = _fixture.Create(); var establishments = _fixture.Create>(); var mockRepo = new Mock(); + var mockTrustRepo = new Mock(); mockRepo.Setup(x => x.GetByUrns(It.Is(v => v == Urns), It.IsAny())).Returns(Task.FromResult(establishments)); var establishmentQueries = new EstablishmentQueries( - mockRepo.Object); + mockRepo.Object, mockTrustRepo.Object); CancellationToken cancellationToken = default(global::System.Threading.CancellationToken); // Act var result = await establishmentQueries.GetByUrns( From 75d8e050764b4d1dd8728a832f898ced0e63295d Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Wed, 8 Nov 2023 16:39:45 +0000 Subject: [PATCH 5/8] Fixed conversion --- .../Repositories/EstablishmentRepository.cs | 23 +++++++++++-------- .../Controllers/V4/TrustsController.cs | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index 7ad462272..9ab5361c1 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -65,17 +65,20 @@ public async Task> GetByUrns(int[] urns, CancellationToken c public async Task> GetByTrust(long? trustId, CancellationToken cancellationToken) { - + + // Change the select to cast to long if necessary var establishmentIds = await context.EducationEstablishmentTrusts - .Where(eet => eet.FK_Trust == trustId) - .Select(eet => eet.FK_EducationEstablishment) - .ToListAsync(cancellationToken) - .ConfigureAwait(false); - - var establishments = await DefaultIncludes() - .Where(e => establishmentIds.Contains(Convert.ToInt32(e.SK))) - .ToListAsync(cancellationToken) - .ConfigureAwait(false); + .Where(eet => eet.FK_Trust == Convert.ToInt32(trustId)) + .Select(eet => (long)eet.FK_EducationEstablishment) + .ToListAsync(cancellationToken) + .ConfigureAwait(false); + + // And ensure there is no conversion if SK is long + var establishments = await DefaultIncludes() + .Where(e => establishmentIds.Contains(e.SK)) // No conversion here + .ToListAsync(cancellationToken) + .ConfigureAwait(false); + return establishments; } diff --git a/TramsDataApi/Controllers/V4/TrustsController.cs b/TramsDataApi/Controllers/V4/TrustsController.cs index ac4fe04d1..3537eaae5 100644 --- a/TramsDataApi/Controllers/V4/TrustsController.cs +++ b/TramsDataApi/Controllers/V4/TrustsController.cs @@ -62,7 +62,7 @@ public async Task> GetTrustByUkprn(string ukprn, Cancella /// /// A Trust or NotFound if not available. [HttpGet] - [Route("trust/{ukprn}")] + [Route("trust/{companiesHouseNumber}")] [SwaggerOperation(Summary = "Retrieve Trust by Companies House Number", Description = "Returns a Trust identified by Companies House Number.")] [SwaggerResponse(200, "Successfully found and returned the Trust.")] [SwaggerResponse(404, "Trust with specified Companies House Number not found.")] From 9ed89e57daa6e8727127268855412db574071322 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Wed, 8 Nov 2023 16:40:19 +0000 Subject: [PATCH 6/8] remove unneeded comment --- .../Repositories/EstablishmentRepository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index 9ab5361c1..f46eae47b 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -75,7 +75,7 @@ public async Task> GetByTrust(long? trustId, CancellationTok // And ensure there is no conversion if SK is long var establishments = await DefaultIncludes() - .Where(e => establishmentIds.Contains(e.SK)) // No conversion here + .Where(e => establishmentIds.Contains(e.SK)) .ToListAsync(cancellationToken) .ConfigureAwait(false); From 0318e7354f7223a0f37911a776fb341ba59fd060 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Thu, 9 Nov 2023 10:12:55 +0000 Subject: [PATCH 7/8] Removed psudocode comments --- .../Repositories/EstablishmentRepository.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index f46eae47b..f8aa04068 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -64,16 +64,13 @@ public async Task> GetByUrns(int[] urns, CancellationToken c } public async Task> GetByTrust(long? trustId, CancellationToken cancellationToken) - { - - // Change the select to cast to long if necessary + { var establishmentIds = await context.EducationEstablishmentTrusts .Where(eet => eet.FK_Trust == Convert.ToInt32(trustId)) .Select(eet => (long)eet.FK_EducationEstablishment) .ToListAsync(cancellationToken) .ConfigureAwait(false); - - // And ensure there is no conversion if SK is long + var establishments = await DefaultIncludes() .Where(e => establishmentIds.Contains(e.SK)) .ToListAsync(cancellationToken) From 95c17837697d6a72efa14eb34774ce0851e3c0c9 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Thu, 9 Nov 2023 10:19:03 +0000 Subject: [PATCH 8/8] As no tracking for the establishment search --- .../Repositories/EstablishmentRepository.cs | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index f8aa04068..a47a33b8c 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -27,23 +27,23 @@ public EstablishmentRepository(MstrContext context) : base(context) } public async Task> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken) { - IQueryable query = DefaultIncludes(); - - query = !string.IsNullOrEmpty(name) - ? query.Where(establishment => establishment.EstablishmentName.Contains(name)) - : query; - - query = !string.IsNullOrEmpty(ukPrn) - ? query.Where(establishment => establishment.UKPRN.Contains(ukPrn)) - : query; - - query = !string.IsNullOrEmpty(urn) - ? query.Where(establishment => establishment.URN.ToString().Contains(urn)) - : query; - - return await query.OrderBy(establishment => establishment.SK) - .ToListAsync(cancellationToken) - .ConfigureAwait(false); + IQueryable query = DefaultIncludes().AsNoTracking(); + if (!string.IsNullOrEmpty(name)) + { + query = query.Where(e => e.EstablishmentName.Contains(name)); + } + if (!string.IsNullOrEmpty(ukPrn)) + { + query = query.Where(e => e.UKPRN == ukPrn); + } + if (!string.IsNullOrEmpty(urn)) + { + if (int.TryParse(urn, out var urnAsNumber)) + { + query = query.Where(e => e.URN == urnAsNumber); + } + } + return await query.ToListAsync(cancellationToken); } public async Task> GetURNsByRegion(string[] regions, CancellationToken cancellationToken) {