diff --git a/TramsDataApi.Test/Controllers/TrustsControllerTests.cs b/TramsDataApi.Test/Controllers/TrustsControllerTests.cs index ab0ff39d1..b13669eb6 100644 --- a/TramsDataApi.Test/Controllers/TrustsControllerTests.cs +++ b/TramsDataApi.Test/Controllers/TrustsControllerTests.cs @@ -77,7 +77,7 @@ public void SearchTrusts_ReturnsEmptySetOfTrustSummaries_WhenNoTrustsFound() var companiesHouseNumber = "Mockcompanieshousenumber"; var searchTrusts = new Mock(); - searchTrusts.Setup(s => s.Execute(1, 10, groupName, ukprn, companiesHouseNumber)) + searchTrusts.Setup(s => s.Execute(1, 10, groupName, ukprn, companiesHouseNumber, true)) .Returns((new List(), 0)); var controller = new TrustsController(new Mock().Object, searchTrusts.Object, mockLogger.Object); @@ -99,7 +99,7 @@ public void SearchTrusts_ByGroupNameAndCompaniesHouseNumber_ReturnsListOfTrustSu .Build(); var searchTrusts = new Mock(); - searchTrusts.Setup(s => s.Execute(1, 10, groupName, null, companiesHouseNumber)) + searchTrusts.Setup(s => s.Execute(1, 10, groupName, null, companiesHouseNumber, true)) .Returns((expectedTrustSummaries, expectedTrustSummaries.Count)); var controller = new TrustsController(new Mock().Object, searchTrusts.Object, mockLogger.Object); @@ -120,7 +120,7 @@ public void SearchTrusts_ByUrn_ReturnsListOfTrustSummaries_WhenTrustsAreFound() .Build(); var searchTrusts = new Mock(); - searchTrusts.Setup(s => s.Execute(1, 10, null, ukprn, null)) + searchTrusts.Setup(s => s.Execute(1, 10, null, ukprn, null, true)) .Returns((expectedTrustSummaries, expectedTrustSummaries.Count)); var controller = new TrustsController(new Mock().Object, searchTrusts.Object, mockLogger.Object); @@ -135,7 +135,7 @@ public void SearchTrusts_WithNoParams_ReturnsAllTrusts() var expectedTrustSummaries = Builder.CreateListOfSize(5).Build(); var searchTrusts = new Mock(); - searchTrusts.Setup(s => s.Execute(1, 10, null, null, null)) + searchTrusts.Setup(s => s.Execute(1, 10, null, null, null, true)) .Returns((expectedTrustSummaries, expectedTrustSummaries.Count)); var controller = new TrustsController(new Mock().Object, searchTrusts.Object, mockLogger.Object); diff --git a/TramsDataApi.Test/Controllers/TrustsControllerV2Tests.cs b/TramsDataApi.Test/Controllers/TrustsControllerV2Tests.cs index 1c8261b5b..bd49a4884 100644 --- a/TramsDataApi.Test/Controllers/TrustsControllerV2Tests.cs +++ b/TramsDataApi.Test/Controllers/TrustsControllerV2Tests.cs @@ -98,7 +98,7 @@ public void SearchTrusts_ReturnsEmptySetOfTrustSummaries_WhenNoTrustsFound() NextPageUrl = null }; - _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, groupName, ukprn, companiesHouseNumber)) + _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, groupName, ukprn, companiesHouseNumber, true)) .Returns((new List(), 0)); var result = _controller.SearchTrusts(groupName, ukprn, companiesHouseNumber, 1, 10); @@ -126,7 +126,7 @@ public void SearchTrusts_ByGroupNameAndCompaniesHouseNumber_ReturnsListOfTrustSu NextPageUrl = null }; - _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, groupName, null, companiesHouseNumber)) + _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, groupName, null, companiesHouseNumber, true)) .Returns((expectedTrustSummaries, expectedTrustSummaries.Count)); var result = _controller.SearchTrusts(groupName, null, companiesHouseNumber, 1, 10); @@ -153,7 +153,7 @@ public void SearchTrusts_ByUrn_ReturnsListOfTrustSummaries_WhenTrustsAreFound() NextPageUrl = null }; - _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, null, ukprn, null)) + _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, null, ukprn, null, true)) .Returns((expectedTrustSummaries, expectedTrustSummaries.Count)); var result = _controller.SearchTrusts(null, ukprn, null, 1, 10); @@ -172,7 +172,7 @@ public void SearchTrusts_WithNoParams_ReturnsAllTrusts() RecordCount = 5, NextPageUrl = null }; - _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, null, null, null)) + _mockSearchTrustsUseCase.Setup(s => s.Execute(1, 10, null, null, null, true)) .Returns((expectedTrustSummaries, expectedTrustSummaries.Count)); var result = _controller.SearchTrusts(null, null, null, 1, 10); @@ -194,7 +194,7 @@ public void SearchTrusts_WithMultiplePagesOfResults_ReturnsAllTrustsWithPagingRe }; _mockSearchTrustsUseCase - .Setup(s => s.Execute(1, 10, null, null, null)) + .Setup(s => s.Execute(1, 10, null, null, null, true)) .Returns((expectedTrustSummaries.Take(10).ToList(), expectedTrustSummaries.Count)); _controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }; diff --git a/TramsDataApi.Test/UseCases/SearchTrustsTests.cs b/TramsDataApi.Test/UseCases/SearchTrustsTests.cs index 432812358..925923cef 100644 --- a/TramsDataApi.Test/UseCases/SearchTrustsTests.cs +++ b/TramsDataApi.Test/UseCases/SearchTrustsTests.cs @@ -50,11 +50,14 @@ public void SearchTrusts_ReturnsListOfTrustSummaryResponses_WhenTrustsFound() var trustsGateway = new Mock(); var establishmentsGateway = new Mock(); - - trustsGateway.Setup(g => g.SearchGroups(1, 10, groupName, null, null)) + + trustsGateway.Setup(g => g.SearchGroups(1, 10, groupName, string.Empty, string.Empty)) .Returns((expectedTrusts, expectedTrusts.Count)); - establishmentsGateway.Setup(g => g.GetByTrustUid(It.IsAny())) + trustsGateway.Setup(m => m.GetIfdTrustsByTrustRef(It.IsAny())) + .Returns(new List()); + + establishmentsGateway.Setup(g => g.GetByTrustUids(It.IsAny())) .Returns(new List()); var expected = expectedTrusts. @@ -62,7 +65,7 @@ public void SearchTrusts_ReturnsListOfTrustSummaryResponses_WhenTrustsFound() .ToList(); var searchTrusts = new SearchTrusts(trustsGateway.Object, establishmentsGateway.Object); - var (result, _) = searchTrusts.Execute(1, 10, groupName, null, null); + var (result, _) = searchTrusts.Execute(1, 10, groupName); result.Should().BeEquivalentTo(expected); } @@ -81,12 +84,16 @@ public void SearchTrusts_ShouldGetTrustsWithEstablishments_WhenTrustsAndEstablis .With(e => e.TrustsCode = expectedTrust.GroupUid) .Build(); + var trusts = Builder.CreateListOfSize(3).Build(); + var trustGateway = new Mock(); var establishmentGateway = new Mock(); - trustGateway.Setup(g => g.SearchGroups(1, 10, null, ukprn, null)) + trustGateway.Setup(g => g.SearchGroups(1, 10, string.Empty, ukprn, string.Empty)) .Returns((new List {expectedTrust}, 1)); - establishmentGateway.Setup(g => g.GetByTrustUid(expectedTrust.GroupUid)) + trustGateway.Setup(m => m.GetIfdTrustsByTrustRef(It.IsAny())) + .Returns(trusts); + establishmentGateway.Setup(g => g.GetByTrustUids(It.IsAny())) .Returns(expectedEstablishments); var expected = new List @@ -95,7 +102,34 @@ public void SearchTrusts_ShouldGetTrustsWithEstablishments_WhenTrustsAndEstablis }; var searchTrusts = new SearchTrusts(trustGateway.Object, establishmentGateway.Object); - var (result, _) = searchTrusts.Execute(1, 10, null, ukprn, null); + var (result, _) = searchTrusts.Execute(1, 10, ukPrn: ukprn); + result.Should().BeEquivalentTo(expected); + } + + [Fact] + public void SearchTrusts_WithoutEstablishments() + { + var ukprn = "mockurn"; + var expectedTrust = Builder + .CreateNew() + .With(g => g.Ukprn = ukprn) + .Build(); + + var trusts = Builder.CreateListOfSize(3).Build(); + var trustGateway = new Mock(); + + trustGateway.Setup(g => g.SearchGroups(1, 10, string.Empty, ukprn, string.Empty)) + .Returns((new List {expectedTrust}, 1)); + trustGateway.Setup(m => m.GetIfdTrustsByTrustRef(It.IsAny())) + .Returns(trusts); + + var expected = new List + { + TrustSummaryResponseFactory.Create(expectedTrust, Enumerable.Empty(), null) + }; + + var searchTrusts = new SearchTrusts(trustGateway.Object, null); + var (result, _) = searchTrusts.Execute(1, 10, ukPrn: ukprn, includeEstablishments: false); result.Should().BeEquivalentTo(expected); } } diff --git a/TramsDataApi/Controllers/TrustsController.cs b/TramsDataApi/Controllers/TrustsController.cs index 48f2ea516..73e9da935 100644 --- a/TramsDataApi/Controllers/TrustsController.cs +++ b/TramsDataApi/Controllers/TrustsController.cs @@ -50,7 +50,7 @@ public ActionResult> SearchTrusts(string groupName, s groupName, ukPrn, companiesHouseNumber, page, count); var trusts = _searchTrusts - .Execute(page, count, groupName, ukPrn, companiesHouseNumber) + .Execute(page, count, groupName, ukPrn, companiesHouseNumber, true) .Item1.ToList(); _logger.LogInformation( diff --git a/TramsDataApi/Controllers/V2/TrustsController.cs b/TramsDataApi/Controllers/V2/TrustsController.cs index 31d9f2f49..6ea60499a 100644 --- a/TramsDataApi/Controllers/V2/TrustsController.cs +++ b/TramsDataApi/Controllers/V2/TrustsController.cs @@ -29,15 +29,17 @@ public TrustsController(IGetTrustByUkprn getTrustByUkPrn, ISearchTrusts searchTr [HttpGet("trusts")] [MapToApiVersion("2.0")] - public ActionResult> SearchTrusts(string groupName, string ukPrn, string companiesHouseNumber, int page = 1, int count = 50) + public ActionResult> SearchTrusts(string groupName, string ukPrn, string companiesHouseNumber, + int page = 1, int count = 50, bool includeEstablishments = true) { _logger.LogInformation( "Searching for trusts by groupName \"{name}\", UKPRN \"{prn}\", companiesHouseNumber \"{number}\", page {page}, count {count}", groupName, ukPrn, companiesHouseNumber, page, count); var (trusts, recordCount) = _searchTrusts - .Execute(page, count, groupName, ukPrn, companiesHouseNumber); - + .Execute(page, count, groupName, ukPrn, companiesHouseNumber, includeEstablishments); + trusts = trusts.ToList(); + _logger.LogInformation( "Found {count} trusts for groupName \"{name}\", UKPRN \"{prn}\", companiesHouseNumber \"{number}\", page {page}, count {count}", trusts.Count(), groupName, ukPrn, companiesHouseNumber, page, count); diff --git a/TramsDataApi/Gateways/ITrustGateway.cs b/TramsDataApi/Gateways/ITrustGateway.cs index fa5c2af8e..b5eccd76b 100644 --- a/TramsDataApi/Gateways/ITrustGateway.cs +++ b/TramsDataApi/Gateways/ITrustGateway.cs @@ -9,7 +9,7 @@ public interface ITrustGateway Group GetGroupByUkPrn(string ukPrn); Trust GetIfdTrustByGroupId(string groupId); Trust GetIfdTrustByRID(string RID); - IQueryable GetIfdTrustsByTrustRef(string[] trustRefs); + IList GetIfdTrustsByTrustRef(string[] trustRefs); (IList, int) SearchGroups(int page, int count, string groupName, string ukPrn, string companiesHouseNumber); IEnumerable GetMultipleGroupsByUkprn(IEnumerable ukprns); IEnumerable GetMultipleTrustsByGroupId(IEnumerable groupIds); diff --git a/TramsDataApi/Gateways/TrustGateway.cs b/TramsDataApi/Gateways/TrustGateway.cs index e7ed3071f..a99bd1db1 100644 --- a/TramsDataApi/Gateways/TrustGateway.cs +++ b/TramsDataApi/Gateways/TrustGateway.cs @@ -32,12 +32,9 @@ public Trust GetIfdTrustByRID(string RID) return _dbContext.Trust.FirstOrDefault(x => x.Rid.Equals(RID)); } - public IQueryable GetIfdTrustsByTrustRef(string[] trustRefs) + public IList GetIfdTrustsByTrustRef(string[] trustRefs) { - Expression> predicate = PredicateBuilder.False(); - foreach (var trustRef in trustRefs) predicate = predicate.Or(t => t.TrustRef == trustRef); - - return _dbContext.Trust.Where(predicate); + return _dbContext.Trust.Where(t => trustRefs.Contains(t.TrustRef)).ToList(); } public (IList, int) SearchGroups(int page, int count, string groupName, string ukPrn, diff --git a/TramsDataApi/UseCases/ISearchTrusts.cs b/TramsDataApi/UseCases/ISearchTrusts.cs index 756d39235..7008eeaf9 100644 --- a/TramsDataApi/UseCases/ISearchTrusts.cs +++ b/TramsDataApi/UseCases/ISearchTrusts.cs @@ -5,6 +5,12 @@ namespace TramsDataApi.UseCases { public interface ISearchTrusts { - public (IEnumerable, int) Execute(int page, int count, string groupName, string urn, string companiesHouseNumber); + public (IEnumerable, int) Execute( + int page = 1, + int count = 50, + string groupName = "", + string ukPrn = "", + string companiesHouseNumber = "", + bool includeEstablishments = true); } } \ No newline at end of file diff --git a/TramsDataApi/UseCases/SearchTrusts.cs b/TramsDataApi/UseCases/SearchTrusts.cs index be1ad8260..5761f4cb1 100644 --- a/TramsDataApi/UseCases/SearchTrusts.cs +++ b/TramsDataApi/UseCases/SearchTrusts.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using TramsDataApi.DatabaseModels; using TramsDataApi.Factories; using TramsDataApi.Gateways; using TramsDataApi.ResponseModels; @@ -17,15 +18,32 @@ public SearchTrusts(ITrustGateway trustGateway, IEstablishmentGateway establishm _establishmentGateway = establishmentGateway; } - public (IEnumerable, int) Execute(int page, int count, string groupName, string ukPrn, string companiesHouseNumber) + public (IEnumerable, int) Execute( + int page = 1, + int count = 50, + string groupName = "", + string ukPrn = "", + string companiesHouseNumber = "", + bool includeEstablishments = true) { var (groups, recordCount) = _trustGateway.SearchGroups(page, count, groupName, ukPrn, companiesHouseNumber); + var groupIds = groups.Select(g => g.GroupId).ToArray(); + var trustsForGroup = _trustGateway.GetIfdTrustsByTrustRef(groupIds); + + IEnumerable establishmentsForGroup = Enumerable.Empty(); + + if (includeEstablishments) + { + var groupUids = groups.Select(g => g.GroupUid).ToArray(); + establishmentsForGroup = _establishmentGateway.GetByTrustUids(groupUids); + } + return ( groups.Select(group => { - var trust = _trustGateway.GetIfdTrustByGroupId(group.GroupId); - var establishments = _establishmentGateway.GetByTrustUid(group.GroupUid); + var establishments = establishmentsForGroup.Where(e => e.TrustsCode == group.GroupUid); + var trust = trustsForGroup.FirstOrDefault(e => e.TrustRef == group.GroupUid); return TrustSummaryResponseFactory.Create(group, establishments, trust); }).ToArray(), recordCount