Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new endpoint for tenures batch search #107

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace HousingSearchApi.Tests.V1.Controllers
public class GetTenureListControllerTests
{
private readonly Mock<IGetTenureListUseCase> _mockGetTenureListUseCase;
private readonly Mock<IGetTenureListByPrnListUseCase> _mockGetTenureListByPrnListUseCase;
private readonly GetTenureListController _classUnderTest;


Expand All @@ -20,7 +21,8 @@ public GetTenureListControllerTests()
new LogCallAspectFixture().RunBeforeTests();

_mockGetTenureListUseCase = new Mock<IGetTenureListUseCase>();
_classUnderTest = new GetTenureListController(_mockGetTenureListUseCase.Object);
_mockGetTenureListByPrnListUseCase = new Mock<IGetTenureListByPrnListUseCase>();
_classUnderTest = new GetTenureListController(_mockGetTenureListUseCase.Object, _mockGetTenureListByPrnListUseCase.Object);
}

[Fact]
Expand Down
1 change: 1 addition & 0 deletions HousingSearchApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ private static void RegisterUseCases(IServiceCollection services)
services.AddScoped<IGetPersonListUseCase, GetPersonListUseCase>();
services.AddScoped<IGetAccountListUseCase, GetAccountListUseCase>();
services.AddScoped<IGetTenureListUseCase, GetTenureListUseCase>();
services.AddScoped<IGetTenureListByPrnListUseCase, GetTenureListUseByPrnListCase>();
services.AddScoped<IElasticSearchWrapper, ElasticSearchWrapper>();
services.AddScoped<IPagingHelper, PagingHelper>();
services.AddScoped<ISortFactory, SortFactory>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace HousingSearchApi.V1.Boundary.Requests
{
public class GetTenureListByPrnListRequest : HousingSearchRequest
{
[FromQuery(Name = "prnList")]
[Required, MinLength(1)]
public List<string> PrnList { get; set; }
}
}
27 changes: 26 additions & 1 deletion HousingSearchApi/V1/Controllers/GetTenureListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Net;
using System.Threading.Tasks;

namespace HousingSearchApi.V1.Controllers
Expand All @@ -18,10 +19,12 @@ namespace HousingSearchApi.V1.Controllers
public class GetTenureListController : BaseController
{
private readonly IGetTenureListUseCase _getTenureListUseCase;
private readonly IGetTenureListByPrnListUseCase _getTenureListByPrnListUseCase;

public GetTenureListController(IGetTenureListUseCase getTenureListUseCase)
public GetTenureListController(IGetTenureListUseCase getTenureListUseCase, IGetTenureListByPrnListUseCase getTenureListByPrnListUseCase)
{
_getTenureListUseCase = getTenureListUseCase;
_getTenureListByPrnListUseCase = getTenureListByPrnListUseCase;
}

[ProducesResponseType(typeof(APIResponse<GetTenureListResponse>), 200)]
Expand All @@ -45,5 +48,27 @@ public async Task<IActionResult> GetTenureList([FromQuery] GetTenureListRequest
return new BadRequestObjectResult(e.Message);
}
}

[ProducesResponseType(typeof(APIResponse<GetTenureListResponse>), 200)]
[ProducesResponseType(typeof(APIResponse<NotFoundException>), 404)]
[ProducesResponseType(typeof(APIResponse<BadRequestException>), 400)]
[HttpGet("byPrnList"), MapToApiVersion("1")]
[LogCall(LogLevel.Information)]
public async Task<IActionResult> GetTenureList([FromQuery] GetTenureListByPrnListRequest request)
{
try
{
var tenuresSearchResult = await _getTenureListByPrnListUseCase.ExecuteAsync(request).ConfigureAwait(false);
var apiResponse = new APIResponse<GetTenureListResponse>(tenuresSearchResult);
apiResponse.Total = tenuresSearchResult.Total();

return new OkObjectResult(apiResponse);
}
catch (Exception e)
{
LambdaLogger.Log(e.Message + e.StackTrace);
return StatusCode((int) HttpStatusCode.InternalServerError, e.Message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface ISearchGateway
{
Task<GetPersonListResponse> GetListOfPersons(GetPersonListRequest query);
Task<GetTenureListResponse> GetListOfTenures(GetTenureListRequest query);
Task<GetTenureListResponse> GetListOfTenures(GetTenureListByPrnListRequest query);
Task<GetAssetListResponse> GetListOfAssets(GetAssetListRequest query);
Task<GetAllAssetListResponse> GetListOfAssetsSets(GetAllAssetListRequest query);
Task<GetAccountListResponse> GetListOfAccounts(GetAccountListRequest query);
Expand Down
15 changes: 15 additions & 0 deletions HousingSearchApi/V1/Gateways/SearchGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public async Task<GetTenureListResponse> GetListOfTenures(GetTenureListRequest q
return tenureListResponse;
}

[LogCall]
public async Task<GetTenureListResponse> GetListOfTenures(GetTenureListByPrnListRequest query)
{
var searchResponse = await _elasticSearchWrapper.Search<QueryableTenure, GetTenureListByPrnListRequest>(query).ConfigureAwait(false);
var tenureListResponse = new GetTenureListResponse();

tenureListResponse.Tenures.AddRange(searchResponse.Documents.Select(queryableTenure =>
queryableTenure.Create())
);

tenureListResponse.SetTotal(searchResponse.Total);

return tenureListResponse;
}

[LogCall]
public async Task<GetAssetListResponse> GetListOfAssets(GetAssetListRequest query)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using Hackney.Core.ElasticSearch.Interfaces;
using Hackney.Shared.HousingSearch.Gateways.Models.Tenures;
using HousingSearchApi.V1.Boundary.Requests;
using HousingSearchApi.V1.Interfaces.Factories;
using Nest;
using System;
using System.Collections.Generic;

namespace HousingSearchApi.V1.Infrastructure.Factories
{
Expand All @@ -19,19 +19,33 @@ public TenureQueryGenerator(IQueryBuilder<QueryableTenure> queryBuilder)

public QueryContainer Create<TRequest>(TRequest request, QueryContainerDescriptor<QueryableTenure> q)
{
if (!(request is GetTenureListRequest tenureListRequest))
throw new ArgumentNullException($"{nameof(request).ToString()} shouldn't be null.");
switch (request)
{
case GetTenureListRequest tenureListRequest:
if (string.IsNullOrWhiteSpace(tenureListRequest.SearchText))
{
return null;
}

return _queryBuilder
.WithWildstarQuery(tenureListRequest.SearchText, new List<string>
{
"paymentReference",
"tenuredAsset.fullAddress^3",
"householdMembers",
"householdMembers.fullName^3"
}).Build(q);

if (string.IsNullOrWhiteSpace(tenureListRequest.SearchText)) return null;
case GetTenureListByPrnListRequest tenureListRequestByPrnList:
return q.Terms(c => c
.Name("named_query")
.Field("paymentReference")
.Boost(1.1)
.Terms(tenureListRequestByPrnList.PrnList));

return _queryBuilder
.WithWildstarQuery(tenureListRequest.SearchText, new List<string>
{
"paymentReference",
"tenuredAsset.fullAddress^3",
"householdMembers",
"householdMembers.fullName^3"
}).Build(q);
default:
throw new ArgumentNullException(nameof(request));
}
}
}
}
26 changes: 26 additions & 0 deletions HousingSearchApi/V1/UseCase/GetTenureListUseByPrnListCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Hackney.Core.Logging;
using HousingSearchApi.V1.Boundary.Requests;
using HousingSearchApi.V1.Boundary.Responses;
using HousingSearchApi.V1.Gateways.Interfaces;
using HousingSearchApi.V1.UseCase.Interfaces;
using System.Threading.Tasks;

namespace HousingSearchApi.V1.UseCase
{
public class GetTenureListUseByPrnListCase : IGetTenureListByPrnListUseCase
{
private readonly ISearchGateway _searchGateway;

public GetTenureListUseByPrnListCase(ISearchGateway searchGateway)
{
_searchGateway = searchGateway;
}

[LogCall]
public async Task<GetTenureListResponse> ExecuteAsync(GetTenureListByPrnListRequest housingSearchRequest)
{
return await _searchGateway.GetListOfTenures(housingSearchRequest).ConfigureAwait(false);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using HousingSearchApi.V1.Boundary.Requests;
using HousingSearchApi.V1.Boundary.Responses;
using System.Threading.Tasks;

namespace HousingSearchApi.V1.UseCase.Interfaces
{
public interface IGetTenureListByPrnListUseCase
{
Task<GetTenureListResponse> ExecuteAsync(GetTenureListByPrnListRequest getPersonListRequest);
}
}