Use quick search from Inventaire for default search

This commit is contained in:
Robbie Antenesse 2019-09-16 12:07:45 -06:00
parent c7aaf56d08
commit d3d35bacc8
3 changed files with 75 additions and 1 deletions

View File

@ -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';

View File

@ -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)}`)

View File

@ -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) => {