Skip to content

Commit

Permalink
Get trust by trust reference number
Browse files Browse the repository at this point in the history
  • Loading branch information
paullocknimble committed Nov 9, 2023
1 parent f34215b commit 7ef71f2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@ public TrustRepository(MstrContext context) : base(context)
{
var trust = await dbSet
.Include(x => x.TrustType)
.SingleOrDefaultAsync(x => x.UKPRN == ukprn).ConfigureAwait(false);
.SingleOrDefaultAsync(x => x.UKPRN == ukprn, cancellationToken).ConfigureAwait(false);

return trust;
}
public async Task<Trust?> GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken)
{
var trust = await dbSet
.Include(x => x.TrustType)
.SingleOrDefaultAsync(x => x.CompaniesHouseNumber == companiesHouseNumber).ConfigureAwait(false);
.SingleOrDefaultAsync(x => x.CompaniesHouseNumber == companiesHouseNumber, cancellationToken).ConfigureAwait(false);

return trust;
}

public async Task<Trust?> GetTrustByTrustReferenceNumber(string trustReferenceNumber, CancellationToken cancellationToken)
{
var trust = await dbSet
.Include(x => x.TrustType)
.SingleOrDefaultAsync(x => x.GroupID == trustReferenceNumber, cancellationToken).ConfigureAwait(false);

return trust;
}
Expand Down
1 change: 1 addition & 0 deletions Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface ITrustQueries
{
Task<TrustDto?> GetByUkprn(string ukprn, CancellationToken cancellationToken);
Task<TrustDto?> GetByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken);
Task<TrustDto?> GetByTrustReferenceNumber(string trustReferenceNumber, CancellationToken cancellationToken);
Task<List<TrustDto>> GetByUkprns(string[] ukprns, CancellationToken cancellationToken);
Task<(List<TrustDto>, int)> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken);
}
Expand Down
6 changes: 6 additions & 0 deletions Dfe.Academies.Application/Queries/Trust/TrustQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public async Task<List<TrustDto>> GetByUkprns(string[] ukprns, CancellationToken

return trusts.Select(x => MapToTrustDto(x)).ToList();
}
public async Task<TrustDto?> GetByTrustReferenceNumber(string trustReferenceNumber, CancellationToken cancellationToken)
{
var trust = await _trustRepository.GetTrustByTrustReferenceNumber(trustReferenceNumber, cancellationToken).ConfigureAwait(false);
return trust == null ? null : MapToTrustDto(trust);
}

private static TrustDto MapToTrustDto(Domain.Trust.Trust trust)
{
return new TrustDto()
Expand Down
1 change: 1 addition & 0 deletions Dfe.Academies.Domain/Trust/ITrustRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface ITrustRepository : IGenericRepository<Trust>
{
Task<Trust?> GetTrustByUkprn(string ukprn, CancellationToken cancellationToken);
Task<Trust?> GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken);
Task<Trust?> GetTrustByTrustReferenceNumber(string trustReferenceNumber, CancellationToken cancellationToken);
Task<List<Trust>> GetTrustsByUkprns(string[] ukprns, CancellationToken cancellationToken);
Task<(List<Trust>, int)> Search(int page, int count, string name, string ukPrn,
string companiesHouseNumber, CancellationToken cancellationToken);
Expand Down
27 changes: 27 additions & 0 deletions TramsDataApi/Controllers/V4/TrustsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ public async Task<ActionResult<TrustDto>> GetTrustByCompaniesHouseNumber(string
return Ok(trust);
}

/// <summary>
/// Retrieves a Trust by its Companies House Number.
/// </summary>
/// <param name="companiesHouseNumber">The Companies House Number identifier.</param>
/// <param name="cancellationToken"></param>
/// <returns>A Trust or NotFound if not available.</returns>
[HttpGet]
[Route("trust/trustReferenceNumber/{trustReferenceNumber}")]
[SwaggerOperation(Summary = "Retrieve Trust by Trust Reference Number also know as the GIAS Group ID", Description = "Retrieve Trust by Trust Reference Number also know as the GIAS Group ID.")]
[SwaggerResponse(200, "Successfully found and returned the Trust.")]
[SwaggerResponse(404, "Trust with specified Trust Reference Number not found.")]
public async Task<ActionResult<TrustDto>> GetTrustByTrustReferenceNumber(string trustReferenceNumber, CancellationToken cancellationToken)
{
_logger.LogInformation($"Attempting to get trust by Trust Reference Number {trustReferenceNumber}");

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarCloud
var trust = await _trustQueries.GetByTrustReferenceNumber(trustReferenceNumber, cancellationToken).ConfigureAwait(false);

if (trust == null)
{
_logger.LogInformation($"No trust found for Trust Reference Number {trustReferenceNumber}");

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarCloud
return NotFound();
}

_logger.LogInformation($"Returning trust found by Trust Reference Number {trustReferenceNumber}");

Check notice

Code scanning / SonarCloud

Logging should not be vulnerable to injection attacks Low

Change this code to not log user-controlled data. See more on SonarCloud
_logger.LogDebug(JsonSerializer.Serialize(trust));
return Ok(trust);
}

/// <summary>
/// Searches for Trusts based on query parameters.
/// </summary>
Expand Down

0 comments on commit 7ef71f2

Please sign in to comment.