From 0dae28ea8da3296ec083293b41e158152c1d6bca Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 8 Jul 2021 17:00:54 +0400 Subject: [PATCH] Languages OPDS feed includes book counts --- include/library.h | 9 +++++++++ src/library.cpp | 21 +++++++++++++++++---- src/opds_dumper.cpp | 5 ++++- static/templates/catalog_v2_languages.xml | 1 + test/server.cpp | 3 +++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/include/library.h b/include/library.h index e98a96d72..9139405a2 100644 --- a/include/library.h +++ b/include/library.h @@ -152,6 +152,7 @@ class Library public: typedef std::vector BookIdCollection; + typedef std::map AttributeCounts; public: Library(); @@ -239,6 +240,13 @@ class Library */ std::vector getBooksLanguages() const; + /** + * Get all languagues of the books in the library with counts. + * + * @return A list of languages with the count of books in each language. + */ + AttributeCounts getBooksLanguagesWithCounts() const; + /** * Get all categories of the books in the library. * @@ -342,6 +350,7 @@ class Library typedef const std::string& (Book::*BookStrPropMemFn)() const; private: // functions + AttributeCounts getBookAttributeCounts(BookStrPropMemFn p) const; std::vector getBookPropValueSet(BookStrPropMemFn p) const; BookIdCollection filterViaBookDB(const Filter& filter) const; void updateBookDB(const Book& book); diff --git a/src/library.cpp b/src/library.cpp index 191b8ce6f..d1ed90d36 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -182,18 +182,26 @@ bool Library::writeBookmarksToFile(const std::string& path) const return writeTextFile(path, dumper.dumpLibXMLBookmark()); } -std::vector Library::getBookPropValueSet(BookStrPropMemFn p) const +Library::AttributeCounts Library::getBookAttributeCounts(BookStrPropMemFn p) const { - std::set propValues; + AttributeCounts propValueCounts; for (const auto& pair: m_books) { const auto& book = pair.second; if (book.getOrigId().empty()) { - propValues.insert((book.*p)()); + propValueCounts[(book.*p)()] += 1; } } + return propValueCounts; +} - return std::vector(propValues.begin(), propValues.end()); +std::vector Library::getBookPropValueSet(BookStrPropMemFn p) const +{ + std::vector result; + for ( const auto& kv : getBookAttributeCounts(p) ) { + result.push_back(kv.first); + } + return result; } std::vector Library::getBooksLanguages() const @@ -201,6 +209,11 @@ std::vector Library::getBooksLanguages() const return getBookPropValueSet(&Book::getLanguage); } +Library::AttributeCounts Library::getBooksLanguagesWithCounts() const +{ + return getBookAttributeCounts(&Book::getLanguage); +} + std::vector Library::getBooksCategories() const { std::set categories; diff --git a/src/opds_dumper.cpp b/src/opds_dumper.cpp index 2c560a7e3..8bf1147dc 100644 --- a/src/opds_dumper.cpp +++ b/src/opds_dumper.cpp @@ -159,11 +159,14 @@ std::string OPDSDumper::languagesOPDSFeed() const { const auto now = gen_date_str(); kainjow::mustache::list languageData; - for ( const auto& languageCode : library->getBooksLanguages() ) { + for ( const auto& langAndBookCount : library->getBooksLanguagesWithCounts() ) { + const std::string languageCode = langAndBookCount.first; + const int bookCount = langAndBookCount.second; const auto languageSelfName = getLanguageSelfName(languageCode); languageData.push_back(kainjow::mustache::object{ {"lang_code", languageCode}, {"lang_self_name", languageSelfName}, + {"book_count", to_string(bookCount)}, {"updated", now}, {"id", gen_uuid(libraryId + "/languages/" + languageCode)} }); diff --git a/static/templates/catalog_v2_languages.xml b/static/templates/catalog_v2_languages.xml index 64e24ac68..8a5b74cce 100644 --- a/static/templates/catalog_v2_languages.xml +++ b/static/templates/catalog_v2_languages.xml @@ -16,6 +16,7 @@ {{lang_self_name}} {{{lang_code}}} + {{book_count}} diff --git a/test/server.cpp b/test/server.cpp index 34dad9346..bfa321cd8 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -1035,6 +1035,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages) English eng + 1 @@ -1044,6 +1045,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages) français fra + 1 @@ -1053,6 +1055,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages) русский rus + 1