From afa2edd02613239822adc08a4208ea8bc5c08f5b Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Fri, 20 Oct 2023 13:41:31 +0100 Subject: [PATCH 1/3] Added Establishment by URN --- .../Repositories/EstablishmentRepository.cs | 8 +++++++- .../ApplicationServiceConfigExtensions.cs | 2 ++ .../Queries/Establishment/EstablishmentQueries.cs | 11 ++++++++++- .../Queries/Establishment/IEstablishmentQueries.cs | 1 + .../Establishment/IEstablishmentRepository.cs | 1 + 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index b4c2fcbf6..d5a255815 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -14,7 +14,13 @@ public EstablishmentRepository(MstrContext context) : base(context) public async Task GetEstablishmentByUkprn(string ukprn, CancellationToken cancellationToken) { - var Establishment = await this.dbSet.SingleOrDefaultAsync(x => x.UKPRN == ukprn).ConfigureAwait(false); + var Establishment = await dbSet.SingleOrDefaultAsync(x => x.UKPRN == ukprn).ConfigureAwait(false); + + return Establishment; + } + public async Task GetEstablishmentByUrn(string urn, CancellationToken cancellationToken) + { + var Establishment = await dbSet.SingleOrDefaultAsync(x => x.URN.ToString() == urn).ConfigureAwait(false); return Establishment; } diff --git a/Dfe.Academies.Application/ApplicationServiceConfigExtensions.cs b/Dfe.Academies.Application/ApplicationServiceConfigExtensions.cs index 432b5872a..559e47547 100644 --- a/Dfe.Academies.Application/ApplicationServiceConfigExtensions.cs +++ b/Dfe.Academies.Application/ApplicationServiceConfigExtensions.cs @@ -1,4 +1,5 @@ using Dfe.Academies.Academisation.Data; +using Dfe.Academies.Application.Queries.Establishment; using Dfe.Academies.Application.Queries.Trust; using Dfe.Academies.Domain.Establishment; using Dfe.Academies.Domain.Trust; @@ -26,6 +27,7 @@ public static IServiceCollection AddApplicationDependencyGroup( { //Queries services.AddScoped(); + services.AddScoped(); //Repos services.AddScoped(); diff --git a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs index f38cbc9f9..245ac7e96 100644 --- a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs @@ -14,8 +14,17 @@ public EstablishmentQueries(IEstablishmentRepository establishmentRepository) public async Task GetByUkprn(string ukprn, CancellationToken cancellationToken) { var establishment = await _establishmentRepository.GetEstablishmentByUkprn(ukprn, cancellationToken).ConfigureAwait(false); + return establishment == null ? null : MapToEstablishmentDto(establishment); + } + public async Task GetByUrn(string urn, CancellationToken cancellationToken) + { + var establishment = await _establishmentRepository.GetEstablishmentByUkprn(ukprn, cancellationToken).ConfigureAwait(false); + return establishment == null ? null : MapToEstablishmentDto(establishment); + } - return establishment == null ? null : new EstablishmentDto() + private static EstablishmentDto MapToEstablishmentDto(Domain.Establishment.Establishment? establishment) + { + return new EstablishmentDto() { Name = establishment.EstablishmentName, Urn = establishment?.URN.ToString() ?? string.Empty, // To question diff --git a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs index f2640128f..deeda7272 100644 --- a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs @@ -10,5 +10,6 @@ namespace Dfe.Academies.Application.Queries.Establishment public interface IEstablishmentQueries { Task GetByUkprn(string ukprn, CancellationToken cancellationToken); + Task GetByUrn(string urn, CancellationToken cancellationToken); } } diff --git a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs index 32390ca73..aa29fa1d1 100644 --- a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs +++ b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs @@ -10,5 +10,6 @@ namespace Dfe.Academies.Domain.Establishment public interface IEstablishmentRepository : IGenericRepository { Task GetEstablishmentByUkprn(string ukprn, CancellationToken cancellationToken); + Task GetEstablishmentByUrn(string urn, CancellationToken cancellationToken); } } From 4b050ad14c22e0a7105377c2a08e24256b734d33 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Tue, 24 Oct 2023 10:03:11 +0100 Subject: [PATCH 2/3] All establishment endpoints covered --- .../Repositories/EstablishmentRepository.cs | 41 +++++++++++++++++++ .../Establishment/EstablishmentQueries.cs | 25 ++++++++++- .../Establishment/IEstablishmentQueries.cs | 4 ++ .../Establishment/IEstablishmentRepository.cs | 4 ++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index d5a255815..2a57397ed 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -3,6 +3,7 @@ using Dfe.Academies.Domain.Establishment; using Dfe.Academies.Domain.Trust; using Microsoft.EntityFrameworkCore; +using System.Linq; namespace Dfe.Academies.Infrastructure.Repositories { @@ -24,5 +25,45 @@ public EstablishmentRepository(MstrContext context) : base(context) return Establishment; } + public async Task> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken) + { + IQueryable query = dbSet; + + 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); + } + public async Task> GetURNsByRegion(ICollection regions, CancellationToken cancellationToken) + { + return (IEnumerable)await dbSet //Adding Explicit cast because the Domain entity has the URN as nullable + .AsNoTracking() + .Where(p => regions.Contains(p!.GORregion.ToLower())) // Assuming GORregion is correct + .Select(e => e.URN) + .ToListAsync(cancellationToken) + .ConfigureAwait(false); + } + public async Task> GetByUrns(int[] urns) + { + var urnsList = urns.ToList(); + return await dbSet + .AsNoTracking() + .Where(e => urnsList.Contains((int)e.URN)) + .ToListAsync(); + } + + + } } diff --git a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs index 245ac7e96..0fef90642 100644 --- a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs @@ -1,5 +1,8 @@ using Dfe.Academies.Contracts.Establishments; +using Dfe.Academies.Contracts.Trusts; using Dfe.Academies.Domain.Establishment; +using System.Threading; +using System; namespace Dfe.Academies.Application.Queries.Establishment { @@ -18,9 +21,27 @@ public EstablishmentQueries(IEstablishmentRepository establishmentRepository) } public async Task GetByUrn(string urn, CancellationToken cancellationToken) { - var establishment = await _establishmentRepository.GetEstablishmentByUkprn(ukprn, cancellationToken).ConfigureAwait(false); + var establishment = await _establishmentRepository.GetEstablishmentByUrn(urn, cancellationToken).ConfigureAwait(false); return establishment == null ? null : MapToEstablishmentDto(establishment); } + public async Task<(List, int)> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken) + { + var establishments = await _establishmentRepository.Search(name, ukPrn, urn, cancellationToken).ConfigureAwait(false); + + return (establishments.Select(x => MapToEstablishmentDto(x)).ToList(), establishments.Count); + } + public async Task> GetURNsByRegion(ICollection regions, CancellationToken cancellationToken) + { + var URNs = await _establishmentRepository.GetURNsByRegion(regions, cancellationToken).ConfigureAwait(false); + + return URNs; + } + public async Task> GetByUrns(int[] Urns) + { + var establishments = await _establishmentRepository.GetByUrns(Urns).ConfigureAwait(false); + + return (establishments.Select(x => MapToEstablishmentDto(x)).ToList()); + } private static EstablishmentDto MapToEstablishmentDto(Domain.Establishment.Establishment? establishment) { @@ -86,7 +107,7 @@ private static EstablishmentDto MapToEstablishmentDto(Domain.Establishment.Estab SixthFormProvision = establishment.SixthFormProvisionWhereApplicable.ToString(), Weblink = establishment.Website, }, - Address = new AddressDto() + Address = new Contracts.Establishments.AddressDto() { Street = establishment.AddressLine1, Town = establishment.Town, diff --git a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs index deeda7272..e1359d392 100644 --- a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs @@ -1,4 +1,5 @@ using Dfe.Academies.Contracts.Establishments; +using Dfe.Academies.Contracts.Trusts; using System; using System.Collections.Generic; using System.Linq; @@ -11,5 +12,8 @@ public interface IEstablishmentQueries { Task GetByUkprn(string ukprn, CancellationToken cancellationToken); Task GetByUrn(string urn, CancellationToken cancellationToken); + Task<(List, int)> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken); + Task> GetURNsByRegion(ICollection regions, CancellationToken cancellationToken); + Task> GetByUrns(int[] Urns); } } diff --git a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs index aa29fa1d1..758f6823d 100644 --- a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs +++ b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs @@ -11,5 +11,9 @@ public interface IEstablishmentRepository : IGenericRepository { Task GetEstablishmentByUkprn(string ukprn, CancellationToken cancellationToken); Task GetEstablishmentByUrn(string urn, CancellationToken cancellationToken); + Task> Search(string name, string ukPrn, + string urn, CancellationToken cancellationToken); + Task> GetURNsByRegion(ICollection regions, CancellationToken cancellationToken); + Task> GetByUrns(int[] Urns); } } From 0bd1e7d6ad45d3e7c696ec1f69375de55fe51e79 Mon Sep 17 00:00:00 2001 From: Dominic NEED Date: Tue, 24 Oct 2023 10:12:52 +0100 Subject: [PATCH 3/3] Updated usings --- .../Repositories/EstablishmentRepository.cs | 2 -- .../Establishment/IEstablishmentRepository.cs | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index 2a57397ed..f1b343ac2 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -1,9 +1,7 @@ using Dfe.Academies.Academisation.Data; using Dfe.Academies.Academisation.Data.Repositories; using Dfe.Academies.Domain.Establishment; -using Dfe.Academies.Domain.Trust; using Microsoft.EntityFrameworkCore; -using System.Linq; namespace Dfe.Academies.Infrastructure.Repositories { diff --git a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs index 758f6823d..86bea94c5 100644 --- a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs +++ b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs @@ -1,9 +1,4 @@ using Dfe.Academies.Academisation.Domain.SeedWork; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Dfe.Academies.Domain.Establishment { @@ -14,6 +9,6 @@ public interface IEstablishmentRepository : IGenericRepository Task> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken); Task> GetURNsByRegion(ICollection regions, CancellationToken cancellationToken); - Task> GetByUrns(int[] Urns); + Task> GetByUrns(int[] Urns); } }