Consolidate and improve creating BookReferences
This commit is contained in:
parent
8283c1987a
commit
a6a819503f
|
@ -202,10 +202,7 @@ class Inventaire {
|
|||
return a.publishDate < b.publishDate ? -1 : 1;
|
||||
});
|
||||
|
||||
return covers.map(cover => {
|
||||
delete cover.publishDate;
|
||||
return cover;
|
||||
});
|
||||
return covers;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,25 +93,6 @@ class BooksController {
|
|||
),
|
||||
};
|
||||
}
|
||||
|
||||
async createBookReference (bookReferencesModel, source, uri) {
|
||||
const inventaire = new Inventaire(this.language);
|
||||
const bookData = await inventaire.getBookData(uri);
|
||||
return await bookReferencesModel.create({
|
||||
name: bookData.name,
|
||||
description: bookData.description,
|
||||
sources: {
|
||||
[source]: uri,
|
||||
},
|
||||
covers: (bookData.covers ? bookData.covers : []).map(cover => {
|
||||
return {
|
||||
sourceId: uri,
|
||||
url: cover.url,
|
||||
};
|
||||
}),
|
||||
locale: this.language,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BooksController;
|
|
@ -7,12 +7,14 @@ class BookReferenceController {
|
|||
this.lang = language;
|
||||
}
|
||||
|
||||
async createOrUpdateReference(source, sourceId) {
|
||||
async createOrUpdateReference(source, sourceId, skipSearchBySourceCodes = false) {
|
||||
const searchController = new SearchController(this.models);
|
||||
const existingReference = searchController.searchReferencesBySourceCode(source, sourceId);
|
||||
if (!skipSearchBySourceCodes) {
|
||||
const existingReferences = await searchController.searchReferencesBySourceCodes(source, [sourceId]);
|
||||
|
||||
if (existingReference.id !== null) {
|
||||
return existingReference;
|
||||
if (existingReferences.length > 0) {
|
||||
return existingReferences[0];
|
||||
}
|
||||
}
|
||||
|
||||
let dataClass;
|
||||
|
@ -30,13 +32,13 @@ class BookReferenceController {
|
|||
// Get formatted book data from source
|
||||
const bookData = await dataClass.getBookData(sourceId);
|
||||
|
||||
if (typeof bookData.uri !== 'undefined') {
|
||||
if (typeof bookData.sources[0].uri !== 'undefined') {
|
||||
// Check for references by exact name and author from source
|
||||
const matchingReference = await searchController.searchReferencesForExactMatch(bookData.name, bookData.description);
|
||||
const matchingReferences = await searchController.searchReferencesForExactMatch(bookData.name, bookData.description);
|
||||
|
||||
if (matchingReference.id !== null) {
|
||||
if (matchingReferences.length > 0) {
|
||||
// If a match is found, update the sources of reference in the database and return it.
|
||||
return await this.addSourceToReference(matchingReference, source, sourceId);
|
||||
return await this.addSourceToReference(matchingReferences[0], source, sourceId);
|
||||
}
|
||||
|
||||
return await this.createReference(bookData, source, sourceId);
|
||||
|
@ -57,12 +59,12 @@ class BookReferenceController {
|
|||
covers: bookData.covers,
|
||||
locale: this.lang,
|
||||
});
|
||||
newReference.totalInteractions = 0;
|
||||
newReference.numReviews = 0;
|
||||
newReference.averageRating = null;
|
||||
newReference.Interactions = [];
|
||||
newReference.Reviews = [];
|
||||
newReference.Ratings = [];
|
||||
// newReference.totalInteractions = 0;
|
||||
// newReference.numReviews = 0;
|
||||
// newReference.averageRating = null;
|
||||
// newReference.Interactions = [];
|
||||
// newReference.Reviews = [];
|
||||
// newReference.Ratings = [];
|
||||
return newReference;
|
||||
}
|
||||
|
||||
|
|
|
@ -168,15 +168,11 @@ class SearchController {
|
|||
);
|
||||
}
|
||||
|
||||
async searchReferencesBySourceCode(source, sourceId) {
|
||||
const sourceJSONKey = `"${source}"`; // Enable searching withing JSON column.
|
||||
return await this.models.BookReference.findOne({
|
||||
async searchReferencesForExactMatch(name, description) {
|
||||
return await this.models.BookReference.findAll({
|
||||
where: {
|
||||
sources: {
|
||||
[sourceJSONKey]: { // Where the object key is the source
|
||||
[Op.eq]: sourceId,
|
||||
},
|
||||
},
|
||||
name,
|
||||
description,
|
||||
},
|
||||
...this.bookReferenceSearchAttributes,
|
||||
}).then( // Empty results give 1 empty model in an array, so filter those out
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const fetch = require('node-fetch');
|
||||
const BookReferenceController = require('./bookReference');
|
||||
|
||||
class ShelfController {
|
||||
constructor (sequelizeModels, language) { // Language needs to be passed with every request involving books.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const BooksController = require('../controllers/bookData');
|
||||
const SearchController = require('../controllers/search');
|
||||
const BookReferenceController = require('../controllers/bookReference');
|
||||
|
||||
async function routes(fastify, options) {
|
||||
fastify.get('/api/books', async (request, reply) => {
|
||||
|
@ -39,9 +40,8 @@ async function routes(fastify, options) {
|
|||
return existingBookReferences[0].id;
|
||||
}
|
||||
|
||||
const books = new BooksController(request.body.source, request.body.uri, request.language);
|
||||
const newBookReference = await books.createBookReference(fastify.models.BookReference, request.body.source, request.body.uri);
|
||||
console.log('created new bookreference', newBookReference);
|
||||
const bookReference = new BookReferenceController(fastify.models, request.language);
|
||||
const newBookReference = await bookReference.createOrUpdateReference(request.body.source, request.body.uri, true);
|
||||
return newBookReference.id;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -188,10 +188,11 @@ async function routes(fastify, options) {
|
|||
});
|
||||
}
|
||||
|
||||
const shelf = await request.user.getShelf({
|
||||
const shelf = (await request.user.getShelves({
|
||||
where: { id: request.body.shelfId },
|
||||
include: [ fastify.models.ShelfItem ],
|
||||
});
|
||||
limit: 1,
|
||||
}))[0];
|
||||
|
||||
if (!ShelfController.userOwnsShelf(request.user, shelf)) {
|
||||
return reply.code(403).send({
|
||||
|
|
Loading…
Reference in New Issue