Skip to content
Merged
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 @@ -14,5 +14,10 @@ public class FindwiseCollectionIdSettings
/// Gets or sets the catalogue collection id.
/// </summary>
public string Catalogue { get; set; } = null!;

/// <summary>
/// Gets or sets the AutoSuggestion collection id.
/// </summary>
public string AutoSuggestion { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public interface ISearchService
/// <returns>The <see cref="Task"/>.</returns>
Task<SearchAllCatalogueResultModel> GetAllCatalogueSearchResultsAsync(AllCatalogueSearchRequestModel catalogSearchRequestModel);

/// <summary>
/// The Get Auto suggestion Results Async method.
/// </summary>
/// <param name="term">The term.</param>
/// <returns>The <see cref="Task"/>.</returns>
Task<AutoSuggestionModel> GetAutoSuggestionResultsAsync(string term);

/// <summary>
/// The remove resource from search method.
/// </summary>
Expand Down
46 changes: 46 additions & 0 deletions OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,52 @@ public async Task<SearchAllCatalogueResultModel> GetAllCatalogueSearchResultsAsy
}
}

/// <summary>
/// The Get Auto suggestion Results Async method.
/// </summary>
/// <param name="term">The term.</param>
/// <returns>The <see cref="Task"/>.</returns>
public async Task<AutoSuggestionModel> GetAutoSuggestionResultsAsync(string term)
{
var viewmodel = new AutoSuggestionModel();

try
{
var client = await this.findwiseClient.GetClient(this.findwiseConfig.SearchBaseUrl);
var request = string.Format(
this.findwiseConfig.SearchEndpointPath + "?q={1}&token={2}",
this.findwiseConfig.CollectionIds.AutoSuggestion,
this.EncodeSearchText(term),
this.findwiseConfig.Token);


var response = await client.GetAsync(request).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
viewmodel = JsonConvert.DeserializeObject<AutoSuggestionModel>(result);
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
this.logger.LogError($"Get Auto Suggetion Result failed in FindWise, HTTP Status Code:{response.StatusCode}");
throw new Exception("AccessDenied to FindWise Server");
}
else
{
var error = response.Content.ReadAsStringAsync().Result.ToString();
this.logger.LogError($"Get Auto Suggetion Result failed in FindWise, HTTP Status Code:{response.StatusCode}, Error Message:{error}");
throw new Exception("Error with FindWise Server");
}

return viewmodel;
}
catch (Exception)
{
throw;
}
}

private string EncodeSearchText(string searchText)
{
string specialSearchCharacters = this.findwiseConfig.SpecialSearchCharacters;
Expand Down
44 changes: 44 additions & 0 deletions OpenAPI/LearningHub.Nhs.OpenApi/Controllers/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,50 @@ public async Task<IActionResult> GetAllCatalogueSearchResult(AllCatalogueSearchR
return this.Ok(vm);
}

/// <summary>
/// Get AutoSuggestionResults.
/// </summary>
/// <param name="term">The term.</param>
/// <returns>The <see cref="Task"/>.</returns>
[HttpGet]
[Route("GetAutoSuggestionResult/{term}")]
public async Task<IActionResult> GetAutoSuggestionResults(string term)
{
var autosuggestionViewModel = await this.GetAutoSuggestions(term);
return this.Ok(autosuggestionViewModel);
}

/// <summary>
/// Get AutoSuggestion Results.
/// </summary>
/// <param name="term">term.</param>
/// <returns>The <see cref="Task"/>.</returns>
private async Task<AutoSuggestionModel> GetAutoSuggestions(string term)
{
var autosuggestionModel = await this.searchService.GetAutoSuggestionResultsAsync(term);
if (autosuggestionModel != null)
{
var documents = autosuggestionModel.CatalogueDocument.CatalogueDocumentList;
var documentIds = documents.Select(x => int.Parse(x.Id)).ToList();
var catalogues = this.catalogueService.GetCataloguesByNodeId(documentIds);

foreach (var document in documents)
{
var catalogue = catalogues.SingleOrDefault(x => x.NodeId == int.Parse(document.Id));
if (catalogue == null)
{
continue;
}

document.Url = catalogue.Url;
}

autosuggestionModel.CatalogueDocument.CatalogueDocumentList = documents;
}

return autosuggestionModel;
}

/// <summary>
/// Get All catalogue search results.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
"FindWise": {
"SearchBaseUrl": "<configure-in-env-var>",
"CollectionIds": {
"Resource": "hee-test"
"Resource": "hee-test",
"Catalogue": "",
"AutoSuggestion": ""
},
"SearchEndpointPath": "rest/apps/HEE/searchers/",
"Token": "<configure-in-env-var>",
Expand Down
Loading