-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #434 from DFE-Digital/feature/improve-v4-test-cove…
…rage added api tests for the v4 trust endpoint
- Loading branch information
Showing
42 changed files
with
3,325 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
768 changes: 768 additions & 0 deletions
768
Dfe.Academies.Api.Infrastructure/Migrations/20231218095513_Initial.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
360 changes: 360 additions & 0 deletions
360
Dfe.Academies.Api.Infrastructure/Migrations/20231218095513_Initial.cs
Large diffs are not rendered by default.
Oops, something went wrong.
766 changes: 766 additions & 0 deletions
766
Dfe.Academies.Api.Infrastructure/Migrations/MstrContextModelSnapshot.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 87 additions & 46 deletions
133
Dfe.Academies.Api.Infrastructure/Repositories/EstablishmentRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,140 @@ | ||
using Dfe.Academies.Academisation.Data; | ||
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 | ||
{ | ||
public class EstablishmentRepository : GenericRepository<Establishment>, IEstablishmentRepository | ||
public class EstablishmentRepository : IEstablishmentRepository | ||
{ | ||
public EstablishmentRepository(MstrContext context) : base(context) | ||
private MstrContext _context; | ||
|
||
public EstablishmentRepository(MstrContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<Establishment?> GetEstablishmentByUkprn(string ukprn, CancellationToken cancellationToken) | ||
{ | ||
var Establishment = await DefaultIncludes().SingleOrDefaultAsync(x => x.UKPRN == ukprn).ConfigureAwait(false); | ||
var queryResult = await BaseQuery().SingleOrDefaultAsync(r => r.Establishment.UKPRN == ukprn); | ||
|
||
if (queryResult == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return Establishment; | ||
var result = ToEstablishment(queryResult); | ||
|
||
return result; | ||
} | ||
|
||
public async Task<Establishment?> GetEstablishmentByUrn(string urn, CancellationToken cancellationToken) | ||
{ | ||
var establishment = await DefaultIncludes().SingleOrDefaultAsync(x => x.URN.ToString() == urn).ConfigureAwait(false); | ||
var queryResult = await BaseQuery().SingleOrDefaultAsync(r => r.Establishment.URN.ToString() == urn); | ||
|
||
return establishment; | ||
if (queryResult == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var result = ToEstablishment(queryResult); | ||
|
||
return result; | ||
} | ||
|
||
public async Task<List<Establishment>> Search(string name, string ukPrn, string urn, CancellationToken cancellationToken) | ||
{ | ||
IQueryable<Establishment> query = DefaultIncludes().AsNoTracking(); | ||
IQueryable<EstablishmentQueryResult> query = BaseQuery(); | ||
|
||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
query = query.Where(e => e.EstablishmentName.Contains(name)); | ||
query = query.Where(r => r.Establishment.EstablishmentName.Contains(name)); | ||
} | ||
if (!string.IsNullOrEmpty(ukPrn)) | ||
{ | ||
query = query.Where(e => e.UKPRN == ukPrn); | ||
query = query.Where(r => r.Establishment.UKPRN == ukPrn); | ||
} | ||
if (!string.IsNullOrEmpty(urn)) | ||
{ | ||
if (int.TryParse(urn, out var urnAsNumber)) | ||
{ | ||
query = query.Where(e => e.URN == urnAsNumber); | ||
} | ||
} | ||
return await query.Take(100).ToListAsync(cancellationToken); | ||
query = query.Where(r => r.Establishment.URN == urnAsNumber); | ||
} | ||
} | ||
var queryResult = await query.Take(100).ToListAsync(cancellationToken); | ||
|
||
var result = queryResult.Select(ToEstablishment).ToList(); | ||
|
||
return result; | ||
} | ||
|
||
public async Task<IEnumerable<int>> GetURNsByRegion(string[] regions, CancellationToken cancellationToken) | ||
{ | ||
return await DefaultIncludes() //Adding Explicit cast because the Domain entity has the URN as nullable | ||
{ | ||
return await _context.Establishments | ||
.AsNoTracking() | ||
.Where(p => regions.Contains(p!.GORregion.ToLower()) && p.URN.HasValue) | ||
.Where(p => regions.Contains(p.GORregion) && p.URN.HasValue) | ||
.Select(e => e.URN.Value) | ||
.ToListAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
.ToListAsync(cancellationToken); | ||
} | ||
|
||
public async Task<List<Establishment>> GetByUrns(int[] urns, CancellationToken cancellationToken) | ||
{ | ||
var urnsList = urns.ToList(); | ||
return await DefaultIncludes() | ||
.AsNoTracking() | ||
.Where(e => urnsList.Contains((int)e.URN)) | ||
var queryResult = await BaseQuery() | ||
.Where(r => urnsList.Contains((int)r.Establishment.URN)) | ||
.ToListAsync(cancellationToken); | ||
|
||
var result = queryResult.Select(ToEstablishment).ToList(); | ||
|
||
return result; | ||
} | ||
|
||
public async Task<List<Establishment>> GetByTrust(long? trustId, CancellationToken cancellationToken) | ||
{ | ||
var establishmentIds = await context.EducationEstablishmentTrusts | ||
.Where(eet => eet.FK_Trust == Convert.ToInt32(trustId)) | ||
.Select(eet => (long)eet.FK_EducationEstablishment) | ||
.ToListAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
var establishments = await DefaultIncludes().AsNoTracking() | ||
.Where(e => establishmentIds.Contains(e.SK)) | ||
.ToListAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
|
||
|
||
return establishments; | ||
} | ||
{ | ||
var establishmentIds = | ||
await _context.EducationEstablishmentTrusts | ||
.AsNoTracking() | ||
.Where(eet => eet.TrustId == Convert.ToInt32(trustId)) | ||
.Select(eet => (long)eet.EducationEstablishmentId) | ||
.ToListAsync(cancellationToken); | ||
|
||
var establishments = | ||
await BaseQuery() | ||
.Where(r => establishmentIds.Contains(r.Establishment.SK.Value)) | ||
.ToListAsync(cancellationToken); | ||
|
||
var result = establishments.Select(ToEstablishment).ToList(); | ||
|
||
return result; | ||
} | ||
|
||
private IQueryable<Establishment> DefaultIncludes() | ||
private IQueryable<EstablishmentQueryResult> BaseQuery() | ||
{ | ||
var x = dbSet | ||
.Include(x => x.EstablishmentType) | ||
.Include(x => x.LocalAuthority) | ||
.Include(x => x.IfdPipeline) | ||
.AsQueryable(); | ||
var result = | ||
from establishment in _context.Establishments | ||
from ifdPipeline in _context.IfdPipelines.Where(i => i.GeneralDetailsUrn == establishment.PK_GIAS_URN).DefaultIfEmpty() | ||
from establishmentType in _context.EstablishmentTypes.Where(e => e.SK == establishment.EstablishmentTypeId).DefaultIfEmpty() | ||
from localAuthority in _context.LocalAuthorities.Where(l => l.SK == establishment.LocalAuthorityId).DefaultIfEmpty() | ||
select new EstablishmentQueryResult { Establishment = establishment, IfdPipeline = ifdPipeline, LocalAuthority = localAuthority, EstablishmentType = establishmentType }; | ||
|
||
return x; | ||
return result; | ||
} | ||
|
||
private static Establishment ToEstablishment(EstablishmentQueryResult queryResult) | ||
{ | ||
var result = queryResult.Establishment; | ||
result.IfdPipeline = queryResult.IfdPipeline; | ||
result.LocalAuthority = queryResult.LocalAuthority; | ||
result.EstablishmentType = queryResult.EstablishmentType; | ||
|
||
return result; | ||
} | ||
} | ||
|
||
internal record EstablishmentQueryResult | ||
{ | ||
public Establishment Establishment { get; set; } | ||
public IfdPipeline IfdPipeline { get; set; } | ||
public LocalAuthority LocalAuthority { get; set; } | ||
public EstablishmentType EstablishmentType { get; set; } | ||
} | ||
} |
Oops, something went wrong.