From d7e1ba00767ae011819060df12184ace1f255b34 Mon Sep 17 00:00:00 2001 From: Hamid Jolany Date: Tue, 23 Nov 2021 00:43:57 +0330 Subject: [PATCH 1/6] Making HousingSearchRequest as abstract --- .../GetPersonListRequestValidatorTests.cs | 4 ++-- .../GetAssetListControllerTests.cs | 2 +- .../GetTenureListControllerTests.cs | 2 +- .../V1/Helper/PersonListSortFactoryTests.cs | 6 ++--- .../V1/Helper/SearchPhraseTests.cs | 2 +- .../V1/Interfaces/SortFactoryTests.cs | 2 +- .../Boundary/Requests/AccountSearchRequest.cs | 24 ------------------- .../Requests/GetAccountListRequest.cs | 16 ------------- .../Boundary/Requests/GetAssetListRequest.cs | 10 ++++++++ .../Boundary/Requests/GetTenureListRequest.cs | 9 +++++++ .../Boundary/Requests/HousingSearchRequest.cs | 7 +----- .../V1/Controllers/GetAssetListController.cs | 2 +- .../V1/Controllers/GetTenureListController.cs | 2 +- .../V1/Gateways/GetAccountGateway.cs | 2 +- .../Gateways/Interfaces/IGetAccountGateway.cs | 2 +- .../V1/Gateways/Interfaces/ISearchGateway.cs | 8 +++---- HousingSearchApi/V1/Gateways/SearchGateway.cs | 16 ++++++------- .../ElasticElasticSearchWrapper.cs | 16 +++++++------ .../Factories/AccountQueryGenerator.cs | 13 ++++++---- .../Factories/AssetQueryGenerator.cs | 23 +++++++++++------- .../Factories/PersonQueryGenerator.cs | 12 ++++++---- .../Infrastructure/Factories/QueryFactory.cs | 3 +-- .../Factories/TenureQueryGenerator.cs | 10 +++++--- .../V1/Infrastructure/Sorting/SortFactory.cs | 6 ++--- .../V1/Interfaces/Factories/IQueryFactory.cs | 2 +- .../Interfaces/Factories/IQueryGenerator.cs | 4 +--- .../V1/Interfaces/IElasticSearchWrapper.cs | 2 +- .../V1/Interfaces/Sorting/ISortFactory.cs | 2 +- .../V1/UseCase/GetAccountListUseCase.cs | 2 +- .../V1/UseCase/GetAssetListUseCase.cs | 2 +- .../V1/UseCase/GetPersonListUseCase.cs | 2 +- .../V1/UseCase/GetTenureListUseCase.cs | 2 +- .../Interfaces/IGetAccountListUseCase.cs | 2 +- .../Interfaces/IGetAssetListUseCase.cs | 2 +- .../Interfaces/IGetPersonListUseCase.cs | 2 +- .../Interfaces/IGetTenureListUseCase.cs | 2 +- 36 files changed, 107 insertions(+), 118 deletions(-) delete mode 100644 HousingSearchApi/V1/Boundary/Requests/AccountSearchRequest.cs create mode 100644 HousingSearchApi/V1/Boundary/Requests/GetAssetListRequest.cs create mode 100644 HousingSearchApi/V1/Boundary/Requests/GetTenureListRequest.cs diff --git a/HousingSearchApi.Tests/V1/Boundary/Requests/Validation/GetPersonListRequestValidatorTests.cs b/HousingSearchApi.Tests/V1/Boundary/Requests/Validation/GetPersonListRequestValidatorTests.cs index 6c9a4407..dc12e783 100644 --- a/HousingSearchApi.Tests/V1/Boundary/Requests/Validation/GetPersonListRequestValidatorTests.cs +++ b/HousingSearchApi.Tests/V1/Boundary/Requests/Validation/GetPersonListRequestValidatorTests.cs @@ -14,9 +14,9 @@ public GetPersonListRequestValidatorTests() _sut = new HousingSearchRequestValidator(); } - private static HousingSearchRequest CreateValidRequest() + private static GetPersonListRequest CreateValidRequest() { - return new HousingSearchRequest() + return new GetPersonListRequest() { SearchText = "Some search text" }; diff --git a/HousingSearchApi.Tests/V1/Controllers/GetAssetListControllerTests.cs b/HousingSearchApi.Tests/V1/Controllers/GetAssetListControllerTests.cs index 7ad9976e..45185160 100644 --- a/HousingSearchApi.Tests/V1/Controllers/GetAssetListControllerTests.cs +++ b/HousingSearchApi.Tests/V1/Controllers/GetAssetListControllerTests.cs @@ -26,7 +26,7 @@ public GetTenureListControllerTests() public async Task GetTenureListShouldCallGetTenureListUseCase() { // given - var request = new HousingSearchRequest(); + var request = new GetAssetListRequest(); var response = new GetAssetListResponse(); _mockGetAssetListUseCase.Setup(x => x.ExecuteAsync(request)).ReturnsAsync(response); diff --git a/HousingSearchApi.Tests/V1/Controllers/GetTenureListControllerTests.cs b/HousingSearchApi.Tests/V1/Controllers/GetTenureListControllerTests.cs index 7ec7841c..80b41e29 100644 --- a/HousingSearchApi.Tests/V1/Controllers/GetTenureListControllerTests.cs +++ b/HousingSearchApi.Tests/V1/Controllers/GetTenureListControllerTests.cs @@ -27,7 +27,7 @@ public GetAssetListControllerTests() public async Task GetAssetListShouldCallGetAssetListUseCase() { // given - var request = new HousingSearchRequest(); + var request = new GetAssetListRequest(); var response = new GetAssetListResponse(); _mockGetAssetListUseCase.Setup(x => x.ExecuteAsync(request)).ReturnsAsync(response); diff --git a/HousingSearchApi.Tests/V1/Helper/PersonListSortFactoryTests.cs b/HousingSearchApi.Tests/V1/Helper/PersonListSortFactoryTests.cs index d9b7e6fb..5ab4cfe3 100644 --- a/HousingSearchApi.Tests/V1/Helper/PersonListSortFactoryTests.cs +++ b/HousingSearchApi.Tests/V1/Helper/PersonListSortFactoryTests.cs @@ -19,7 +19,7 @@ public PersonListSortFactoryTests() public void ShouldNotSortAsDefault() { // Arrange + Act - var result = _sut.Create(new HousingSearchRequest()); + var result = _sut.Create(new GetPersonListRequest()); // Assert result.Should().BeOfType(typeof(DefaultSort)); @@ -29,7 +29,7 @@ public void ShouldNotSortAsDefault() public void ShouldReturnSurnameAscWhenRequestSurnameAndAsc() { // Arrange + Act - var result = _sut.Create(new HousingSearchRequest { SortBy = "surname", IsDesc = false }); + var result = _sut.Create(new GetPersonListRequest { SortBy = "surname", IsDesc = false }); // Assert result.Should().BeOfType(typeof(SurnameAsc)); @@ -39,7 +39,7 @@ public void ShouldReturnSurnameAscWhenRequestSurnameAndAsc() public void ShouldReturnSurnameDescWhenRequestSurnameAndDesc() { // Arrange + Act - var result = _sut.Create(new HousingSearchRequest { SortBy = "surname", IsDesc = true }); + var result = _sut.Create(new GetPersonListRequest { SortBy = "surname", IsDesc = true }); // Assert result.Should().BeOfType(typeof(SurnameDesc)); diff --git a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs index 26d60016..288eb7bd 100644 --- a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs +++ b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs @@ -24,7 +24,7 @@ public SearchPhraseTests() public void ShouldReturnNullIfRequestTypeIsUnknown(string searchText) { // Arrange + Act - var result = _sut.Create(new HousingSearchRequest { SearchText = searchText }, new QueryContainerDescriptor()); + var result = _sut.Create(new GetPersonListRequest{ SearchText = searchText }, new QueryContainerDescriptor()); // Assert result.Should().BeNull(); diff --git a/HousingSearchApi.Tests/V1/Interfaces/SortFactoryTests.cs b/HousingSearchApi.Tests/V1/Interfaces/SortFactoryTests.cs index d124082b..b8f9bf3b 100644 --- a/HousingSearchApi.Tests/V1/Interfaces/SortFactoryTests.cs +++ b/HousingSearchApi.Tests/V1/Interfaces/SortFactoryTests.cs @@ -18,7 +18,7 @@ public SortFactoryTests() public void GivenARequestShouldReturnDefaultSortForUnknownType() { // Arrange + act - var result = _sut.Create(new HousingSearchRequest()); + var result = _sut.Create(new GetPersonListRequest()); // Assert result.Should().BeOfType>(); diff --git a/HousingSearchApi/V1/Boundary/Requests/AccountSearchRequest.cs b/HousingSearchApi/V1/Boundary/Requests/AccountSearchRequest.cs deleted file mode 100644 index f11a7b23..00000000 --- a/HousingSearchApi/V1/Boundary/Requests/AccountSearchRequest.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace HousingSearchApi.V1.Boundary.Requests -{ - public class AccountSearchRequest - { - private const int DefaultPageSize = 12; - - [FromQuery(Name = "searchText")] - public string SearchText { get; set; } - - [FromQuery(Name = "pageSize")] - public int PageSize { get; set; } = DefaultPageSize; - - [FromQuery(Name = "page")] - public int Page { get; set; } - - [FromQuery(Name = "sortBy")] - public string SortBy { get; set; } - - [FromQuery(Name = "isDesc")] - public bool IsDesc { get; set; } - } -} diff --git a/HousingSearchApi/V1/Boundary/Requests/GetAccountListRequest.cs b/HousingSearchApi/V1/Boundary/Requests/GetAccountListRequest.cs index b89d16af..61c7a910 100644 --- a/HousingSearchApi/V1/Boundary/Requests/GetAccountListRequest.cs +++ b/HousingSearchApi/V1/Boundary/Requests/GetAccountListRequest.cs @@ -2,22 +2,6 @@ namespace HousingSearchApi.V1.Boundary.Requests { public class GetAccountListRequest : HousingSearchRequest { - /*private const int DefaultPageSize = 12; - [FromQuery(Name = "searchText")] - public string SearchText { get; set; } - - [FromQuery(Name = "pageSize")] - public int PageSize { get; set; } = DefaultPageSize; - - [FromQuery(Name = "pageNumber")] - public int PageNumber { get; set; } = 1; - - [FromQuery(Name = "sortBy")] - [JsonConverter(typeof(StringEnumConverter))] - public AccountSortBy SortBy { get; set; } - - [FromQuery(Name = "isDesc")] - public bool IsDesc { get; set; }*/ } } diff --git a/HousingSearchApi/V1/Boundary/Requests/GetAssetListRequest.cs b/HousingSearchApi/V1/Boundary/Requests/GetAssetListRequest.cs new file mode 100644 index 00000000..16998907 --- /dev/null +++ b/HousingSearchApi/V1/Boundary/Requests/GetAssetListRequest.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Mvc; + +namespace HousingSearchApi.V1.Boundary.Requests +{ + public class GetAssetListRequest : HousingSearchRequest + { + [FromQuery(Name = "assetTypes")] + public string AssetTypes { get; set; } + } +} diff --git a/HousingSearchApi/V1/Boundary/Requests/GetTenureListRequest.cs b/HousingSearchApi/V1/Boundary/Requests/GetTenureListRequest.cs new file mode 100644 index 00000000..ddec51a8 --- /dev/null +++ b/HousingSearchApi/V1/Boundary/Requests/GetTenureListRequest.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.Mvc; + +namespace HousingSearchApi.V1.Boundary.Requests +{ + public class GetTenureListRequest : HousingSearchRequest + { + + } +} diff --git a/HousingSearchApi/V1/Boundary/Requests/HousingSearchRequest.cs b/HousingSearchApi/V1/Boundary/Requests/HousingSearchRequest.cs index 560e233b..67416ca4 100644 --- a/HousingSearchApi/V1/Boundary/Requests/HousingSearchRequest.cs +++ b/HousingSearchApi/V1/Boundary/Requests/HousingSearchRequest.cs @@ -2,16 +2,13 @@ namespace HousingSearchApi.V1.Boundary.Requests { - public class HousingSearchRequest + public abstract class HousingSearchRequest { private const int DefaultPageSize = 12; [FromQuery(Name = "searchText")] public string SearchText { get; set; } - [FromQuery(Name = "assetTypes")] - public string AssetTypes { get; set; } - [FromQuery(Name = "pageSize")] public int PageSize { get; set; } = DefaultPageSize; @@ -24,7 +21,5 @@ public class HousingSearchRequest [FromQuery(Name = "isDesc")] public bool IsDesc { get; set; } - [FromQuery(Name = "propertyReference")] - public string PropertyReference { get; set; } } } diff --git a/HousingSearchApi/V1/Controllers/GetAssetListController.cs b/HousingSearchApi/V1/Controllers/GetAssetListController.cs index e0b785a1..a5831121 100644 --- a/HousingSearchApi/V1/Controllers/GetAssetListController.cs +++ b/HousingSearchApi/V1/Controllers/GetAssetListController.cs @@ -29,7 +29,7 @@ public GetAssetListController(IGetAssetListUseCase getAssetListUseCase) [ProducesResponseType(typeof(APIResponse), 400)] [HttpGet, MapToApiVersion("1")] [LogCall(LogLevel.Information)] - public async Task GetAssetList([FromQuery] HousingSearchRequest request) + public async Task GetAssetList([FromQuery] GetAssetListRequest request) { try { diff --git a/HousingSearchApi/V1/Controllers/GetTenureListController.cs b/HousingSearchApi/V1/Controllers/GetTenureListController.cs index a2b35df7..fe4021ec 100644 --- a/HousingSearchApi/V1/Controllers/GetTenureListController.cs +++ b/HousingSearchApi/V1/Controllers/GetTenureListController.cs @@ -29,7 +29,7 @@ public GetTenureListController(IGetTenureListUseCase getTenureListUseCase) [ProducesResponseType(typeof(APIResponse), 400)] [HttpGet, MapToApiVersion("1")] [LogCall(LogLevel.Information)] - public async Task GetTenureList([FromQuery] HousingSearchRequest request) + public async Task GetTenureList([FromQuery] GetTenureListRequest request) { try { diff --git a/HousingSearchApi/V1/Gateways/GetAccountGateway.cs b/HousingSearchApi/V1/Gateways/GetAccountGateway.cs index 751a695d..c3d3f0e1 100644 --- a/HousingSearchApi/V1/Gateways/GetAccountGateway.cs +++ b/HousingSearchApi/V1/Gateways/GetAccountGateway.cs @@ -19,7 +19,7 @@ public GetAccountGateway(ISearchGateway searchGateway, ILogger Search(HousingSearchRequest parameters) + public async Task Search(GetAccountListRequest parameters) { _logger.LogInformation("Housing search api for getting account list called."); return await _searchGateway.GetListOfAccounts(parameters).ConfigureAwait(false); diff --git a/HousingSearchApi/V1/Gateways/Interfaces/IGetAccountGateway.cs b/HousingSearchApi/V1/Gateways/Interfaces/IGetAccountGateway.cs index 70131803..55687e8b 100644 --- a/HousingSearchApi/V1/Gateways/Interfaces/IGetAccountGateway.cs +++ b/HousingSearchApi/V1/Gateways/Interfaces/IGetAccountGateway.cs @@ -6,6 +6,6 @@ namespace HousingSearchApi.V1.Gateways.Interfaces { public interface IGetAccountGateway { - Task Search(HousingSearchRequest parameters); + Task Search(GetAccountListRequest parameters); } } diff --git a/HousingSearchApi/V1/Gateways/Interfaces/ISearchGateway.cs b/HousingSearchApi/V1/Gateways/Interfaces/ISearchGateway.cs index 15312a1f..d8c702a5 100644 --- a/HousingSearchApi/V1/Gateways/Interfaces/ISearchGateway.cs +++ b/HousingSearchApi/V1/Gateways/Interfaces/ISearchGateway.cs @@ -6,9 +6,9 @@ namespace HousingSearchApi.V1.Gateways.Interfaces { public interface ISearchGateway { - Task GetListOfPersons(HousingSearchRequest query); - Task GetListOfTenures(HousingSearchRequest query); - Task GetListOfAssets(HousingSearchRequest query); - Task GetListOfAccounts(HousingSearchRequest query); + Task GetListOfPersons(GetPersonListRequest query); + Task GetListOfTenures(GetTenureListRequest query); + Task GetListOfAssets(GetAssetListRequest query); + Task GetListOfAccounts(GetAccountListRequest query); } } diff --git a/HousingSearchApi/V1/Gateways/SearchGateway.cs b/HousingSearchApi/V1/Gateways/SearchGateway.cs index d61d75ed..f69e0d84 100644 --- a/HousingSearchApi/V1/Gateways/SearchGateway.cs +++ b/HousingSearchApi/V1/Gateways/SearchGateway.cs @@ -22,9 +22,9 @@ public SearchGateway(IElasticSearchWrapper elasticSearchWrapper) } [LogCall] - public async Task GetListOfPersons(HousingSearchRequest query) + public async Task GetListOfPersons(GetPersonListRequest query) { - var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); + var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); var personListResponse = new GetPersonListResponse(); personListResponse.Persons.AddRange(searchResponse.Documents.Select(queryablePerson => @@ -37,9 +37,9 @@ public async Task GetListOfPersons(HousingSearchRequest q } [LogCall] - public async Task GetListOfTenures(HousingSearchRequest query) + public async Task GetListOfTenures(GetTenureListRequest query) { - var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); + var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); var tenureListResponse = new GetTenureListResponse(); tenureListResponse.Tenures.AddRange(searchResponse.Documents.Select(queryableTenure => @@ -52,9 +52,9 @@ public async Task GetListOfTenures(HousingSearchRequest q } [LogCall] - public async Task GetListOfAssets(HousingSearchRequest query) + public async Task GetListOfAssets(GetAssetListRequest query) { - var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); + var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); var assetListResponse = new GetAssetListResponse(); assetListResponse.Assets.AddRange(searchResponse.Documents.Select(queryableAsset => @@ -66,9 +66,9 @@ public async Task GetListOfAssets(HousingSearchRequest que return assetListResponse; } - public async Task GetListOfAccounts(HousingSearchRequest query) + public async Task GetListOfAccounts(GetAccountListRequest query) { - var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); + var searchResponse = await _elasticSearchWrapper.Search(query).ConfigureAwait(false); var accountListResponse = GetAccountListResponse.Create(searchResponse.Documents.Select(queryableAccount => queryableAccount.ToAccount())?.ToList()); diff --git a/HousingSearchApi/V1/Infrastructure/ElasticElasticSearchWrapper.cs b/HousingSearchApi/V1/Infrastructure/ElasticElasticSearchWrapper.cs index 15322932..46d89a7e 100644 --- a/HousingSearchApi/V1/Infrastructure/ElasticElasticSearchWrapper.cs +++ b/HousingSearchApi/V1/Infrastructure/ElasticElasticSearchWrapper.cs @@ -30,7 +30,7 @@ public ElasticSearchWrapper(IElasticClient esClient, IQueryFactory queryFactory, _indexSelector = indexSelector; } - public async Task> Search(HousingSearchRequest request) where T : class + public async Task> Search(TRequest request) where T : class where TRequest : class { try { @@ -40,12 +40,14 @@ public async Task> Search(HousingSearchRequest request) wh if (request == null) return new SearchResponse(); - var pageOffset = _pagingHelper.GetPageOffset(request.PageSize, request.Page); + HousingSearchRequest searchRequest = (HousingSearchRequest) (object) request; + + var pageOffset = _pagingHelper.GetPageOffset(searchRequest.PageSize, searchRequest.Page); var result = await _esClient.SearchAsync(x => x.Index(_indexSelector.Create()) - .Query(q => BaseQuery(request).Create(request, q)) - .Sort(_iSortFactory.Create(request).GetSortDescriptor) - .Size(request.PageSize) + .Query(q => BaseQuery().Create(request, q)) + .Sort(_iSortFactory.Create(request).GetSortDescriptor) + .Size(searchRequest.PageSize) .Skip(pageOffset) .TrackTotalHits()).ConfigureAwait(false); @@ -60,9 +62,9 @@ public async Task> Search(HousingSearchRequest request) wh } } - private IQueryGenerator BaseQuery(HousingSearchRequest request) where T : class + private IQueryGenerator BaseQuery() where T : class { - return _queryFactory.CreateQuery(request); + return _queryFactory.CreateQuery(); } } } diff --git a/HousingSearchApi/V1/Infrastructure/Factories/AccountQueryGenerator.cs b/HousingSearchApi/V1/Infrastructure/Factories/AccountQueryGenerator.cs index 2ec7e175..6747b4d5 100644 --- a/HousingSearchApi/V1/Infrastructure/Factories/AccountQueryGenerator.cs +++ b/HousingSearchApi/V1/Infrastructure/Factories/AccountQueryGenerator.cs @@ -17,16 +17,19 @@ public AccountQueryGenerator(IQueryBuilder queryBuilder) _queryBuilder = queryBuilder; } - public QueryContainer Create(HousingSearchRequest request, QueryContainerDescriptor q) + + public QueryContainer Create(TRequest request, QueryContainerDescriptor q) { - if (request == null) + GetAccountListRequest accountListRequest = request as GetAccountListRequest; + if (accountListRequest == null) throw new ArgumentNullException($"{nameof(request).ToString()} shouldn't be null."); - if (!string.IsNullOrEmpty(request.SearchText)) + + if (!string.IsNullOrEmpty(accountListRequest.SearchText)) _queryBuilder - .WithWildstarQuery(request.SearchText, + .WithWildstarQuery(accountListRequest.SearchText, new List { "paymentReference", "tenure.fullAddress", "tenure.primaryTenants.fullName" }) - .WithExactQuery(request.SearchText, + .WithExactQuery(accountListRequest.SearchText, new List { "paymentReference", "tenure.fullAddress", "tenure.primaryTenants.fullName" }); return _queryBuilder.Build(q); diff --git a/HousingSearchApi/V1/Infrastructure/Factories/AssetQueryGenerator.cs b/HousingSearchApi/V1/Infrastructure/Factories/AssetQueryGenerator.cs index bdaf54a4..f607e6bd 100644 --- a/HousingSearchApi/V1/Infrastructure/Factories/AssetQueryGenerator.cs +++ b/HousingSearchApi/V1/Infrastructure/Factories/AssetQueryGenerator.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Hackney.Core.ElasticSearch.Interfaces; using Hackney.Shared.HousingSearch.Gateways.Models.Assets; @@ -16,20 +17,24 @@ public AssetQueryGenerator(IQueryBuilder queryBuilder) _queryBuilder = queryBuilder; } - public QueryContainer Create(HousingSearchRequest request, QueryContainerDescriptor q) + public QueryContainer Create(TRequest request, QueryContainerDescriptor q) { + GetAssetListRequest assetListRequest = request as GetAssetListRequest; + if (assetListRequest == null) + throw new ArgumentNullException($"{nameof(request).ToString()} shouldn't be null."); + return _queryBuilder - .WithWildstarQuery(request.SearchText, - new List { "assetAddress.addressLine1", "assetAddress.postCode", "assetAddress.uprn" }) - .WithExactQuery(request.SearchText, - new List - { + .WithWildstarQuery(assetListRequest.SearchText, + new List { "assetAddress.addressLine1", "assetAddress.postCode", "assetAddress.uprn" }) + .WithExactQuery(assetListRequest.SearchText, + new List + { "assetAddress.addressLine1", "assetAddress.uprn", "assetAddress.postCode" - }) - .WithFilterQuery(request.AssetTypes, new List { "assetType" }) - .Build(q); + }) + .WithFilterQuery(assetListRequest.AssetTypes, new List { "assetType" }) + .Build(q); } } } diff --git a/HousingSearchApi/V1/Infrastructure/Factories/PersonQueryGenerator.cs b/HousingSearchApi/V1/Infrastructure/Factories/PersonQueryGenerator.cs index 6dad67b6..4d192e00 100644 --- a/HousingSearchApi/V1/Infrastructure/Factories/PersonQueryGenerator.cs +++ b/HousingSearchApi/V1/Infrastructure/Factories/PersonQueryGenerator.cs @@ -1,3 +1,4 @@ +using System; using HousingSearchApi.V1.Boundary.Requests; using Nest; using System.Collections.Generic; @@ -17,17 +18,20 @@ public PersonQueryGenerator(IQueryBuilder queryBuilder) _queryBuilder = queryBuilder; } - public QueryContainer Create(HousingSearchRequest request, QueryContainerDescriptor q) + + + public QueryContainer Create(TRequest request, QueryContainerDescriptor q) { + if (!(request is GetPersonListRequest personListRequest)) { - return null; + throw new ArgumentNullException($"{nameof(request).ToString()} shouldn't be null."); } _queryBuilder - .WithWildstarQuery(request.SearchText, + .WithWildstarQuery(personListRequest.SearchText, new List { "firstname", "surname" }) - .WithExactQuery(request.SearchText, + .WithExactQuery(personListRequest.SearchText, new List { "firstname", "surname" }, new ExactSearchQuerystringProcessor()); if (personListRequest.PersonType.HasValue) diff --git a/HousingSearchApi/V1/Infrastructure/Factories/QueryFactory.cs b/HousingSearchApi/V1/Infrastructure/Factories/QueryFactory.cs index 76665ebe..aa938eec 100644 --- a/HousingSearchApi/V1/Infrastructure/Factories/QueryFactory.cs +++ b/HousingSearchApi/V1/Infrastructure/Factories/QueryFactory.cs @@ -3,7 +3,6 @@ using Hackney.Shared.HousingSearch.Gateways.Models.Accounts; using Hackney.Shared.HousingSearch.Gateways.Models.Assets; using Hackney.Shared.HousingSearch.Gateways.Models.Persons; -using HousingSearchApi.V1.Boundary.Requests; using HousingSearchApi.V1.Interfaces.Factories; using Microsoft.Extensions.DependencyInjection; using QueryableTenure = Hackney.Shared.HousingSearch.Gateways.Models.Tenures.QueryableTenure; @@ -19,7 +18,7 @@ public QueryFactory(IServiceProvider serviceProvider) _serviceProvider = serviceProvider; } - public IQueryGenerator CreateQuery(HousingSearchRequest request) where T : class + public IQueryGenerator CreateQuery() where T : class { if (typeof(T) == typeof(QueryablePerson)) { diff --git a/HousingSearchApi/V1/Infrastructure/Factories/TenureQueryGenerator.cs b/HousingSearchApi/V1/Infrastructure/Factories/TenureQueryGenerator.cs index 273a8b27..6ef2ec8b 100644 --- a/HousingSearchApi/V1/Infrastructure/Factories/TenureQueryGenerator.cs +++ b/HousingSearchApi/V1/Infrastructure/Factories/TenureQueryGenerator.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Hackney.Core.ElasticSearch.Interfaces; using Hackney.Shared.HousingSearch.Gateways.Models.Tenures; @@ -16,12 +17,15 @@ public TenureQueryGenerator(IQueryBuilder queryBuilder) _queryBuilder = queryBuilder; } - public QueryContainer Create(HousingSearchRequest request, QueryContainerDescriptor q) + public QueryContainer Create(TRequest request, QueryContainerDescriptor q) { - if (string.IsNullOrWhiteSpace(request.SearchText)) return null; + if (!(request is GetTenureListRequest tenureListRequest)) + throw new ArgumentNullException($"{nameof(request).ToString()} shouldn't be null."); + + if (string.IsNullOrWhiteSpace(tenureListRequest.SearchText)) return null; return _queryBuilder - .WithWildstarQuery(request.SearchText, new List + .WithWildstarQuery(tenureListRequest.SearchText, new List { "paymentReference", "tenuredAsset.fullAddress^3", diff --git a/HousingSearchApi/V1/Infrastructure/Sorting/SortFactory.cs b/HousingSearchApi/V1/Infrastructure/Sorting/SortFactory.cs index ea48a515..3ca143dd 100644 --- a/HousingSearchApi/V1/Infrastructure/Sorting/SortFactory.cs +++ b/HousingSearchApi/V1/Infrastructure/Sorting/SortFactory.cs @@ -6,14 +6,14 @@ namespace HousingSearchApi.V1.Infrastructure.Sorting { public class SortFactory : ISortFactory { - public ISort Create(HousingSearchRequest request) where T : class + public ISort Create(TRequest request) where T : class where TRequest : class { if (typeof(T) == typeof(QueryablePerson)) { - if (string.IsNullOrEmpty(request.SortBy)) + if (string.IsNullOrEmpty(((HousingSearchRequest) (object) request).SortBy)) return new DefaultSort(); - switch (request.IsDesc) + switch (((HousingSearchRequest) (object) request).IsDesc) { case true: return (ISort) new SurnameDesc(); diff --git a/HousingSearchApi/V1/Interfaces/Factories/IQueryFactory.cs b/HousingSearchApi/V1/Interfaces/Factories/IQueryFactory.cs index 720e2a92..ff20fc21 100644 --- a/HousingSearchApi/V1/Interfaces/Factories/IQueryFactory.cs +++ b/HousingSearchApi/V1/Interfaces/Factories/IQueryFactory.cs @@ -4,6 +4,6 @@ namespace HousingSearchApi.V1.Interfaces.Factories { public interface IQueryFactory { - IQueryGenerator CreateQuery(HousingSearchRequest request) where T : class; + IQueryGenerator CreateQuery() where T : class; } } diff --git a/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs b/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs index da68d022..7aed3862 100644 --- a/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs +++ b/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs @@ -4,9 +4,7 @@ namespace HousingSearchApi.V1.Interfaces.Factories { public interface IQueryGenerator where T : class - { - QueryContainer Create(HousingSearchRequest request, - QueryContainerDescriptor q); + QueryContainer Create(TRequest request,QueryContainerDescriptor q); } } diff --git a/HousingSearchApi/V1/Interfaces/IElasticSearchWrapper.cs b/HousingSearchApi/V1/Interfaces/IElasticSearchWrapper.cs index b0f5119b..84a0c676 100644 --- a/HousingSearchApi/V1/Interfaces/IElasticSearchWrapper.cs +++ b/HousingSearchApi/V1/Interfaces/IElasticSearchWrapper.cs @@ -6,6 +6,6 @@ namespace HousingSearchApi.V1.Interfaces { public interface IElasticSearchWrapper { - Task> Search(HousingSearchRequest request) where T : class; + Task> Search(TRequest request) where T : class where TRequest : class; } } diff --git a/HousingSearchApi/V1/Interfaces/Sorting/ISortFactory.cs b/HousingSearchApi/V1/Interfaces/Sorting/ISortFactory.cs index 5bdbfdb9..6d0741ad 100644 --- a/HousingSearchApi/V1/Interfaces/Sorting/ISortFactory.cs +++ b/HousingSearchApi/V1/Interfaces/Sorting/ISortFactory.cs @@ -4,6 +4,6 @@ namespace HousingSearchApi.V1.Interfaces.Sorting { public interface ISortFactory { - ISort Create(HousingSearchRequest request) where T : class; + ISort Create(TRequest request) where T : class where TRequest : class; } } diff --git a/HousingSearchApi/V1/UseCase/GetAccountListUseCase.cs b/HousingSearchApi/V1/UseCase/GetAccountListUseCase.cs index 16707e31..96541b22 100644 --- a/HousingSearchApi/V1/UseCase/GetAccountListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/GetAccountListUseCase.cs @@ -15,7 +15,7 @@ public GetAccountListUseCase(IGetAccountGateway getAccountGateway) _getAccountGateway = getAccountGateway; } - public async Task ExecuteAsync(HousingSearchRequest getAccountListRequest) + public async Task ExecuteAsync(GetAccountListRequest getAccountListRequest) { return await _getAccountGateway.Search(getAccountListRequest).ConfigureAwait(false); } diff --git a/HousingSearchApi/V1/UseCase/GetAssetListUseCase.cs b/HousingSearchApi/V1/UseCase/GetAssetListUseCase.cs index d53cee1f..9c83fe64 100644 --- a/HousingSearchApi/V1/UseCase/GetAssetListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/GetAssetListUseCase.cs @@ -17,7 +17,7 @@ public GetAssetListUseCase(ISearchGateway searchGateway) } [LogCall] - public async Task ExecuteAsync(HousingSearchRequest housingSearchRequest) + public async Task ExecuteAsync(GetAssetListRequest housingSearchRequest) { return await _searchGateway.GetListOfAssets(housingSearchRequest).ConfigureAwait(false); } diff --git a/HousingSearchApi/V1/UseCase/GetPersonListUseCase.cs b/HousingSearchApi/V1/UseCase/GetPersonListUseCase.cs index 8bd7260e..4c8ba4dd 100644 --- a/HousingSearchApi/V1/UseCase/GetPersonListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/GetPersonListUseCase.cs @@ -16,7 +16,7 @@ public GetPersonListUseCase(ISearchGateway searchGateway) _searchGateway = searchGateway; } [LogCall] - public async Task ExecuteAsync(HousingSearchRequest housingSearchRequest) + public async Task ExecuteAsync(GetPersonListRequest housingSearchRequest) { return await _searchGateway.GetListOfPersons(housingSearchRequest).ConfigureAwait(false); } diff --git a/HousingSearchApi/V1/UseCase/GetTenureListUseCase.cs b/HousingSearchApi/V1/UseCase/GetTenureListUseCase.cs index 7a8bf6a5..3af26dbb 100644 --- a/HousingSearchApi/V1/UseCase/GetTenureListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/GetTenureListUseCase.cs @@ -17,7 +17,7 @@ public GetTenureListUseCase(ISearchGateway searchGateway) } [LogCall] - public async Task ExecuteAsync(HousingSearchRequest housingSearchRequest) + public async Task ExecuteAsync(GetTenureListRequest housingSearchRequest) { return await _searchGateway.GetListOfTenures(housingSearchRequest).ConfigureAwait(false); } diff --git a/HousingSearchApi/V1/UseCase/Interfaces/IGetAccountListUseCase.cs b/HousingSearchApi/V1/UseCase/Interfaces/IGetAccountListUseCase.cs index 520b45fd..2d6cee75 100644 --- a/HousingSearchApi/V1/UseCase/Interfaces/IGetAccountListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/Interfaces/IGetAccountListUseCase.cs @@ -6,6 +6,6 @@ namespace HousingSearchApi.V1.UseCase.Interfaces { public interface IGetAccountListUseCase { - Task ExecuteAsync(HousingSearchRequest getAccountListRequest); + Task ExecuteAsync(GetAccountListRequest getAccountListRequest); } } diff --git a/HousingSearchApi/V1/UseCase/Interfaces/IGetAssetListUseCase.cs b/HousingSearchApi/V1/UseCase/Interfaces/IGetAssetListUseCase.cs index e1246150..cd7adf5a 100644 --- a/HousingSearchApi/V1/UseCase/Interfaces/IGetAssetListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/Interfaces/IGetAssetListUseCase.cs @@ -6,6 +6,6 @@ namespace HousingSearchApi.V1.UseCase.Interfaces { public interface IGetAssetListUseCase { - Task ExecuteAsync(HousingSearchRequest getPersonListRequest); + Task ExecuteAsync(GetAssetListRequest getPersonListRequest); } } diff --git a/HousingSearchApi/V1/UseCase/Interfaces/IGetPersonListUseCase.cs b/HousingSearchApi/V1/UseCase/Interfaces/IGetPersonListUseCase.cs index 4ededf69..33072423 100644 --- a/HousingSearchApi/V1/UseCase/Interfaces/IGetPersonListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/Interfaces/IGetPersonListUseCase.cs @@ -6,6 +6,6 @@ namespace HousingSearchApi.V1.UseCase.Interfaces { public interface IGetPersonListUseCase { - Task ExecuteAsync(HousingSearchRequest housingSearchRequest); + Task ExecuteAsync(GetPersonListRequest housingSearchRequest); } } diff --git a/HousingSearchApi/V1/UseCase/Interfaces/IGetTenureListUseCase.cs b/HousingSearchApi/V1/UseCase/Interfaces/IGetTenureListUseCase.cs index 72a4405b..21244c7b 100644 --- a/HousingSearchApi/V1/UseCase/Interfaces/IGetTenureListUseCase.cs +++ b/HousingSearchApi/V1/UseCase/Interfaces/IGetTenureListUseCase.cs @@ -6,6 +6,6 @@ namespace HousingSearchApi.V1.UseCase.Interfaces { public interface IGetTenureListUseCase { - Task ExecuteAsync(HousingSearchRequest getPersonListRequest); + Task ExecuteAsync(GetTenureListRequest getPersonListRequest); } } From dbd4f62ab90efded3c42c9d2775851e6f0d4326b Mon Sep 17 00:00:00 2001 From: Hamid Jolany Date: Wed, 24 Nov 2021 13:33:48 +0330 Subject: [PATCH 2/6] Format fixing --- HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs | 2 +- HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs index 288eb7bd..e20f031d 100644 --- a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs +++ b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs @@ -24,7 +24,7 @@ public SearchPhraseTests() public void ShouldReturnNullIfRequestTypeIsUnknown(string searchText) { // Arrange + Act - var result = _sut.Create(new GetPersonListRequest{ SearchText = searchText }, new QueryContainerDescriptor()); + var result = _sut.Create(new GetPersonListRequest { SearchText = searchText }, new QueryContainerDescriptor()); // Assert result.Should().BeNull(); diff --git a/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs b/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs index 7aed3862..8bbb0dd3 100644 --- a/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs +++ b/HousingSearchApi/V1/Interfaces/Factories/IQueryGenerator.cs @@ -5,6 +5,6 @@ namespace HousingSearchApi.V1.Interfaces.Factories { public interface IQueryGenerator where T : class { - QueryContainer Create(TRequest request,QueryContainerDescriptor q); + QueryContainer Create(TRequest request, QueryContainerDescriptor q); } } From c962d3b2c1ba278a7566a87f46bd153ea1e24ee8 Mon Sep 17 00:00:00 2001 From: Hamid Jolany Date: Wed, 24 Nov 2021 18:51:49 +0330 Subject: [PATCH 3/6] Bug fixed --- HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs index e20f031d..8061d536 100644 --- a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs +++ b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs @@ -1,3 +1,4 @@ +using System; using FluentAssertions; using Hackney.Core.ElasticSearch; using Hackney.Shared.HousingSearch.Gateways.Models.Persons; @@ -24,10 +25,11 @@ public SearchPhraseTests() public void ShouldReturnNullIfRequestTypeIsUnknown(string searchText) { // Arrange + Act - var result = _sut.Create(new GetPersonListRequest { SearchText = searchText }, new QueryContainerDescriptor()); + QueryContainer Func() => _sut.Create(new GetAccountListRequest {SearchText = searchText}, new QueryContainerDescriptor()); // Assert - result.Should().BeNull(); + Exception ex = Assert.Throws((Func) Func); + } } } From e20cd823c1eec2ebe9914093859e07c02dd258d4 Mon Sep 17 00:00:00 2001 From: Hamid Jolany Date: Wed, 24 Nov 2021 19:17:40 +0330 Subject: [PATCH 4/6] Fluent validator added --- .../GetAccountListRequestValidator.cs | 18 ++++++++++++++++++ .../Validation/GetAssetListRequestValidator.cs | 18 ++++++++++++++++++ .../GetTenureListRequestValidator.cs | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 HousingSearchApi/V1/Boundary/Requests/Validation/GetAccountListRequestValidator.cs create mode 100644 HousingSearchApi/V1/Boundary/Requests/Validation/GetAssetListRequestValidator.cs create mode 100644 HousingSearchApi/V1/Boundary/Requests/Validation/GetTenureListRequestValidator.cs diff --git a/HousingSearchApi/V1/Boundary/Requests/Validation/GetAccountListRequestValidator.cs b/HousingSearchApi/V1/Boundary/Requests/Validation/GetAccountListRequestValidator.cs new file mode 100644 index 00000000..c24979b5 --- /dev/null +++ b/HousingSearchApi/V1/Boundary/Requests/Validation/GetAccountListRequestValidator.cs @@ -0,0 +1,18 @@ +using FluentValidation; +using Hackney.Core.Validation; + +namespace HousingSearchApi.V1.Boundary.Requests.Validation +{ + public class GetAccountListRequestValidator : AbstractValidator + { + public GetAccountListRequestValidator() + { + RuleFor(x => x.SearchText).NotNull() + .NotEmpty() + .MinimumLength(2) + .NotXssString(); + RuleFor(x => x.PageSize).GreaterThan(0); + RuleFor(x => x.SortBy).NotXssString(); + } + } +} diff --git a/HousingSearchApi/V1/Boundary/Requests/Validation/GetAssetListRequestValidator.cs b/HousingSearchApi/V1/Boundary/Requests/Validation/GetAssetListRequestValidator.cs new file mode 100644 index 00000000..bf83c85d --- /dev/null +++ b/HousingSearchApi/V1/Boundary/Requests/Validation/GetAssetListRequestValidator.cs @@ -0,0 +1,18 @@ +using FluentValidation; +using Hackney.Core.Validation; + +namespace HousingSearchApi.V1.Boundary.Requests.Validation +{ + public class GetAssetListRequestValidator : AbstractValidator + { + public GetAssetListRequestValidator() + { + RuleFor(x => x.SearchText).NotNull() + .NotEmpty() + .MinimumLength(2) + .NotXssString(); + RuleFor(x => x.PageSize).GreaterThan(0); + RuleFor(x => x.SortBy).NotXssString(); + } + } +} diff --git a/HousingSearchApi/V1/Boundary/Requests/Validation/GetTenureListRequestValidator.cs b/HousingSearchApi/V1/Boundary/Requests/Validation/GetTenureListRequestValidator.cs new file mode 100644 index 00000000..557f9cda --- /dev/null +++ b/HousingSearchApi/V1/Boundary/Requests/Validation/GetTenureListRequestValidator.cs @@ -0,0 +1,18 @@ +using FluentValidation; +using Hackney.Core.Validation; + +namespace HousingSearchApi.V1.Boundary.Requests.Validation +{ + public class GetTenureListRequestValidator : AbstractValidator + { + public GetTenureListRequestValidator() + { + RuleFor(x => x.SearchText).NotNull() + .NotEmpty() + .MinimumLength(2) + .NotXssString(); + RuleFor(x => x.PageSize).GreaterThan(0); + RuleFor(x => x.SortBy).NotXssString(); + } + } +} From 778e157b6d4587163f0e97858ffc3070cc3d9a83 Mon Sep 17 00:00:00 2001 From: Hamid Jolany Date: Wed, 24 Nov 2021 19:21:46 +0330 Subject: [PATCH 5/6] Format Fixing --- HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs | 2 +- .../V1/Infrastructure/Extensions/ElasticSearchExtensions.cs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs index 8061d536..022a169c 100644 --- a/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs +++ b/HousingSearchApi.Tests/V1/Helper/SearchPhraseTests.cs @@ -25,7 +25,7 @@ public SearchPhraseTests() public void ShouldReturnNullIfRequestTypeIsUnknown(string searchText) { // Arrange + Act - QueryContainer Func() => _sut.Create(new GetAccountListRequest {SearchText = searchText}, new QueryContainerDescriptor()); + QueryContainer Func() => _sut.Create(new GetAccountListRequest { SearchText = searchText }, new QueryContainerDescriptor()); // Assert Exception ex = Assert.Throws((Func) Func); diff --git a/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs b/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs index c302a7fc..dd6e290d 100644 --- a/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs +++ b/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs @@ -21,7 +21,10 @@ public static void ConfigureElasticSearch(this IServiceCollection services, ICon var pool = new SingleNodeConnectionPool(new Uri(url)); var connectionSettings = new ConnectionSettings(pool) - .PrettyJson().ThrowExceptions().DisableDirectStreaming(); + .PrettyJson() + .BasicAuthentication("elastic", "KLO@1361") + .ThrowExceptions() + .DisableDirectStreaming(); var esClient = new ElasticClient(connectionSettings); services.TryAddSingleton(esClient); From f0a477571422d35b87dd22a4a5638018a06de67f Mon Sep 17 00:00:00 2001 From: Hamid Jolany Date: Wed, 24 Nov 2021 19:22:51 +0330 Subject: [PATCH 6/6] Some local configuration removed --- .../V1/Infrastructure/Extensions/ElasticSearchExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs b/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs index dd6e290d..deb8cb2e 100644 --- a/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs +++ b/HousingSearchApi/V1/Infrastructure/Extensions/ElasticSearchExtensions.cs @@ -22,7 +22,6 @@ public static void ConfigureElasticSearch(this IServiceCollection services, ICon var connectionSettings = new ConnectionSettings(pool) .PrettyJson() - .BasicAuthentication("elastic", "KLO@1361") .ThrowExceptions() .DisableDirectStreaming(); var esClient = new ElasticClient(connectionSettings);