diff --git a/server/controllers/books.js b/server/controllers/books.js index ae394c2..727275b 100644 --- a/server/controllers/books.js +++ b/server/controllers/books.js @@ -61,6 +61,41 @@ class BooksController { } } + handleQuickInventaireEntity(entityObject) { + return { + name: ( + typeof entityObject.label !== 'undefined' + ? entityObject.label + : null + ), + description: ( + typeof entityObject.description !== 'undefined' + ? entityObject.description + : null + ), + link: ( + typeof entityObject.uri !== 'undefined' + ? `${this.inventaire}/entity/${entityObject.uri}` + : null + ), + uri: ( + typeof entityObject.uri !== 'undefined' + ? entityObject.uri + : null + ), + covers: ( + typeof entityObject.image !== 'undefined' + ? entityObject.image.map(imageId => { + return { + uri: imageId, + url: `${this.inventaire}/img/entities/${imageId}`, + } + }) + : [] + ), + }; + } + handleInventaireEntity(entityObject) { const hasLabels = typeof entityObject.labels !== 'undefined'; const hasDescriptions = typeof entityObject.descriptions !== 'undefined'; diff --git a/server/controllers/search.js b/server/controllers/search.js index b677bdd..9b84885 100644 --- a/server/controllers/search.js +++ b/server/controllers/search.js @@ -13,6 +13,45 @@ class SearchController { return typeof this.term !== 'undefined' && this.term !== ''; } + quickSearchInventaire() { + if (this.hasQuery) { + const request = fetch(`${this.inventaire}/api/search?types=works&search=${encodeURIComponent(this.term)}&lang=${encodeURIComponent(this.lang)}&limit=10`) + request.catch(exception => { + console.error(exception); + return { + error: exception, + message: 'An error occurred when trying to reach the Inventaire API.', + } + }); + const json = request.then(response => response.json()); + json.catch(exception => { + console.error(exception); + return { + error: exception, + message: 'An error occurred when trying read the response from Inventaire as JSON.', + } + }); + return json.then(responseJSON => { + const works = responseJSON.results.map(work => { + const booksController = new BooksController(this.inventaire, work.uri, this.lang); + const bookData = booksController.handleQuickInventaireEntity(work); + const communityData = booksController.getCommunityData(5); + + return { + ...bookData, + ...communityData, + } + }); + + return { + humans: [], + series: [], + works, + } + }); + } + } + searchInventaire() { if (this.hasQuery) { const request = fetch(`${this.inventaire}/api/entities?action=search&search=${encodeURIComponent(this.term)}&lang=${encodeURIComponent(this.lang)}`) diff --git a/server/routes/search.js b/server/routes/search.js index 40c120b..87cd520 100644 --- a/server/routes/search.js +++ b/server/routes/search.js @@ -6,7 +6,7 @@ async function routes(fastify, options) { const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US' const search = new SearchController(fastify.siteConfig.inventaireDomain, searchTerm, language); - return await search.searchInventaire(); + return await search.quickSearchInventaire(); }); fastify.get('/api/search/cover', async (request, reply) => {