mirror of https://github.com/kiwix/libkiwix.git
Helper function Library::getBookPropValueSet()
Introduced a helper function `Library::getBookPropValueSet()` and deduplicated Library::getBooks{Languages,Creators,Publishers}() methods.
This commit is contained in:
parent
b2027b397c
commit
18871b4b15
|
@ -341,7 +341,11 @@ class Library
|
||||||
friend class OPDSDumper;
|
friend class OPDSDumper;
|
||||||
friend class libXMLDumper;
|
friend class libXMLDumper;
|
||||||
|
|
||||||
|
private: // types
|
||||||
|
typedef const std::string& (Book::*BookStrPropMemFn)() const;
|
||||||
|
|
||||||
private: // functions
|
private: // functions
|
||||||
|
std::vector<std::string> getBookPropValueSet(BookStrPropMemFn p) const;
|
||||||
BookIdCollection filterViaBookDB(const Filter& filter) const;
|
BookIdCollection filterViaBookDB(const Filter& filter) const;
|
||||||
void updateBookDB(const Book& book);
|
void updateBookDB(const Book& book);
|
||||||
};
|
};
|
||||||
|
|
|
@ -208,23 +208,23 @@ bool Library::writeBookmarksToFile(const std::string& path) const
|
||||||
return writeTextFile(path, dumper.dumpLibXMLBookmark());
|
return writeTextFile(path, dumper.dumpLibXMLBookmark());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p) const
|
||||||
|
{
|
||||||
|
std::set<std::string> propValues;
|
||||||
|
|
||||||
|
for (const auto& pair: m_books) {
|
||||||
|
const auto& book = pair.second;
|
||||||
|
if (book.getOrigId().empty()) {
|
||||||
|
propValues.insert((book.*p)());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::vector<std::string>(propValues.begin(), propValues.end());
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> Library::getBooksLanguages() const
|
std::vector<std::string> Library::getBooksLanguages() const
|
||||||
{
|
{
|
||||||
std::vector<std::string> booksLanguages;
|
return getBookPropValueSet(&Book::getLanguage);
|
||||||
std::map<std::string, bool> booksLanguagesMap;
|
|
||||||
|
|
||||||
for (auto& pair: m_books) {
|
|
||||||
auto& book = pair.second;
|
|
||||||
auto& language = book.getLanguage();
|
|
||||||
if (booksLanguagesMap.find(language) == booksLanguagesMap.end()) {
|
|
||||||
if (book.getOrigId().empty()) {
|
|
||||||
booksLanguagesMap[language] = true;
|
|
||||||
booksLanguages.push_back(language);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return booksLanguages;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> Library::getBooksCategories() const
|
std::vector<std::string> Library::getBooksCategories() const
|
||||||
|
@ -244,40 +244,12 @@ std::vector<std::string> Library::getBooksCategories() const
|
||||||
|
|
||||||
std::vector<std::string> Library::getBooksCreators() const
|
std::vector<std::string> Library::getBooksCreators() const
|
||||||
{
|
{
|
||||||
std::vector<std::string> booksCreators;
|
return getBookPropValueSet(&Book::getCreator);
|
||||||
std::map<std::string, bool> booksCreatorsMap;
|
|
||||||
|
|
||||||
for (auto& pair: m_books) {
|
|
||||||
auto& book = pair.second;
|
|
||||||
auto& creator = book.getCreator();
|
|
||||||
if (booksCreatorsMap.find(creator) == booksCreatorsMap.end()) {
|
|
||||||
if (book.getOrigId().empty()) {
|
|
||||||
booksCreatorsMap[creator] = true;
|
|
||||||
booksCreators.push_back(creator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return booksCreators;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> Library::getBooksPublishers() const
|
std::vector<std::string> Library::getBooksPublishers() const
|
||||||
{
|
{
|
||||||
std::vector<std::string> booksPublishers;
|
return getBookPropValueSet(&Book::getPublisher);
|
||||||
std::map<std::string, bool> booksPublishersMap;
|
|
||||||
|
|
||||||
for (auto& pair:m_books) {
|
|
||||||
auto& book = pair.second;
|
|
||||||
auto& publisher = book.getPublisher();
|
|
||||||
if (booksPublishersMap.find(publisher) == booksPublishersMap.end()) {
|
|
||||||
if (book.getOrigId().empty()) {
|
|
||||||
booksPublishersMap[publisher] = true;
|
|
||||||
booksPublishers.push_back(publisher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return booksPublishers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks) const
|
const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks) const
|
||||||
|
|
|
@ -275,9 +275,25 @@ TEST_F(LibraryTest, getBookMarksTest)
|
||||||
TEST_F(LibraryTest, sanityCheck)
|
TEST_F(LibraryTest, sanityCheck)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(lib.getBookCount(true, true), 12U);
|
EXPECT_EQ(lib.getBookCount(true, true), 12U);
|
||||||
EXPECT_EQ(lib.getBooksLanguages().size(), 3U);
|
EXPECT_EQ(lib.getBooksLanguages(),
|
||||||
EXPECT_EQ(lib.getBooksCreators().size(), 9U);
|
std::vector<std::string>({"deu", "eng", "fra"})
|
||||||
EXPECT_EQ(lib.getBooksPublishers().size(), 3U);
|
);
|
||||||
|
EXPECT_EQ(lib.getBooksCreators(), std::vector<std::string>({
|
||||||
|
"Islam Stack Exchange",
|
||||||
|
"Movies & TV Stack Exchange",
|
||||||
|
"Mythology & Folklore Stack Exchange",
|
||||||
|
"TED",
|
||||||
|
"Tania Louis",
|
||||||
|
"Wiki",
|
||||||
|
"Wikibooks",
|
||||||
|
"Wikipedia",
|
||||||
|
"Wikiquote"
|
||||||
|
}));
|
||||||
|
EXPECT_EQ(lib.getBooksPublishers(), std::vector<std::string>({
|
||||||
|
"",
|
||||||
|
"Kiwix",
|
||||||
|
"Kiwix & Some Enthusiasts"
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(LibraryTest, categoryHandling)
|
TEST_F(LibraryTest, categoryHandling)
|
||||||
|
|
Loading…
Reference in New Issue