diff --git a/include/library.h b/include/library.h index 6425ba63d..53dec415a 100644 --- a/include/library.h +++ b/include/library.h @@ -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 getReaderById(const std::string& id); /** diff --git a/include/searcher.h b/include/searcher.h index 9c0058258..2d3a58f5c 100644 --- a/include/searcher.h +++ b/include/searcher.h @@ -89,7 +89,7 @@ class Searcher * @param resultEnd the end offset of the search results (used for pagination). * @param verbose print some info on stdout if true. */ - void search(std::string& search, + void search(const std::string& search, unsigned int resultStart, unsigned int resultEnd, const bool verbose = false); diff --git a/src/library.cpp b/src/library.cpp index fc027718d..0dad74794 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -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 Library::getReaderById(const std::string& id) { try { diff --git a/src/searcher.cpp b/src/searcher.cpp index 6802d6635..fb5eca13d 100644 --- a/src/searcher.cpp +++ b/src/searcher.cpp @@ -97,7 +97,7 @@ Reader* Searcher::get_reader(int readerIndex) } /* Search strings in the database */ -void Searcher::search(std::string& search, +void Searcher::search(const std::string& search, unsigned int resultStart, unsigned int resultEnd, const bool verbose) diff --git a/test/library.cpp b/test/library.cpp index 3f23df775..b4efc44a1 100644 --- a/test/library.cpp +++ b/test/library.cpp @@ -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); +} };