Skip to content

Commit

Permalink
Check values in query is not null before reading it
Browse files Browse the repository at this point in the history
  • Loading branch information
nwarms committed Jan 25, 2024
1 parent fec04fc commit 51056f9
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ public TrustRepository(MstrContext context)

return trust;
}
public async Task<Trust?> GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken)

public async Task<Trust?> GetTrustByCompaniesHouseNumber(string companiesHouseNumber,
CancellationToken cancellationToken)
{
var trust = await DefaultIncludes().AsNoTracking()
.SingleOrDefaultAsync(x => x.CompaniesHouseNumber == companiesHouseNumber, cancellationToken).ConfigureAwait(false);
.SingleOrDefaultAsync(x => x.CompaniesHouseNumber == companiesHouseNumber, cancellationToken)
.ConfigureAwait(false);

return trust;
}

public async Task<Trust?> GetTrustByTrustReferenceNumber(string trustReferenceNumber, CancellationToken cancellationToken)
public async Task<Trust?> GetTrustByTrustReferenceNumber(string trustReferenceNumber,
CancellationToken cancellationToken)
{
var trust = await DefaultIncludes().AsNoTracking()
.SingleOrDefaultAsync(x => x.GroupID == trustReferenceNumber, cancellationToken).ConfigureAwait(false);
Expand All @@ -38,36 +42,44 @@ public TrustRepository(MstrContext context)

public async Task<List<Trust>> GetTrustsByUkprns(string[] ukprns, CancellationToken cancellationToken)
{
var trusts = await DefaultIncludes().AsNoTracking().Where(x => ukprns.Contains(x.UKPRN)).ToListAsync(cancellationToken).ConfigureAwait(false);
var trusts = await DefaultIncludes().AsNoTracking().Where(x => ukprns.Contains(x.UKPRN))
.ToListAsync(cancellationToken).ConfigureAwait(false);

return trusts;
}

public async Task<(List<Trust>, int)> Search(int page, int count, string? name, string? ukPrn, string? companiesHouseNumber, TrustStatus status, CancellationToken cancellationToken)
public async Task<(List<Trust>, int)> Search(int page, int count, string? name, string? ukPrn,
string? companiesHouseNumber, TrustStatus status, CancellationToken cancellationToken)
{
if (name == null && ukPrn == null && companiesHouseNumber == null)
{
IOrderedQueryable<Trust> allTrusts = DefaultIncludes().AsNoTracking().OrderBy(trust => trust.GroupUID);
IOrderedQueryable<Trust> allTrusts = DefaultIncludes().AsNoTracking().OrderBy(trust => trust.GroupUID);

return (await allTrusts.Skip((page - 1) * count)
.Take(count).ToListAsync(cancellationToken).ConfigureAwait(false), allTrusts.Count());
.Take(count).ToListAsync(cancellationToken).ConfigureAwait(false), allTrusts.Count());
}

IQueryable<Trust> filteredGroups = DefaultIncludes().AsNoTracking()
.Where(trust => (trust.Name.Contains(name) ||
trust.UKPRN.Contains(ukPrn) ||
trust.CompaniesHouseNumber.Contains(companiesHouseNumber))
&& (
trust.TrustType.Name == "Single-academy trust" ||
trust.TrustType.Name == "Multi-academy trust"
));

.Where(trust => trust.CompaniesHouseNumber != null
&& (
(name != null && trust.Name != null && trust.Name.Contains(name)) ||
(ukPrn != null && trust.UKPRN != null && trust.UKPRN.Contains(ukPrn)) ||
(companiesHouseNumber != null
&& trust.CompaniesHouseNumber != null
&& trust.CompaniesHouseNumber.Contains(companiesHouseNumber))
)
&& trust.TrustType != null &&
(trust.TrustType.Name == "Single-academy trust" ||
trust.TrustType.Name == "Multi-academy trust"));

if (status == TrustStatus.Open)
{
filteredGroups = filteredGroups.Where(trust => trust.TrustStatus == "Open");
}

return (await filteredGroups.OrderBy(trust => trust.GroupUID).Skip((page - 1) * count).Take(count).ToListAsync(cancellationToken).ConfigureAwait(false), filteredGroups.Count());
return (
await filteredGroups.OrderBy(trust => trust.GroupUID).Skip((page - 1) * count).Take(count)
.ToListAsync(cancellationToken).ConfigureAwait(false), filteredGroups.Count());
}

private IQueryable<Trust> DefaultIncludes()
Expand Down

0 comments on commit 51056f9

Please sign in to comment.