Replace std::vector<std::string> with SuggestionItem

Each sugestions used to be stored as vector of strings to hold various values
such as title, path etc inside them. With this commit, we use the new
dedicated class `SuggestionItem` to do the same.
This commit is contained in:
Maneesh P M 2021-05-13 12:00:39 +05:30 committed by Matthieu Gautier
parent 5315034afe
commit 5567d8ca49
3 changed files with 15 additions and 21 deletions

View File

@ -77,7 +77,7 @@ class SuggestionItem
* file.
*/
using SuggestionsList_t = std::vector<std::vector<std::string>>;
using SuggestionsList_t = std::vector<SuggestionItem>;
class Reader
{
public:

View File

@ -432,12 +432,12 @@ bool Reader::searchSuggestions(const string& prefix,
article is already in the suggestions list (with an other
title) */
bool insert = true;
std::vector<std::vector<std::string>>::iterator suggestionItr;
std::vector<SuggestionItem>::iterator suggestionItr;
for (suggestionItr = results.begin();
suggestionItr != results.end();
suggestionItr++) {
int result = normalizedArticleTitle.compare((*suggestionItr)[2]);
if (result == 0 && articleFinalUrl.compare((*suggestionItr)[1]) == 0) {
int result = normalizedArticleTitle.compare((*suggestionItr).getNormalizedTitle());
if (result == 0 && articleFinalUrl.compare((*suggestionItr).getPath()) == 0) {
insert = false;
break;
} else if (result < 0) {
@ -447,10 +447,7 @@ bool Reader::searchSuggestions(const string& prefix,
/* Insert if possible */
if (insert) {
std::vector<std::string> suggestion;
suggestion.push_back(entry.getTitle());
suggestion.push_back(articleFinalUrl);
suggestion.push_back(normalizedArticleTitle);
SuggestionItem suggestion(entry.getTitle(), normalizedArticleTitle, articleFinalUrl);
results.insert(suggestionItr, suggestion);
}
@ -506,11 +503,8 @@ bool Reader::searchSuggestionsSmart(const string& prefix,
for (auto current = suggestions.begin();
current != suggestions.end();
current++) {
std::vector<std::string> suggestion;
suggestion.push_back(current.getTitle());
suggestion.push_back(current.getPath());
suggestion.push_back(kiwix::normalize(current.getTitle()));
suggestion.push_back(current.getSnippet());
SuggestionItem suggestion(current.getTitle(), kiwix::normalize(current.getTitle()),
current.getPath(), current.getSnippet());
results.push_back(suggestion);
}
retVal = true;
@ -531,7 +525,7 @@ bool Reader::getNextSuggestion(string& title)
{
if (this->suggestionsOffset != this->suggestions.end()) {
/* title */
title = (*(this->suggestionsOffset))[0];
title = (*(this->suggestionsOffset)).getTitle();
/* increment the cursor for the next call */
this->suggestionsOffset++;
@ -546,8 +540,8 @@ bool Reader::getNextSuggestion(string& title, string& url)
{
if (this->suggestionsOffset != this->suggestions.end()) {
/* title */
title = (*(this->suggestionsOffset))[0];
url = (*(this->suggestionsOffset))[1];
title = (*(this->suggestionsOffset)).getTitle();
url = (*(this->suggestionsOffset)).getPath();
/* increment the cursor for the next call */
this->suggestionsOffset++;

View File

@ -414,15 +414,15 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
reader->searchSuggestionsSmart(term, maxSuggestionCount, suggestions);
for(auto& suggestion:suggestions) {
MustacheData result;
result.set("label", suggestion[0]);
result.set("label", suggestion.getTitle());
if (!suggestion[3].empty()) {
result.set("label", suggestion[3]);
if (suggestion.hasSnippet()) {
result.set("label", suggestion.getSnippet());
}
result.set("value", suggestion[0]);
result.set("value", suggestion.getTitle());
result.set("kind", "path");
result.set("path", suggestion[1]);
result.set("path", suggestion.getPath());
result.set("first", first);
first = false;
results.push_back(result);