Split XML generation code for full & partial entries

This commit is contained in:
Veloman Yunkan 2022-10-06 13:48:58 +04:00
parent 0841472004
commit dc194683bb
1 changed files with 20 additions and 14 deletions

View File

@ -71,10 +71,11 @@ IllustrationInfo getBookIllustrationInfo(const Book& book)
return illustrations; return illustrations;
} }
kainjow::mustache::object getSingleBookData(const Book& book) std::string fullEntryXML(const Book& book, const std::string& rootLocation)
{ {
const auto bookDate = book.getDate() + "T00:00:00Z"; const auto bookDate = book.getDate() + "T00:00:00Z";
return kainjow::mustache::object{ const kainjow::mustache::object data{
{"root", rootLocation},
{"id", book.getId()}, {"id", book.getId()},
{"name", book.getName()}, {"name", book.getName()},
{"title", book.getTitle()}, {"title", book.getTitle()},
@ -94,16 +95,20 @@ kainjow::mustache::object getSingleBookData(const Book& book)
{"size", to_string(book.getSize())}, {"size", to_string(book.getSize())},
{"icons", getBookIllustrationInfo(book)}, {"icons", getBookIllustrationInfo(book)},
}; };
return render_template(RESOURCE::templates::catalog_v2_entry_xml, data);
} }
std::string getSingleBookEntryXML(const Book& book, const std::string& rootLocation, const std::string& endpointRoot, bool partial) std::string partialEntryXML(const Book& book, const std::string& rootLocation, const std::string& endpointRoot)
{ {
auto data = getSingleBookData(book); const auto bookDate = book.getDate() + "T00:00:00Z";
data["endpoint_root"] = endpointRoot; const kainjow::mustache::object data{
data["root"] = rootLocation; {"root", rootLocation},
const auto xmlTemplate = partial {"endpoint_root", endpointRoot},
? RESOURCE::templates::catalog_v2_partial_entry_xml {"id", book.getId()},
: RESOURCE::templates::catalog_v2_entry_xml; {"title", book.getTitle()},
{"updated", bookDate}, // XXX: this should be the entry update datetime
};
const auto xmlTemplate = RESOURCE::templates::catalog_v2_partial_entry_xml;
return render_template(xmlTemplate, data); return render_template(xmlTemplate, data);
} }
@ -113,9 +118,10 @@ BooksData getBooksData(const Library* library, const std::vector<std::string>& b
for ( const auto& bookId : bookIds ) { for ( const auto& bookId : bookIds ) {
try { try {
const Book book = library->getBookByIdThreadSafe(bookId); const Book book = library->getBookByIdThreadSafe(bookId);
booksData.push_back(kainjow::mustache::object{ const auto entryXML = partial
{"entry", getSingleBookEntryXML(book, rootLocation, endpointRoot, partial)} ? partialEntryXML(book, rootLocation, endpointRoot)
}); : fullEntryXML(book, rootLocation);
booksData.push_back(kainjow::mustache::object{ {"entry", entryXML} });
} catch ( const std::out_of_range& ) { } catch ( const std::out_of_range& ) {
// the book was removed from the library since its id was obtained // the book was removed from the library since its id was obtained
// ignore it // ignore it
@ -224,7 +230,7 @@ std::string OPDSDumper::dumpOPDSCompleteEntry(const std::string& bookId) const
const auto book = library->getBookById(bookId); const auto book = library->getBookById(bookId);
return XML_HEADER return XML_HEADER
+ "\n" + "\n"
+ getSingleBookEntryXML(book, rootLocation, "", false); + fullEntryXML(book, rootLocation);
} }
std::string OPDSDumper::categoriesOPDSFeed() const std::string OPDSDumper::categoriesOPDSFeed() const