From 1dc169d14b9a397b8de526be779c6baea9e4d569 Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Thu, 24 Nov 2022 19:52:51 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=20opds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/opds/SearchPage.js | 83 ++++++++++++++++++++++++++++++++++ server/core/opds/index.js | 3 ++ 2 files changed, 86 insertions(+) create mode 100644 server/core/opds/SearchPage.js diff --git a/server/core/opds/SearchPage.js b/server/core/opds/SearchPage.js new file mode 100644 index 0000000..239069d --- /dev/null +++ b/server/core/opds/SearchPage.js @@ -0,0 +1,83 @@ +const BasePage = require('./BasePage'); + +class SearchPage extends BasePage { + constructor(config) { + super(config); + + this.id = 'search'; + this.title = 'Поиск'; + } + + async body(req) { + const result = {}; + + const query = { + type: req.query.type || '', + term: req.query.term || '', + page: parseInt(req.query.page, 10) || 1, + }; + + let entry = []; + if (query.type) { + if (['author', 'series', 'title'].includes(query.type)) { + const from = query.type; + const page = query.page; + + const limit = 100; + const offset = (page - 1)*limit; + const queryRes = await this.webWorker.search(from, {[from]: query.term, del: 0, offset, limit}); + + const found = queryRes.found; + + for (let i = 0; i < found.length; i++) { + if (i >= limit) + break; + + const row = found[i]; + + entry.push( + this.makeEntry({ + id: row.id, + title: row[from], + link: this.navLink({href: `/${from}?${from}==${encodeURIComponent(row[from])}`}), + }), + ); + } + + if (queryRes.totalFound > offset + found.length) { + entry.push( + this.makeEntry({ + id: 'next_page', + title: '[Следующая страница]', + link: this.navLink({href: `/${this.id}?type=${from}&term=${encodeURIComponent(query.term)}&page=${page + 1}`}), + }), + ); + } + } + } else { + //корневой раздел + entry = [ + this.makeEntry({ + id: 'search_author', + title: 'Поиск авторов', + link: this.navLink({href: `/${this.id}?type=author&term=${encodeURIComponent(query.term)}`}), + }), + this.makeEntry({ + id: 'search_series', + title: 'Поиск серий', + link: this.navLink({href: `/${this.id}?type=series&term=${encodeURIComponent(query.term)}`}), + }), + this.makeEntry({ + id: 'search_title', + title: 'Поиск книг', + link: this.navLink({href: `/${this.id}?type=title&term=${encodeURIComponent(query.term)}`}), + }), + ] + } + + result.entry = entry; + return this.makeBody(result, req); + } +} + +module.exports = SearchPage; \ No newline at end of file diff --git a/server/core/opds/index.js b/server/core/opds/index.js index 4816ae6..b5c85e1 100644 --- a/server/core/opds/index.js +++ b/server/core/opds/index.js @@ -6,6 +6,7 @@ const GenrePage = require('./GenrePage'); const BookPage = require('./BookPage'); const OpensearchPage = require('./OpensearchPage'); +const SearchPage = require('./SearchPage'); module.exports = function(app, config) { const opdsRoot = '/opds'; @@ -19,6 +20,7 @@ module.exports = function(app, config) { const book = new BookPage(config); const opensearch = new OpensearchPage(config); + const search = new SearchPage(config); const routes = [ ['', root], @@ -30,6 +32,7 @@ module.exports = function(app, config) { ['/book', book], ['/opensearch', opensearch], + ['/search', search], ]; const pages = new Map();