Skip to content

Commit

Permalink
Merge pull request #403 from DFE-Digital/feature/establishment-by-urn-v1
Browse files Browse the repository at this point in the history
Feature/establishment by urn v1
  • Loading branch information
dneed-nimble authored Oct 24, 2023
2 parents a87df4b + 0bd1e7d commit 3eb119f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,9 +13,55 @@ public EstablishmentRepository(MstrContext context) : base(context)

public async Task<Establishment?> 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<Establishment?> GetEstablishmentByUrn(string urn, CancellationToken cancellationToken)
{
var Establishment = await dbSet.SingleOrDefaultAsync(x => x.URN.ToString() == urn).ConfigureAwait(false);

return Establishment;
}
public async Task<List<Establishment>> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken)
{
IQueryable<Establishment> 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<IEnumerable<int>> GetURNsByRegion(ICollection<string> regions, CancellationToken cancellationToken)
{
return (IEnumerable<int>)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<List<Establishment>> GetByUrns(int[] urns)
{
var urnsList = urns.ToList();
return await dbSet
.AsNoTracking()
.Where(e => urnsList.Contains((int)e.URN))
.ToListAsync();
}



}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -26,6 +27,7 @@ public static IServiceCollection AddApplicationDependencyGroup(
{
//Queries
services.AddScoped<ITrustQueries, TrustQueries>();
services.AddScoped<IEstablishmentQueries, EstablishmentQueries>();

//Repos
services.AddScoped<ITrustRepository, TrustRepository>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -14,8 +17,35 @@ public EstablishmentQueries(IEstablishmentRepository establishmentRepository)
public async Task<EstablishmentDto?> GetByUkprn(string ukprn, CancellationToken cancellationToken)
{
var establishment = await _establishmentRepository.GetEstablishmentByUkprn(ukprn, cancellationToken).ConfigureAwait(false);
return establishment == null ? null : MapToEstablishmentDto(establishment);
}
public async Task<EstablishmentDto?> GetByUrn(string urn, CancellationToken cancellationToken)
{
var establishment = await _establishmentRepository.GetEstablishmentByUrn(urn, cancellationToken).ConfigureAwait(false);
return establishment == null ? null : MapToEstablishmentDto(establishment);
}
public async Task<(List<EstablishmentDto>, 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<IEnumerable<int>> GetURNsByRegion(ICollection<string> regions, CancellationToken cancellationToken)
{
var URNs = await _establishmentRepository.GetURNsByRegion(regions, cancellationToken).ConfigureAwait(false);

return establishment == null ? null : new EstablishmentDto()
return URNs;
}
public async Task<List<EstablishmentDto>> 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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.Academies.Contracts.Establishments;
using Dfe.Academies.Contracts.Trusts;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,5 +11,9 @@ namespace Dfe.Academies.Application.Queries.Establishment
public interface IEstablishmentQueries
{
Task<EstablishmentDto?> GetByUkprn(string ukprn, CancellationToken cancellationToken);
Task<EstablishmentDto?> GetByUrn(string urn, CancellationToken cancellationToken);
Task<(List<EstablishmentDto>, int)> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken);
Task<IEnumerable<int>> GetURNsByRegion(ICollection<string> regions, CancellationToken cancellationToken);
Task<List<EstablishmentDto>> GetByUrns(int[] Urns);
}
}
10 changes: 5 additions & 5 deletions Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Establishment>
{
Task<Establishment?> GetEstablishmentByUkprn(string ukprn, CancellationToken cancellationToken);
Task<Establishment?> GetEstablishmentByUrn(string urn, CancellationToken cancellationToken);
Task<List<Establishment>> Search(string name, string ukPrn,
string urn, CancellationToken cancellationToken);
Task<IEnumerable<int>> GetURNsByRegion(ICollection<string> regions, CancellationToken cancellationToken);
Task<List<Establishment>> GetByUrns(int[] Urns);
}
}

0 comments on commit 3eb119f

Please sign in to comment.