Skip to content
This repository was archived by the owner on Jul 9, 2021. It is now read-only.
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
19 changes: 18 additions & 1 deletion src/SFA.DAS.Apprenticeships.Api.Client/StandardApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using SFA.DAS.Apprenticeships.Api.Types;
using SFA.DAS.Apprenticeships.Api.Types.Providers;

namespace SFA.DAS.Apprenticeships.Api.Client
{
Expand Down Expand Up @@ -131,5 +132,21 @@ public async Task<IEnumerable<Standard>> GetStandardsByIdAsync(List<string> ids,
return await RequestAndDeserialiseAsync<IEnumerable<Standard>>(request);
}
}
}

public async Task<IEnumerable<StandardProvider>> GetProvidersByStandardAndLocationAsync(int standardCode, string postCode, int page = 1)
{
using (var request = new HttpRequestMessage(HttpMethod.Get, $"/standards/{standardCode}/providers/{postCode}?page={page}"))
{
return await RequestAndDeserialiseAsync<IEnumerable<StandardProvider>>(request);
}
}

public IEnumerable<StandardProvider> GetProvidersByStandardAndLocation(int standardCode, string postCode,int page = 1)
{
using (var request = new HttpRequestMessage(HttpMethod.Get, $"/standards/{standardCode}/providers/{postCode}?page={page}"))
{
return RequestAndDeserialise<IEnumerable<StandardProvider>>(request);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Sfa.Das.ApprenticeshipInfoService.Api.Controllers
using System.Threading.Tasks;

namespace Sfa.Das.ApprenticeshipInfoService.Api.Controllers
{
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -355,6 +357,22 @@ public ApprenticeshipDetails GetFrameworkProviderDetails(string frameworkId, int
throw new HttpResponseException(HttpStatusCode.NotFound);
}

/// <summary>
/// Get list of providers by Standard and Postcode
/// </summary>
/// <param name="standardCode">standard code</param>
/// <param name="postCode">postcode to give distances from</param>
/// <param name="page">page number to return</param>
/// <returns>A list of providers and distance from postcode for that standard</returns>
[SwaggerResponse(HttpStatusCode.OK, "OK", typeof(IEnumerable<StandardProviderSearchResultsItemResponse>))]
[SwaggerResponse(HttpStatusCode.InternalServerError)]
[Route("standards/{standardCode}/providers/{postCode}", Name = "GetProvidersByStandardWithLocation")]
[ExceptionHandling]
public async Task<IEnumerable<StandardProviderSearchResultsItemResponse>> GetActiveApprenticeshipTrainingByProvider(int standardCode, string postCode, int page = 1)
{
return await _getProviders.GetProvidersByPostCodeAndStandard(postCode, standardCode, page);
}

private string Resolve(long ukprn)
{
return Url.Link("DefaultApi", new { controller = "providers", id = ukprn });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ public interface IConfigurationSettings
int PageSizeApprenticeshipSummary { get; }

string IfaStandardApiUrl { get; }
Uri PostCodeUrl { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Sfa.Das.ApprenticeshipInfoService.Core.Models
{
public class PostCodeResponse
{
public int Status { get; set; }
public PostCodeResult Result { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Sfa.Das.ApprenticeshipInfoService.Core.Models
{
public class PostCodeResult
{
public double? Longitude { get; set; }
public double? Latitude { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sfa.Das.ApprenticeshipInfoService.Core.Models;
using System.Threading.Tasks;
using Sfa.Das.ApprenticeshipInfoService.Core.Models;
using SFA.DAS.Apprenticeships.Api.Types.Providers;

namespace Sfa.Das.ApprenticeshipInfoService.Core.Services
Expand Down Expand Up @@ -26,5 +27,6 @@ public interface IGetProviders
IEnumerable<ProviderStandard> GetStandardsByProviderUkprn(long ukprn);

ApprenticeshipTrainingSummary GetActiveApprenticeshipTrainingByProvider(long ukprn, int page);
Task<IEnumerable<StandardProviderSearchResultsItemResponse>> GetProvidersByPostCodeAndStandard(string postCode, int standardId, int page);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sfa.Das.ApprenticeshipInfoService.Core.Models;

namespace Sfa.Das.ApprenticeshipInfoService.Core.Services
{
public interface IPostCodeIoLocator
{
Task<PostCodeResponse> GetLatLongFromPostcode(string postCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
<Compile Include="Models\ApprenticeshipProduct.cs" />
<Compile Include="Models\ApprenticeshipBasic.cs" />
<Compile Include="Models\ApprenticeshipSearchResults.cs" />
<Compile Include="Models\PostCodeResponse.cs" />
<Compile Include="Models\PostCodeResult.cs" />
<Compile Include="Models\ProviderSearchResultsItem.cs" />
<Compile Include="Models\DetailProviderResponse.cs" />
<Compile Include="Models\ContactInformation.cs" />
Expand All @@ -87,6 +89,7 @@
<Compile Include="Models\FrameworkSearchResultsItem.cs" />
<Compile Include="Models\IApprenticeshipProduct.cs" />
<Compile Include="Services\IApprenticeshipProviderRepository.cs" />
<Compile Include="Services\IPostCodeIoLocator.cs" />
<Compile Include="Services\IProviderSearchService.cs" />
<Compile Include="Services\IGetAssessmentOrgs.cs" />
<Compile Include="Services\IGetProviders.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Reflection;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.Models;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.PostCodeIo;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.Services;
using SFA.DAS.NLog.Logger;

Expand Down Expand Up @@ -44,6 +45,8 @@ public InfrastructureRegistry()
For<IFundingCapCalculator>().Use<FundingCapCalculator>();
For<IPaginationHelper>().Use<PaginationHelper>();
For<IGetIfaStandardsUrlService>().Use<GetIfaStandardsUrlService>();
For<IHttpClient>().Use<HttpClient>();
For<IPostCodeIoLocator>().Use<PostCodeIoLocator>();
}

private IDictionary<string, object> GetProperties()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Sfa.Das.ApprenticeshipInfoService.Core.Helpers;
using System.Net;
using System.Threading.Tasks;
using Sfa.Das.ApprenticeshipInfoService.Core.Helpers;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.Models;
using SFA.DAS.Apprenticeships.Api.Types;

Expand Down Expand Up @@ -29,16 +31,16 @@ public sealed class ProviderRepository : IGetProviders
private readonly IQueryHelper _queryHelper;
private readonly IActiveApprenticeshipChecker _activeApprenticeshipChecker;
private readonly IPaginationHelper _paginationHelper;
private readonly IPostCodeIoLocator _postCodeIoLocator;

public ProviderRepository(
IElasticsearchCustomClient elasticsearchCustomClient,
public ProviderRepository(IElasticsearchCustomClient elasticsearchCustomClient,
ILog applicationLogger,
IConfigurationSettings applicationSettings,
IProviderLocationSearchProvider providerLocationSearchProvider,
IProviderMapping providerMapping,
IQueryHelper queryHelper,
IActiveApprenticeshipChecker activeApprenticeshipChecker,
IPaginationHelper paginationHelper)
IPaginationHelper paginationHelper, IPostCodeIoLocator postCodeIoLocator)
{
_elasticsearchCustomClient = elasticsearchCustomClient;
_applicationLogger = applicationLogger;
Expand All @@ -48,6 +50,7 @@ public ProviderRepository(
_queryHelper = queryHelper;
_activeApprenticeshipChecker = activeApprenticeshipChecker;
_paginationHelper = paginationHelper;
_postCodeIoLocator = postCodeIoLocator;
}

public IEnumerable<ProviderSummary> GetAllProviders()
Expand Down Expand Up @@ -235,7 +238,7 @@ public List<StandardProviderSearchResultsItemResponse> GetByStandardIdAndLocatio

return providers.Select(provider => _providerMapping.MapToStandardProviderResponse(provider)).ToList();
}

public List<FrameworkProviderSearchResultsItemResponse> GetByFrameworkIdAndLocation(int id, double lat, double lon, int page)
{
var coordinates = new Coordinate
Expand Down Expand Up @@ -309,6 +312,26 @@ public IEnumerable<FrameworkProviderSearchResultsItem> GetProvidersByFrameworkId
return results.Documents;
}

public async Task<IEnumerable<StandardProviderSearchResultsItemResponse>> GetProvidersByPostCodeAndStandard(string postCode, int standardId, int page)
{
var postCodeResponse = await _postCodeIoLocator.GetLatLongFromPostcode(postCode);

if (postCodeResponse.Status == 404)
{
return new List<StandardProviderSearchResultsItemResponse>();
}

var coordinates = new Coordinate
{
Lat = postCodeResponse.Result.Latitude.GetValueOrDefault(0),
Lon = postCodeResponse.Result.Longitude.GetValueOrDefault(0)
};

var providers = _providerLocationSearchProvider.SearchStandardProviders(standardId, coordinates, page);

return providers.Select(provider => _providerMapping.MapToStandardProviderResponse(provider)).ToList();
}

public ApprenticeshipTrainingSummary GetActiveApprenticeshipTrainingByProvider(long ukprn, int page)
{
var apprenticeshipTrainingSummary = new ApprenticeshipTrainingSummary { Ukprn = ukprn };
Expand Down Expand Up @@ -401,5 +424,5 @@ private IEnumerable<ApprenticeshipTraining> GetActiveStandardsForUkprn(long ukpr
})
.ToList();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Sfa.Das.ApprenticeshipInfoService.Core.Configuration;
using Sfa.Das.ApprenticeshipInfoService.Core.Models;
using Sfa.Das.ApprenticeshipInfoService.Core.Services;
using Sfa.Das.ApprenticeshipInfoService.Infrastructure.Services;

namespace Sfa.Das.ApprenticeshipInfoService.Infrastructure.PostCodeIo
{
public class PostCodeIoLocator : IPostCodeIoLocator
{
private readonly IConfigurationSettings _configurationSettings;
private readonly IHttpClient _httpClient;

public PostCodeIoLocator(IConfigurationSettings configurationSettings, IHttpClient httpClient)
{
_configurationSettings = configurationSettings;
_httpClient = httpClient;
}

public async Task<PostCodeResponse> GetLatLongFromPostcode(string postCode)
{
var uri = _configurationSettings.PostCodeUrl;

var response = await _httpClient.GetAsync(new Uri(uri, postCode));

return JsonConvert.DeserializeObject<PostCodeResponse>(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;

namespace Sfa.Das.ApprenticeshipInfoService.Infrastructure.Services
{
public class HttpClient : IHttpClient
{
public async Task<string> GetAsync(Uri uri)
{
using (var client = new System.Net.Http.HttpClient())
{
var response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();

return await response.Content.ReadAsStringAsync();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Threading.Tasks;

namespace Sfa.Das.ApprenticeshipInfoService.Infrastructure.Services
{
public interface IHttpClient
{
Task<string> GetAsync(Uri uri);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Sfa.Das.ApprenticeshipInfoService.Infrastructure.Settings
using Nest;

namespace Sfa.Das.ApprenticeshipInfoService.Infrastructure.Settings
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -30,6 +32,8 @@ public sealed class ApplicationSettings : IConfigurationSettings
public string ElasticsearchPassword => CloudConfigurationManager.GetSetting("ElasticsearchPassword");
public string IfaStandardApiUrl => ConfigurationManager.AppSettings["IFAStandardsApiUrl"];

public Uri PostCodeUrl => new Uri("https://postcodes.io/postcodes/");

public int PageSizeApprenticeshipSummary => int.Parse(
ConfigurationManager.AppSettings["PageSizeApprenticeshipSummary"]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,15 @@
<Compile Include="Models\GaRouteTrackingArg.cs" />
<Compile Include="Models\ProviderFrameworkDto.cs" />
<Compile Include="Models\ProviderStandardDto.cs" />
<Compile Include="PostCodeIo\PostCodeIoLocator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\AnalyticsService.cs" />
<Compile Include="Services\GetIFAStandardsUrlService.cs" />
<Compile Include="Services\HttpClient.cs" />
<Compile Include="Services\IAnalyticsService.cs" />
<Compile Include="Models\IfaApiStandard.cs" />
<Compile Include="Services\IGetIfaStandardsUrlService.cs" />
<Compile Include="Services\IHttpClient.cs" />
<Compile Include="Settings\ApplicationSettings.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading