Skip to content

Commit

Permalink
Merge pull request #413 from DFE-Digital/feature/trust-v4-endpoint
Browse files Browse the repository at this point in the history
Feature/trust v4 endpoint
  • Loading branch information
paullocknimble authored Nov 9, 2023
2 parents ef35dd1 + 909523e commit f34215b
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.2" />
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.24" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.24">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public async Task<List<Trust>> GetTrustsByUkprns(string[] ukprns, CancellationTo
return trusts;
}

public async Task<List<Trust>> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken)
public async Task<(List<Trust>, int)> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken)
{
if (name == null && ukPrn == null && companiesHouseNumber == null)
{
List<Trust> allTrusts = await dbSet.OrderBy(trust => trust.GroupUID).Skip((page - 1) * count)
.Take(count).ToListAsync(cancellationToken).ConfigureAwait(false);
return allTrusts;
return (allTrusts, allTrusts.Count);
}

IOrderedQueryable<Trust> filteredGroups = dbSet
Expand All @@ -54,7 +54,7 @@ public async Task<List<Trust>> Search(int page, int count, string name, string u
))
.OrderBy(trust => trust.GroupUID);

return await filteredGroups.Skip((page - 1) * count).Take(count).ToListAsync(cancellationToken).ConfigureAwait(false);
return (await filteredGroups.Skip((page - 1) * count).Take(count).ToListAsync(cancellationToken).ConfigureAwait(false), filteredGroups.Count());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using AutoFixture;
using Dfe.Academies.Application.Queries.Establishment;
using Dfe.Academies.Contracts.Establishments;
using Dfe.Academies.Contracts.Trusts;
using Dfe.Academies.Contracts.V4.Establishments;
using Dfe.Academies.Domain.Establishment;
using Dfe.Academies.Domain.Trust;
using FluentAssertions;
using Moq;
using System.Runtime.CompilerServices;

namespace Dfe.Academies.Application.Tests.Queries.Establishment
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AutoFixture;
using Dfe.Academies.Application.Queries.Trust;
using Dfe.Academies.Contracts.Trusts;
using Dfe.Academies.Contracts.V4.Trusts;
using Dfe.Academies.Domain.Trust;
using FluentAssertions;
using Moq;
Expand Down Expand Up @@ -57,7 +57,7 @@ public async Task Search_TrustsReturnedFromRepo_eturnsAListOfTrustDtosInAPagedRe
// Arrange
var trusts = _fixture.Create<List<Domain.Trust.Trust>>();
var mockRepo = new Mock<ITrustRepository>();
mockRepo.Setup(x => x.Search(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(trusts));
mockRepo.Setup(x => x.Search(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult((trusts, trusts.Count)));

var trustQueries = new TrustQueries(mockRepo.Object);
int page = 0;
Expand Down
5 changes: 3 additions & 2 deletions Dfe.Academies.Application/Builders/EstablishmentDtoBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.Academies.Contracts.Establishments;
using Dfe.Academies.Contracts.V4;
using Dfe.Academies.Contracts.V4.Establishments;

namespace Dfe.Academies.Application.Builders
{
Expand Down Expand Up @@ -128,7 +129,7 @@ public EstablishmentDtoBuilder WithMISEstablishment(Domain.Establishment.Establi

public EstablishmentDtoBuilder WithAddress(Domain.Establishment.Establishment establishment)
{
_dto.Address = new Contracts.Trusts.AddressDto()
_dto.Address = new AddressDto()
{
Street = establishment?.AddressLine1,
Town = establishment?.Town,
Expand Down
2 changes: 1 addition & 1 deletion Dfe.Academies.Application/Dfe.Academies.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.2" />
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Dfe.Academies.Contracts.Establishments;
using Dfe.Academies.Contracts.Trusts;
using Dfe.Academies.Contracts.V4.Establishments;
using Dfe.Academies.Domain.Establishment;
using System.Threading;
using System;

using Dfe.Academies.Application.Builders;
using Dfe.Academies.Domain.Trust;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using Dfe.Academies.Contracts.Establishments;
using Dfe.Academies.Contracts.Trusts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dfe.Academies.Contracts.V4.Establishments;

namespace Dfe.Academies.Application.Queries.Establishment
{
Expand Down
2 changes: 1 addition & 1 deletion Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Dfe.Academies.Contracts.Trusts;
using Dfe.Academies.Contracts.V4.Trusts;

namespace Dfe.Academies.Application.Queries.Trust
{
Expand Down
7 changes: 4 additions & 3 deletions Dfe.Academies.Application/Queries/Trust/TrustQueries.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.Academies.Contracts.Trusts;
using Dfe.Academies.Contracts.V4;
using Dfe.Academies.Contracts.V4.Trusts;
using Dfe.Academies.Domain.Trust;

namespace Dfe.Academies.Application.Queries.Trust
Expand All @@ -24,9 +25,9 @@ public TrustQueries(ITrustRepository trustRepository)

public async Task<(List<TrustDto>, int)> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken)
{
var trusts = await _trustRepository.Search(page, count, name, ukPrn, companiesHouseNumber, cancellationToken).ConfigureAwait(false);
var (trusts, recordCount) = await _trustRepository.Search(page, count, name, ukPrn, companiesHouseNumber, cancellationToken).ConfigureAwait(false);

return (trusts.Select(x => MapToTrustDto(x)).ToList(), trusts.Count);
return (trusts.Select(x => MapToTrustDto(x)).ToList(), recordCount);
}

public async Task<List<TrustDto>> GetByUkprns(string[] ukprns, CancellationToken cancellationToken)
Expand Down
2 changes: 1 addition & 1 deletion Dfe.Academies.Domain/Dfe.Academies.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.2" />
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.4" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Dfe.Academies.Domain/Trust/ITrustRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface ITrustRepository : IGenericRepository<Trust>
Task<Trust?> GetTrustByUkprn(string ukprn, CancellationToken cancellationToken);
Task<Trust?> GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken);
Task<List<Trust>> GetTrustsByUkprns(string[] ukprns, CancellationToken cancellationToken);
Task<List<Trust>> Search(int page, int count, string name, string ukPrn,
Task<(List<Trust>, int)> Search(int page, int count, string name, string ukPrn,
string companiesHouseNumber, CancellationToken cancellationToken);
}
}
2 changes: 1 addition & 1 deletion TramsDataApi.Test/TramsDataApi.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.0" />
<PackageReference Include="AutoFixture.Idioms" Version="4.18.0" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.18.0" />
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.2" />
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.4" />
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.19" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
Expand Down
4 changes: 1 addition & 3 deletions TramsDataApi/Controllers/V4/EstablishmentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
using System.Threading;
using System.Threading.Tasks;
using Dfe.Academies.Application.Queries.Establishment;
using Dfe.Academies.Contracts.Establishments;
using Dfe.Academies.Contracts.V4.Establishments;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Swashbuckle.AspNetCore.Annotations;
using TramsDataApi.RequestModels;
using TramsDataApi.ResponseModels;

namespace TramsDataApi.Controllers.V4
{
Expand Down
9 changes: 5 additions & 4 deletions TramsDataApi/Controllers/V4/TrustsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
using System.Threading;
using System.Threading.Tasks;
using Dfe.Academies.Application.Queries.Trust;
using Dfe.Academies.Contracts.Trusts;
using Dfe.Academies.Contracts.V4;
using Dfe.Academies.Contracts.V4.Trusts;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Swashbuckle.AspNetCore.Annotations;
Expand Down Expand Up @@ -96,7 +97,7 @@ public async Task<ActionResult<TrustDto>> GetTrustByCompaniesHouseNumber(string
[Route("trusts")]
[SwaggerOperation(Summary = "Search Trusts", Description = "Returns a list of Trusts based on search criteria.")]
[SwaggerResponse(200, "Successfully executed the search and returned Trusts.")]
public async Task<ActionResult<ApiResponseV2<TrustDto>>> SearchTrusts(string groupName, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken, int page = 1, int count = 10)
public async Task<ActionResult<PagedDataResponse<TrustDto>>> SearchTrusts(string groupName, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken, int page = 1, int count = 10)
{
_logger.LogInformation(
"Searching for trusts by groupName \"{name}\", UKPRN \"{prn}\", companiesHouseNumber \"{number}\", page {page}, count {count}",
Expand All @@ -111,8 +112,8 @@ public async Task<ActionResult<ApiResponseV2<TrustDto>>> SearchTrusts(string gro

_logger.LogDebug(JsonSerializer.Serialize(trusts));

var pagingResponse = PagingResponseFactory.Create(page, count, recordCount, Request);
var response = new ApiResponseV2<TrustDto>(trusts, pagingResponse);
var pagingResponse = PagingResponseFactory.CreateV4PagingResponse(page, count, recordCount, Request);
var response = new PagedDataResponse<TrustDto>(trusts, pagingResponse);

return Ok(response);
}
Expand Down
25 changes: 25 additions & 0 deletions TramsDataApi/ResponseModels/PagingResponseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,30 @@ public static PagingResponse Create(int page, int count, int recordCount, HttpRe

return pagingResponse;
}

public static Dfe.Academies.Contracts.V4.PagingResponse CreateV4PagingResponse(int page, int count, int recordCount, HttpRequest request)
{
var pagingResponse = new Dfe.Academies.Contracts.V4.PagingResponse
{
RecordCount = recordCount,
Page = page
};

if ((count * page) >= recordCount) return pagingResponse;

var queryAttributes = request.Query
.Where(q => q.Key != nameof(page) && q.Key != nameof(count))
.Select(q => new KeyValuePair<string, string>(q.Key, q.Value));

var queryBuilder = new QueryBuilder(queryAttributes)
{
{nameof(page), $"{page + 1}"},
{nameof(count), $"{count}"}
};

pagingResponse.NextPageUrl = $"{request.Path}{queryBuilder}";

return pagingResponse;
}
}
}
2 changes: 1 addition & 1 deletion TramsDataApi/TramsDataApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.2" />
<PackageReference Include="Dfe.Academies.Contracts" Version="1.0.4" />
<PackageReference Include="Dfe.Academisation.CorrelationIdMiddleware" Version="2.0.2" />
<PackageReference Include="FluentValidation" Version="11.6.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
Expand Down

0 comments on commit f34215b

Please sign in to comment.