2020-02-07 01:27:22 +01:00
|
|
|
const BooksController = require('../controllers/bookData');
|
2020-06-25 04:56:59 +02:00
|
|
|
const SearchController = require('../controllers/search');
|
2020-08-25 01:14:08 +02:00
|
|
|
const BookReferenceController = require('../controllers/bookReference');
|
2019-09-12 19:35:43 +02:00
|
|
|
|
|
|
|
async function routes(fastify, options) {
|
|
|
|
fastify.get('/api/books', async (request, reply) => {
|
|
|
|
const bookURI = typeof request.query.uri !== 'undefined' ? request.query.uri.trim() : '';
|
|
|
|
const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US'
|
|
|
|
const books = new BooksController(fastify.siteConfig.inventaireDomain, bookURI, language);
|
|
|
|
|
|
|
|
return books.getBookData();
|
|
|
|
});
|
2019-09-13 01:04:50 +02:00
|
|
|
|
|
|
|
fastify.get('/api/books/covers', async (request, reply) => {
|
|
|
|
const bookURI = typeof request.query.uri !== 'undefined' ? request.query.uri.trim() : '';
|
|
|
|
const language = typeof request.query.lang !== 'undefined' ? request.query.lang.trim().split('-')[0] : undefined; // Get base language in cases like 'en-US'
|
|
|
|
const books = new BooksController(fastify.siteConfig.inventaireDomain, bookURI, language);
|
|
|
|
|
|
|
|
return await books.getInventaireCovers();
|
|
|
|
});
|
2020-06-25 04:56:59 +02:00
|
|
|
|
|
|
|
fastify.post('/api/books/getId', async (request, reply) => {
|
|
|
|
if (typeof request.body.source === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.getId.missing_source',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof request.body.uri === 'undefined') {
|
|
|
|
return reply.code(400).send({
|
|
|
|
error: true,
|
|
|
|
message: 'api.shelf.getId.missing_uri',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const search = new SearchController(fastify.models);
|
|
|
|
const existingBookReferences = await search.searchReferencesBySourceCodes(request.body.source, [request.body.uri]);
|
|
|
|
if (existingBookReferences.length > 0) {
|
|
|
|
return existingBookReferences[0].id;
|
|
|
|
}
|
|
|
|
|
2020-08-25 01:14:08 +02:00
|
|
|
const bookReference = new BookReferenceController(fastify.models, request.language);
|
|
|
|
const newBookReference = await bookReference.createOrUpdateReference(request.body.source, request.body.uri, true);
|
2020-06-25 04:56:59 +02:00
|
|
|
return newBookReference.id;
|
|
|
|
});
|
2019-09-12 19:35:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = routes
|