diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs index b4c2fcbf6..f1b343ac2 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs @@ -1,7 +1,6 @@ using Dfe.Academies.Academisation.Data; using Dfe.Academies.Academisation.Data.Repositories; using Dfe.Academies.Domain.Establishment; -using Dfe.Academies.Domain.Trust; using Microsoft.EntityFrameworkCore; namespace Dfe.Academies.Infrastructure.Repositories @@ -14,9 +13,55 @@ 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; + } + 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/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..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 { @@ -14,8 +17,35 @@ 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.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 establishment == null ? null : new EstablishmentDto() + 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) + { + return new EstablishmentDto() { Name = establishment.EstablishmentName, Urn = establishment?.URN.ToString() ?? string.Empty, // To question @@ -77,7 +107,7 @@ public EstablishmentQueries(IEstablishmentRepository establishmentRepository) 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 f2640128f..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; @@ -10,5 +11,9 @@ namespace Dfe.Academies.Application.Queries.Establishment 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 32390ca73..86bea94c5 100644 --- a/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs +++ b/Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs @@ -1,14 +1,14 @@ 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 { 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); } }