mirror of https://github.com/kiwix/libkiwix.git
Add the method get bookByPath in library.
This commit is contained in:
parent
90ba27fbab
commit
46626a3f98
|
@ -149,6 +149,7 @@ class Library
|
|||
bool removeBookmark(const std::string& zimId, const std::string& url);
|
||||
|
||||
Book& getBookById(const std::string& id);
|
||||
Book& getBookByPath(const std::string& path);
|
||||
std::shared_ptr<Reader> getReaderById(const std::string& id);
|
||||
|
||||
/**
|
||||
|
|
|
@ -85,6 +85,18 @@ Book& Library::getBookById(const std::string& id)
|
|||
return m_books.at(id);
|
||||
}
|
||||
|
||||
Book& Library::getBookByPath(const std::string& path)
|
||||
{
|
||||
for(auto& it: m_books) {
|
||||
auto& book = it.second;
|
||||
if (book.getPath() == path)
|
||||
return book;
|
||||
}
|
||||
std::ostringstream ss;
|
||||
ss << "No book with path " << path << " in the library." << std::endl;
|
||||
throw std::out_of_range(ss.str());
|
||||
}
|
||||
|
||||
std::shared_ptr<Reader> Library::getReaderById(const std::string& id)
|
||||
{
|
||||
try {
|
||||
|
|
|
@ -258,4 +258,12 @@ TEST_F(LibraryTest, filterCheck)
|
|||
EXPECT_EQ(bookIds.size(), 1U);
|
||||
|
||||
}
|
||||
|
||||
TEST_F(LibraryTest, getBookByPath)
|
||||
{
|
||||
auto& book = lib.getBookById(lib.getBooksIds()[0]);
|
||||
book.setPath("/some/abs/path.zim");
|
||||
EXPECT_EQ(lib.getBookByPath("/some/abs/path.zim").getId(), book.getId());
|
||||
EXPECT_THROW(lib.getBookByPath("non/existant/path.zim"), std::out_of_range);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue