Skip to content

Commit

Permalink
All establishment endpoints covered
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic NEED authored and Dominic NEED committed Oct 24, 2023
1 parent afa2edd commit 4b050ad
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -24,5 +25,45 @@ public EstablishmentRepository(MstrContext context) : base(context)

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,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 @@ -18,9 +21,27 @@ public EstablishmentQueries(IEstablishmentRepository establishmentRepository)
}
public async Task<EstablishmentDto?> 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<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 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)
{
Expand Down Expand Up @@ -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,
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 @@ -11,5 +12,8 @@ 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ 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<EstablishmentDto>> GetByUrns(int[] Urns);

Check failure on line 17 in Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The type or namespace name 'EstablishmentDto' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 17 in Dfe.Academies.Domain/Establishment/IEstablishmentRepository.cs

View workflow job for this annotation

GitHub Actions / build-and-test

The type or namespace name 'EstablishmentDto' could not be found (are you missing a using directive or an assembly reference?)
}
}

0 comments on commit 4b050ad

Please sign in to comment.