diff --git a/OpenAPI/LearningHub.Nhs.OpenApi.Models/Configuration/FindwiseCollectionIdSettings.cs b/OpenAPI/LearningHub.Nhs.OpenApi.Models/Configuration/FindwiseCollectionIdSettings.cs index 3e97bbb53..ece9a6012 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi.Models/Configuration/FindwiseCollectionIdSettings.cs +++ b/OpenAPI/LearningHub.Nhs.OpenApi.Models/Configuration/FindwiseCollectionIdSettings.cs @@ -14,5 +14,10 @@ public class FindwiseCollectionIdSettings /// Gets or sets the catalogue collection id. /// public string Catalogue { get; set; } = null!; + + /// + /// Gets or sets the AutoSuggestion collection id. + /// + public string AutoSuggestion { get; set; } = null!; } } diff --git a/OpenAPI/LearningHub.Nhs.OpenApi.Services.Interface/Services/ISearchService.cs b/OpenAPI/LearningHub.Nhs.OpenApi.Services.Interface/Services/ISearchService.cs index 406b2442f..8061f94da 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi.Services.Interface/Services/ISearchService.cs +++ b/OpenAPI/LearningHub.Nhs.OpenApi.Services.Interface/Services/ISearchService.cs @@ -24,6 +24,13 @@ public interface ISearchService /// The . Task GetAllCatalogueSearchResultsAsync(AllCatalogueSearchRequestModel catalogSearchRequestModel); + /// + /// The Get Auto suggestion Results Async method. + /// + /// The term. + /// The . + Task GetAutoSuggestionResultsAsync(string term); + /// /// The remove resource from search method. /// diff --git a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/SearchService.cs b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/SearchService.cs index 12cd36e4e..549abc79d 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/SearchService.cs +++ b/OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/SearchService.cs @@ -168,6 +168,52 @@ public async Task GetAllCatalogueSearchResultsAsy } } + /// + /// The Get Auto suggestion Results Async method. + /// + /// The term. + /// The . + public async Task 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(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; diff --git a/OpenAPI/LearningHub.Nhs.OpenApi/Controllers/SearchController.cs b/OpenAPI/LearningHub.Nhs.OpenApi/Controllers/SearchController.cs index 840732449..bd8a17605 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi/Controllers/SearchController.cs +++ b/OpenAPI/LearningHub.Nhs.OpenApi/Controllers/SearchController.cs @@ -63,6 +63,50 @@ public async Task GetAllCatalogueSearchResult(AllCatalogueSearchR return this.Ok(vm); } + /// + /// Get AutoSuggestionResults. + /// + /// The term. + /// The . + [HttpGet] + [Route("GetAutoSuggestionResult/{term}")] + public async Task GetAutoSuggestionResults(string term) + { + var autosuggestionViewModel = await this.GetAutoSuggestions(term); + return this.Ok(autosuggestionViewModel); + } + + /// + /// Get AutoSuggestion Results. + /// + /// term. + /// The . + private async Task 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; + } + /// /// Get All catalogue search results. /// diff --git a/OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json b/OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json index 2b393603f..13658ec61 100644 --- a/OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json +++ b/OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json @@ -50,7 +50,9 @@ "FindWise": { "SearchBaseUrl": "", "CollectionIds": { - "Resource": "hee-test" + "Resource": "hee-test", + "Catalogue": "", + "AutoSuggestion": "" }, "SearchEndpointPath": "rest/apps/HEE/searchers/", "Token": "",