Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #91 from SkillsFundingAgency/CI-955_Retrieving_mul…
Browse files Browse the repository at this point in the history
…tiple_standards_in_one_request

Ci 955 retrieving multiple standards in one request
  • Loading branch information
dashton82 committed Oct 2, 2018
2 parents d5f6f16 + a874bba commit 3cbaec6
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 20 deletions.
67 changes: 47 additions & 20 deletions src/SFA.DAS.Apprenticeships.Api.Client/StandardApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using SFA.DAS.Apprenticeships.Api.Types;
Expand Down Expand Up @@ -84,25 +85,51 @@ public async Task<IEnumerable<StandardSummary>> FindAllAsync()
}
}

/// <summary>
/// Get all standards
/// GET /standards/v2
/// </summary>
/// <returns>a collection of standard summaries</returns>
public IEnumerable<StandardSummary> GetAll()
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "/standards/v2"))
{
return RequestAndDeserialise<IEnumerable<StandardSummary>>(request);
}
}
/// <summary>
/// Get all standards
/// GET /standards/v2
/// </summary>
/// <returns>a collection of standard summaries</returns>
public IEnumerable<StandardSummary> GetAll()
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "/standards/v2"))
{
return RequestAndDeserialise<IEnumerable<StandardSummary>>(request);
}
}

public async Task<IEnumerable<StandardSummary>> GetAllAsync()
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "/standards/v2"))
{
return await RequestAndDeserialiseAsync<IEnumerable<StandardSummary>>(request);
}
}
}
public async Task<IEnumerable<StandardSummary>> GetAllAsync()
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "/standards/v2"))
{
return await RequestAndDeserialiseAsync<IEnumerable<StandardSummary>>(request);
}
}

public IEnumerable<Standard> GetStandardsById(List<int> ids, int page = 1)
{
return GetStandardsById(ids.Select(id => id.ToString()).ToList(), page);
}

public IEnumerable<Standard> GetStandardsById(List<string> ids, int page = 1)
{
using (var request = new HttpRequestMessage(HttpMethod.Get, $"/standards/getlistbyids?ids={string.Join("%2C%20", ids)}&page={page}"))
{
return RequestAndDeserialise<IEnumerable<Standard>>(request);
}
}

public async Task<IEnumerable<Standard>> GetStandardsByIdAsync(List<int> ids, int page = 1)
{
return await GetStandardsByIdAsync(ids.Select(id => id.ToString()).ToList(), page);
}

public async Task<IEnumerable<Standard>> GetStandardsByIdAsync(List<string> ids, int page = 1)
{
using (var request = new HttpRequestMessage(HttpMethod.Get, $"/standards/getlistbyids?ids={string.Join("%2C%20", ids)}&page={page}"))
{
return await RequestAndDeserialiseAsync<IEnumerable<Standard>>(request);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ public Standard Get(string id)
return standard;
}

/// <summary>
/// Get a list of standards
/// </summary>
/// <param name="ids">Standard ids, coma separated</param>
/// <param name="page">Page you want to get</param>
/// <returns>a list of standard</returns>
[SwaggerOperation("GetStandardsById")]
[SwaggerResponse(HttpStatusCode.OK, "OK", typeof(List<Standard>))]
[SwaggerResponse(HttpStatusCode.NotFound)]
[Route("standards/getlistbyids")]
[ExceptionHandling]
public List<Standard> Get(string ids, int page = 1)
{
var listIds = ValidateIds(ids);

var standards = _getStandards.GetStandardsById(listIds, page);

foreach (var standard in standards)
{
standard.Uri = Resolve(standard.StandardId);
standard.Providers = ResolveProvidersUrl(standard.StandardId);
}

return standards;
}

/// <summary>
/// Do we have standards?
/// </summary>
Expand All @@ -129,6 +155,27 @@ public void Head(string id)
Get(id);
}

private List<int> ValidateIds(string ids)
{
var response = new List<int>();
foreach (var idElement in ids.Split(','))
{
int id = 0;
var validInt = int.TryParse(idElement, out id);

if (validInt)
{
response.Add(id);
}
else
{
throw new Exception($"{idElement} is not a valid id");
}
}

return response;
}

private string Resolve(string id)
{
return Url.Link("DefaultApi", new { controller = "standards", id = id });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public interface IGetStandards
IEnumerable<StandardSummary> GetAllStandards();

Standard GetStandardById(string id);

List<Standard> GetStandardsById(List<int> ids, int page);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public sealed class StandardRepository : IGetStandards
private readonly IQueryHelper _queryHelper;
private readonly IGetIfaStandardsUrlService _getIfaStandardUrlService;

private const int Take = 20;

public StandardRepository(
IElasticsearchCustomClient elasticsearchCustomClient,
IConfigurationSettings applicationSettings,
Expand Down Expand Up @@ -78,6 +80,45 @@ public Standard GetStandardById(string id)
return response;
}

public List<Standard> GetStandardsById(List<int> ids, int page)
{
var skip = GetSkipAmount(page);
ids.Sort();
var elements = ids.Skip(skip).Take(Take);
var results = _elasticsearchCustomClient.Search<StandardSearchResultsItem>(
s =>
s.Index(_applicationSettings.ApprenticeshipIndexAlias)
.Type(Types.Parse("standarddocument"))
.Take(Take)
.Query(q => q
.Terms(t => t
.Field(fi => fi.StandardId).Terms(elements))));

var documents = results.Documents.Any() ? results.Documents : null;

if (documents == null)
{
return null;
}

var response = new List<Standard>();

foreach (var standardSearchResultsItem in documents)
{
var standard = _standardMapping.MapToStandard(standardSearchResultsItem);
standard.StandardPageUri = _getIfaStandardUrlService.GetStandardUrl(standard.StandardId);

response.Add(standard);
}

return response;
}

private int GetSkipAmount(int page)
{
return (page - 1) * Take;
}

private ISearchRequest GetAllStandardsSeachDescriptor(int take)
{
if (Is<Elk5Feature>.Enabled)
Expand Down

0 comments on commit 3cbaec6

Please sign in to comment.