From 26d80cf1bdd91c5f9ca36057c1644ad1e4bf5a26 Mon Sep 17 00:00:00 2001 From: plockwood Date: Thu, 9 Nov 2023 11:27:46 +0000 Subject: [PATCH 1/2] Trust serach paging fixed --- .../Dfe.Academies.Infrastructure.csproj | 2 +- .../Repositories/TrustRepository.cs | 6 ++--- .../EstablishmentQueriesTests.cs | 5 +--- .../Queries/Trust/TrustQueriesTests.cs | 4 +-- .../Builders/EstablishmentDtoBuilder.cs | 5 ++-- .../Dfe.Academies.Application.csproj | 2 +- .../Establishment/EstablishmentQueries.cs | 6 ++--- .../Establishment/IEstablishmentQueries.cs | 8 +----- .../Queries/Trust/ITrustQueries.cs | 2 +- .../Queries/Trust/TrustQueries.cs | 7 +++--- .../Dfe.Academies.Domain.csproj | 2 +- .../Trust/ITrustRepository.cs | 2 +- TramsDataApi.Test/TramsDataApi.Test.csproj | 2 +- .../V4/EstablishmentsController.cs | 4 +-- .../Controllers/V4/TrustsController.cs | 9 ++++--- .../ResponseModels/PagingResponseFactory.cs | 25 +++++++++++++++++++ TramsDataApi/TramsDataApi.csproj | 2 +- 17 files changed, 54 insertions(+), 39 deletions(-) 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 25a245bbd..00afef694 100644 --- a/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs +++ b/Dfe.Academies.Api.Infrastructure/Repositories/TrustRepository.cs @@ -27,13 +27,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 @@ -46,7 +46,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 b2aa15f92..20cbf532c 100644 --- a/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs +++ b/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs @@ -1,12 +1,9 @@ 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 108d873ce..2626ba48b 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; namespace Dfe.Academies.Application.Queries.Establishment diff --git a/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs b/Dfe.Academies.Application/Queries/Establishment/IEstablishmentQueries.cs index 229ad2a27..e08cfd2ab 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 3b03a8875..9099dbf2c 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 251b26e49..9be574a37 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 @@ -19,9 +20,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 6908e5c9f..8eaeb7003 100644 --- a/Dfe.Academies.Domain/Trust/ITrustRepository.cs +++ b/Dfe.Academies.Domain/Trust/ITrustRepository.cs @@ -6,7 +6,7 @@ public interface ITrustRepository : IGenericRepository { Task GetTrustByUkprn(string ukprn, 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 9533393c3..aae3b64ae 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 6a8b46247..7e64a9bcf 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; @@ -70,7 +71,7 @@ public async Task> GetTrustByUkprn(string ukprn, Cancella [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}", @@ -85,8 +86,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 @@ - + From 909523eee9f6c98d8e66c7b74f6579d60b48926b Mon Sep 17 00:00:00 2001 From: plockwood Date: Thu, 9 Nov 2023 11:36:46 +0000 Subject: [PATCH 2/2] test fix --- .../Queries/Establishment/EstablishmentQueriesTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs b/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs index 918d48709..8f7b9d0d2 100644 --- a/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs +++ b/Dfe.Academies.Application.Tests/Queries/Establishment/EstablishmentQueriesTests.cs @@ -2,6 +2,7 @@ using Dfe.Academies.Application.Queries.Establishment; using Dfe.Academies.Contracts.V4.Establishments; using Dfe.Academies.Domain.Establishment; +using Dfe.Academies.Domain.Trust; using FluentAssertions; using Moq;