Skip to content

Commit 2be68a9

Browse files
veloman-yunkankelson42
authored andcommitted
Languages OPDS feed includes book counts
1 parent 8a210ed commit 2be68a9

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

include/library.h

+9
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ class Library
154154

155155
public:
156156
typedef std::vector<std::string> BookIdCollection;
157+
typedef std::map<std::string, int> AttributeCounts;
157158

158159
public:
159160
Library();
@@ -242,6 +243,13 @@ class Library
242243
*/
243244
std::vector<std::string> getBooksLanguages() const;
244245

246+
/**
247+
* Get all languagues of the books in the library with counts.
248+
*
249+
* @return A list of languages with the count of books in each language.
250+
*/
251+
AttributeCounts getBooksLanguagesWithCounts() const;
252+
245253
/**
246254
* Get all categories of the books in the library.
247255
*
@@ -345,6 +353,7 @@ class Library
345353
typedef const std::string& (Book::*BookStrPropMemFn)() const;
346354

347355
private: // functions
356+
AttributeCounts getBookAttributeCounts(BookStrPropMemFn p) const;
348357
std::vector<std::string> getBookPropValueSet(BookStrPropMemFn p) const;
349358
BookIdCollection filterViaBookDB(const Filter& filter) const;
350359
void updateBookDB(const Book& book);

src/library.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -208,25 +208,38 @@ bool Library::writeBookmarksToFile(const std::string& path) const
208208
return writeTextFile(path, dumper.dumpLibXMLBookmark());
209209
}
210210

211-
std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p) const
211+
Library::AttributeCounts Library::getBookAttributeCounts(BookStrPropMemFn p) const
212212
{
213-
std::set<std::string> propValues;
213+
AttributeCounts propValueCounts;
214214

215215
for (const auto& pair: m_books) {
216216
const auto& book = pair.second;
217217
if (book.getOrigId().empty()) {
218-
propValues.insert((book.*p)());
218+
propValueCounts[(book.*p)()] += 1;
219219
}
220220
}
221+
return propValueCounts;
222+
}
221223

222-
return std::vector<std::string>(propValues.begin(), propValues.end());
224+
std::vector<std::string> Library::getBookPropValueSet(BookStrPropMemFn p) const
225+
{
226+
std::vector<std::string> result;
227+
for ( const auto& kv : getBookAttributeCounts(p) ) {
228+
result.push_back(kv.first);
229+
}
230+
return result;
223231
}
224232

225233
std::vector<std::string> Library::getBooksLanguages() const
226234
{
227235
return getBookPropValueSet(&Book::getLanguage);
228236
}
229237

238+
Library::AttributeCounts Library::getBooksLanguagesWithCounts() const
239+
{
240+
return getBookAttributeCounts(&Book::getLanguage);
241+
}
242+
230243
std::vector<std::string> Library::getBooksCategories() const
231244
{
232245
std::set<std::string> categories;

src/opds_dumper.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,14 @@ std::string OPDSDumper::languagesOPDSFeed() const
160160
{
161161
const auto now = gen_date_str();
162162
kainjow::mustache::list languageData;
163-
for ( const auto& languageCode : library->getBooksLanguages() ) {
163+
for ( const auto& langAndBookCount : library->getBooksLanguagesWithCounts() ) {
164+
const std::string languageCode = langAndBookCount.first;
165+
const int bookCount = langAndBookCount.second;
164166
const auto languageSelfName = getLanguageSelfName(languageCode);
165167
languageData.push_back(kainjow::mustache::object{
166168
{"lang_code", languageCode},
167169
{"lang_self_name", languageSelfName},
170+
{"book_count", to_string(bookCount)},
168171
{"updated", now},
169172
{"id", gen_uuid(libraryId + "/languages/" + languageCode)}
170173
});

static/templates/catalog_v2_languages.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<entry>
1717
<title>{{lang_self_name}}</title>
1818
<dc:language>{{{lang_code}}}</dc:language>
19+
<thr:count>{{book_count}}</thr:count>
1920
<link rel="subsection"
2021
href="{{endpoint_root}}/entries?lang={{{lang_code}}}"
2122
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>

test/server.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages)
10351035
<entry>
10361036
<title>English</title>
10371037
<dc:language>eng</dc:language>
1038+
<thr:count>1</thr:count>
10381039
<link rel="subsection"
10391040
href="/catalog/v2/entries?lang=eng"
10401041
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
@@ -1044,6 +1045,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages)
10441045
<entry>
10451046
<title>français</title>
10461047
<dc:language>fra</dc:language>
1048+
<thr:count>1</thr:count>
10471049
<link rel="subsection"
10481050
href="/catalog/v2/entries?lang=fra"
10491051
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>
@@ -1053,6 +1055,7 @@ TEST_F(LibraryServerTest, catalog_v2_languages)
10531055
<entry>
10541056
<title>русский</title>
10551057
<dc:language>rus</dc:language>
1058+
<thr:count>1</thr:count>
10561059
<link rel="subsection"
10571060
href="/catalog/v2/entries?lang=rus"
10581061
type="application/atom+xml;profile=opds-catalog;kind=acquisition"/>

0 commit comments

Comments
 (0)