Skip to content

Commit 9a97957

Browse files
committed
Merge remote-tracking branch 'origin/Develop/Features/TD-5118-P1-P2-P3' into Develop/Features/TD-5118-P1-P2-P3
2 parents e59c9d3 + 0efabf8 commit 9a97957

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

OpenAPI/LearningHub.Nhs.OpenApi.Models/Configuration/FindwiseCollectionIdSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ public class FindwiseCollectionIdSettings
1414
/// Gets or sets the catalogue collection id.
1515
/// </summary>
1616
public string Catalogue { get; set; } = null!;
17+
18+
/// <summary>
19+
/// Gets or sets the AutoSuggestion collection id.
20+
/// </summary>
21+
public string AutoSuggestion { get; set; } = null!;
1722
}
1823
}

OpenAPI/LearningHub.Nhs.OpenApi.Services.Interface/Services/ISearchService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public interface ISearchService
2424
/// <returns>The <see cref="Task"/>.</returns>
2525
Task<SearchAllCatalogueResultModel> GetAllCatalogueSearchResultsAsync(AllCatalogueSearchRequestModel catalogSearchRequestModel);
2626

27+
/// <summary>
28+
/// The Get Auto suggestion Results Async method.
29+
/// </summary>
30+
/// <param name="term">The term.</param>
31+
/// <returns>The <see cref="Task"/>.</returns>
32+
Task<AutoSuggestionModel> GetAutoSuggestionResultsAsync(string term);
33+
2734
/// <summary>
2835
/// The remove resource from search method.
2936
/// </summary>

OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/SearchService.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,52 @@ public async Task<SearchAllCatalogueResultModel> GetAllCatalogueSearchResultsAsy
167167
}
168168
}
169169

170+
/// <summary>
171+
/// The Get Auto suggestion Results Async method.
172+
/// </summary>
173+
/// <param name="term">The term.</param>
174+
/// <returns>The <see cref="Task"/>.</returns>
175+
public async Task<AutoSuggestionModel> GetAutoSuggestionResultsAsync(string term)
176+
{
177+
var viewmodel = new AutoSuggestionModel();
178+
179+
try
180+
{
181+
var client = await this.findwiseClient.GetClient(this.findwiseConfig.SearchBaseUrl);
182+
var request = string.Format(
183+
this.findwiseConfig.SearchEndpointPath + "?q={1}&token={2}",
184+
this.findwiseConfig.CollectionIds.AutoSuggestion,
185+
this.EncodeSearchText(term),
186+
this.findwiseConfig.Token);
187+
188+
189+
var response = await client.GetAsync(request).ConfigureAwait(false);
190+
191+
if (response.IsSuccessStatusCode)
192+
{
193+
var result = response.Content.ReadAsStringAsync().Result;
194+
viewmodel = JsonConvert.DeserializeObject<AutoSuggestionModel>(result);
195+
}
196+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
197+
{
198+
this.logger.LogError($"Get Auto Suggetion Result failed in FindWise, HTTP Status Code:{response.StatusCode}");
199+
throw new Exception("AccessDenied to FindWise Server");
200+
}
201+
else
202+
{
203+
var error = response.Content.ReadAsStringAsync().Result.ToString();
204+
this.logger.LogError($"Get Auto Suggetion Result failed in FindWise, HTTP Status Code:{response.StatusCode}, Error Message:{error}");
205+
throw new Exception("Error with FindWise Server");
206+
}
207+
208+
return viewmodel;
209+
}
210+
catch (Exception)
211+
{
212+
throw;
213+
}
214+
}
215+
170216
private string EncodeSearchText(string searchText)
171217
{
172218
string specialSearchCharacters = this.findwiseConfig.SpecialSearchCharacters;

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/SearchController.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,50 @@ public async Task<IActionResult> GetAllCatalogueSearchResult(AllCatalogueSearchR
6363
return this.Ok(vm);
6464
}
6565

66+
/// <summary>
67+
/// Get AutoSuggestionResults.
68+
/// </summary>
69+
/// <param name="term">The term.</param>
70+
/// <returns>The <see cref="Task"/>.</returns>
71+
[HttpGet]
72+
[Route("GetAutoSuggestionResult/{term}")]
73+
public async Task<IActionResult> GetAutoSuggestionResults(string term)
74+
{
75+
var autosuggestionViewModel = await this.GetAutoSuggestions(term);
76+
return this.Ok(autosuggestionViewModel);
77+
}
78+
79+
/// <summary>
80+
/// Get AutoSuggestion Results.
81+
/// </summary>
82+
/// <param name="term">term.</param>
83+
/// <returns>The <see cref="Task"/>.</returns>
84+
private async Task<AutoSuggestionModel> GetAutoSuggestions(string term)
85+
{
86+
var autosuggestionModel = await this.searchService.GetAutoSuggestionResultsAsync(term);
87+
if (autosuggestionModel != null)
88+
{
89+
var documents = autosuggestionModel.CatalogueDocument.CatalogueDocumentList;
90+
var documentIds = documents.Select(x => int.Parse(x.Id)).ToList();
91+
var catalogues = this.catalogueService.GetCataloguesByNodeId(documentIds);
92+
93+
foreach (var document in documents)
94+
{
95+
var catalogue = catalogues.SingleOrDefault(x => x.NodeId == int.Parse(document.Id));
96+
if (catalogue == null)
97+
{
98+
continue;
99+
}
100+
101+
document.Url = catalogue.Url;
102+
}
103+
104+
autosuggestionModel.CatalogueDocument.CatalogueDocumentList = documents;
105+
}
106+
107+
return autosuggestionModel;
108+
}
109+
66110
/// <summary>
67111
/// Get All catalogue search results.
68112
/// </summary>

OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
"FindWise": {
5151
"SearchBaseUrl": "<configure-in-env-var>",
5252
"CollectionIds": {
53-
"Resource": "hee-test"
53+
"Resource": "hee-test",
54+
"Catalogue": "",
55+
"AutoSuggestion": ""
5456
},
5557
"SearchEndpointPath": "rest/apps/HEE/searchers/",
5658
"Token": "<configure-in-env-var>",

0 commit comments

Comments
 (0)