Skip to content

Commit 0efabf8

Browse files
authored
Merge pull request #1005 from TechnologyEnhancedLearning/Develop/Features/TD-5384
Added Autosuggestion
2 parents b61a5f8 + caf02c2 commit 0efabf8

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
@@ -168,6 +168,52 @@ public async Task<SearchAllCatalogueResultModel> GetAllCatalogueSearchResultsAsy
168168
}
169169
}
170170

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