Consolidate and improve creating BookReferences

This commit is contained in:
Robbie Antenesse 2020-08-24 17:14:08 -06:00
parent 8283c1987a
commit a6a819503f
7 changed files with 28 additions and 50 deletions

View File

@ -202,10 +202,7 @@ class Inventaire {
return a.publishDate < b.publishDate ? -1 : 1; return a.publishDate < b.publishDate ? -1 : 1;
}); });
return covers.map(cover => { return covers;
delete cover.publishDate;
return cover;
});
}); });
} }
} }

View File

@ -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; module.exports = BooksController;

View File

@ -7,12 +7,14 @@ class BookReferenceController {
this.lang = language; this.lang = language;
} }
async createOrUpdateReference(source, sourceId) { async createOrUpdateReference(source, sourceId, skipSearchBySourceCodes = false) {
const searchController = new SearchController(this.models); 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) { if (existingReferences.length > 0) {
return existingReference; return existingReferences[0];
}
} }
let dataClass; let dataClass;
@ -30,13 +32,13 @@ class BookReferenceController {
// Get formatted book data from source // Get formatted book data from source
const bookData = await dataClass.getBookData(sourceId); 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 // 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. // 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); return await this.createReference(bookData, source, sourceId);
@ -57,12 +59,12 @@ class BookReferenceController {
covers: bookData.covers, covers: bookData.covers,
locale: this.lang, locale: this.lang,
}); });
newReference.totalInteractions = 0; // newReference.totalInteractions = 0;
newReference.numReviews = 0; // newReference.numReviews = 0;
newReference.averageRating = null; // newReference.averageRating = null;
newReference.Interactions = []; // newReference.Interactions = [];
newReference.Reviews = []; // newReference.Reviews = [];
newReference.Ratings = []; // newReference.Ratings = [];
return newReference; return newReference;
} }

View File

@ -168,15 +168,11 @@ class SearchController {
); );
} }
async searchReferencesBySourceCode(source, sourceId) { async searchReferencesForExactMatch(name, description) {
const sourceJSONKey = `"${source}"`; // Enable searching withing JSON column. return await this.models.BookReference.findAll({
return await this.models.BookReference.findOne({
where: { where: {
sources: { name,
[sourceJSONKey]: { // Where the object key is the source description,
[Op.eq]: sourceId,
},
},
}, },
...this.bookReferenceSearchAttributes, ...this.bookReferenceSearchAttributes,
}).then( // Empty results give 1 empty model in an array, so filter those out }).then( // Empty results give 1 empty model in an array, so filter those out

View File

@ -1,4 +1,5 @@
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const BookReferenceController = require('./bookReference');
class ShelfController { class ShelfController {
constructor (sequelizeModels, language) { // Language needs to be passed with every request involving books. constructor (sequelizeModels, language) { // Language needs to be passed with every request involving books.

View File

@ -1,5 +1,6 @@
const BooksController = require('../controllers/bookData'); const BooksController = require('../controllers/bookData');
const SearchController = require('../controllers/search'); const SearchController = require('../controllers/search');
const BookReferenceController = require('../controllers/bookReference');
async function routes(fastify, options) { async function routes(fastify, options) {
fastify.get('/api/books', async (request, reply) => { fastify.get('/api/books', async (request, reply) => {
@ -39,9 +40,8 @@ async function routes(fastify, options) {
return existingBookReferences[0].id; return existingBookReferences[0].id;
} }
const books = new BooksController(request.body.source, request.body.uri, request.language); const bookReference = new BookReferenceController(fastify.models, request.language);
const newBookReference = await books.createBookReference(fastify.models.BookReference, request.body.source, request.body.uri); const newBookReference = await bookReference.createOrUpdateReference(request.body.source, request.body.uri, true);
console.log('created new bookreference', newBookReference);
return newBookReference.id; return newBookReference.id;
}); });
} }

View File

@ -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 }, where: { id: request.body.shelfId },
include: [ fastify.models.ShelfItem ], include: [ fastify.models.ShelfItem ],
}); limit: 1,
}))[0];
if (!ShelfController.userOwnsShelf(request.user, shelf)) { if (!ShelfController.userOwnsShelf(request.user, shelf)) {
return reply.code(403).send({ return reply.code(403).send({