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); } }