diff --git a/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj b/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj index 819fb5f34..d225b02cb 100644 --- a/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj +++ b/Dfe.Academies.Api.Infrastructure/Dfe.Academies.Infrastructure.csproj @@ -7,7 +7,7 @@ - + all diff --git a/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs b/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs index 3647ea4ae..097ccf788 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs @@ -35,13 +35,13 @@ public async Task> GetTrustsByUkprns(string[] ukprns, CancellationTo return trusts; } - public async Task> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken) + public async Task<(List, int)> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken) { if (name == null && ukPrn == null && companiesHouseNumber == null) { List allTrusts = await dbSet.OrderBy(trust => trust.GroupUID).Skip((page - 1) * count) .Take(count).ToListAsync(cancellationToken).ConfigureAwait(false); - return allTrusts; + return (allTrusts, allTrusts.Count); } IOrderedQueryable filteredGroups = dbSet @@ -54,7 +54,7 @@ public async Task> 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()); } } } diff --git a/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs b/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs index df6a9421a..8f7b9d0d2 100644 --- a/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs +++ b/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs @@ -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 { diff --git a/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs b/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs index 728d1873c..1a9fea981 100644 --- a/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs +++ b/Dfe.Academies.Application.Tests/Queries/Trust/TrustQueriesTests.cs @@ -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; @@ -57,7 +57,7 @@ public async Task Search_TrustsReturnedFromRepo_eturnsAListOfTrustDtosInAPagedRe // Arrange var trusts = _fixture.Create>(); var mockRepo = new Mock(); - mockRepo.Setup(x => x.Search(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(trusts)); + mockRepo.Setup(x => x.Search(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult((trusts, trusts.Count))); var trustQueries = new TrustQueries(mockRepo.Object); int page = 0; diff --git a/Dfe.Academies.Application/Builders/EstablishmentDtoBuilder.cs b/Dfe.Academies.Application/Builders/EstablishmentDtoBuilder.cs index 038b60f11..aad060b1c 100644 --- a/Dfe.Academies.Application/Builders/EstablishmentDtoBuilder.cs +++ b/Dfe.Academies.Application/Builders/EstablishmentDtoBuilder.cs @@ -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 { @@ -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, diff --git a/Dfe.Academies.Application/Dfe.Academies.Application.csproj b/Dfe.Academies.Application/Dfe.Academies.Application.csproj index 2b119b832..e4ab16c2b 100644 --- a/Dfe.Academies.Application/Dfe.Academies.Application.csproj +++ b/Dfe.Academies.Application/Dfe.Academies.Application.csproj @@ -7,7 +7,7 @@ - + diff --git a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs index a2509a9ba..4c29a375b 100644 --- a/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/EstablishmentQueries.cs @@ -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; diff --git a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs index 569946379..002d47769 100644 --- a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs +++ b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs @@ -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 { diff --git a/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs b/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs index 7f0836cf4..4cef0318c 100644 --- a/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs +++ b/Dfe.Academies.Application/Queries/Trust/ITrustQueries.cs @@ -1,4 +1,4 @@ -using Dfe.Academies.Contracts.Trusts; +using Dfe.Academies.Contracts.V4.Trusts; namespace Dfe.Academies.Application.Queries.Trust { diff --git a/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs b/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs index 139a3bb1a..2cb527e26 100644 --- a/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs +++ b/Dfe.Academies.Application/Queries/Trust/TrustQueries.cs @@ -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 @@ -24,9 +25,9 @@ public TrustQueries(ITrustRepository trustRepository) public async Task<(List, 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> GetByUkprns(string[] ukprns, CancellationToken cancellationToken) diff --git a/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj b/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj index 6d51bbda4..4d76dbc8b 100644 --- a/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj +++ b/Dfe.Academies.Domain/Dfe.Academies.Domain.csproj @@ -7,7 +7,7 @@ - + diff --git a/Dfe.Academies.Domain/Trust/ITrustRepository.cs b/Dfe.Academies.Domain/Trust/ITrustRepository.cs index ad2dcc9a5..89af234e9 100644 --- a/Dfe.Academies.Domain/Trust/ITrustRepository.cs +++ b/Dfe.Academies.Domain/Trust/ITrustRepository.cs @@ -7,7 +7,7 @@ public interface ITrustRepository : IGenericRepository Task GetTrustByUkprn(string ukprn, CancellationToken cancellationToken); Task GetTrustByCompaniesHouseNumber(string companiesHouseNumber, CancellationToken cancellationToken); Task> GetTrustsByUkprns(string[] ukprns, CancellationToken cancellationToken); - Task> Search(int page, int count, string name, string ukPrn, + Task<(List, int)> Search(int page, int count, string name, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken); } } diff --git a/TramsDataApi.Test/TramsDataApi.Test.csproj b/TramsDataApi.Test/TramsDataApi.Test.csproj index a147d5745..9c381b73e 100644 --- a/TramsDataApi.Test/TramsDataApi.Test.csproj +++ b/TramsDataApi.Test/TramsDataApi.Test.csproj @@ -16,7 +16,7 @@ - + diff --git a/TramsDataApi/Controllers/V4/EstablishmentsController.cs b/TramsDataApi/Controllers/V4/EstablishmentsController.cs index abd404dc2..0d2612042 100644 --- a/TramsDataApi/Controllers/V4/EstablishmentsController.cs +++ b/TramsDataApi/Controllers/V4/EstablishmentsController.cs @@ -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 { diff --git a/TramsDataApi/Controllers/V4/TrustsController.cs b/TramsDataApi/Controllers/V4/TrustsController.cs index fcc1438f3..5836ab783 100644 --- a/TramsDataApi/Controllers/V4/TrustsController.cs +++ b/TramsDataApi/Controllers/V4/TrustsController.cs @@ -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; @@ -96,7 +97,7 @@ public async Task> 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>> SearchTrusts(string groupName, string ukPrn, string companiesHouseNumber, CancellationToken cancellationToken, int page = 1, int count = 10) + public async Task>> 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}", @@ -111,8 +112,8 @@ public async Task>> SearchTrusts(string gro _logger.LogDebug(JsonSerializer.Serialize(trusts)); - var pagingResponse = PagingResponseFactory.Create(page, count, recordCount, Request); - var response = new ApiResponseV2(trusts, pagingResponse); + var pagingResponse = PagingResponseFactory.CreateV4PagingResponse(page, count, recordCount, Request); + var response = new PagedDataResponse(trusts, pagingResponse); return Ok(response); } diff --git a/TramsDataApi/ResponseModels/PagingResponseFactory.cs b/TramsDataApi/ResponseModels/PagingResponseFactory.cs index 7c7c66fb2..f8e25c3ef 100644 --- a/TramsDataApi/ResponseModels/PagingResponseFactory.cs +++ b/TramsDataApi/ResponseModels/PagingResponseFactory.cs @@ -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(q.Key, q.Value)); + + var queryBuilder = new QueryBuilder(queryAttributes) + { + {nameof(page), $"{page + 1}"}, + {nameof(count), $"{count}"} + }; + + pagingResponse.NextPageUrl = $"{request.Path}{queryBuilder}"; + + return pagingResponse; + } } } \ No newline at end of file diff --git a/TramsDataApi/TramsDataApi.csproj b/TramsDataApi/TramsDataApi.csproj index fd8f654fa..5b46843ce 100644 --- a/TramsDataApi/TramsDataApi.csproj +++ b/TramsDataApi/TramsDataApi.csproj @@ -6,7 +6,7 @@ - +