From a4d6fc99133aed61f7cd8e6ed095561be3a15551 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Wed, 24 Jun 2020 20:56:59 -0600 Subject: [PATCH] Start working on adding items to shelves from search --- app/views/search/controller.js | 27 +++++++++++++++++------ app/views/search/resultDetails.js | 6 ++++- app/views/shelves/controller.js | 33 ++++++++++++++++++++++++++++ server/controllers/bookData/index.js | 21 ++++++++++++++++++ server/routes/books.js | 28 +++++++++++++++++++++++ 5 files changed, 107 insertions(+), 8 deletions(-) diff --git a/app/views/search/controller.js b/app/views/search/controller.js index ce5d222..1277301 100644 --- a/app/views/search/controller.js +++ b/app/views/search/controller.js @@ -101,15 +101,28 @@ export class SearchController extends ViewController { return Promise.resolve(); } - async showShelves () { + showShelves () { const shelfController = new ShelvesController(this.appState, this.i18n); - if (!this.hasFetchedShelves) { + let shelvesPromise; + if (shelfController.state.myShelves.length < 1) { console.log('getting'); - await shelfController.getUserShelves(); - console.log('got'); + shelvesPromise = shelfController.getUserShelves(); + } else { + shelvesPromise = Promise.resolve(); } - console.log(shelfController.myShelves); - this.showShelves = true; - this.emit('render'); + shelvesPromise.then(() => { + console.log(shelfController.state.myShelves); + this.showShelves = true; + this.emit('render'); + }); + } + + addToShelf(bookData, shelfId) { + const shelfController = new ShelvesController(this.appState, this.i18n); + shelfController.addItemToShelf(bookData, shelfId).then(result => { + console.log(result); + this.showShelves = false; + this.emit('render'); + }); } } \ No newline at end of file diff --git a/app/views/search/resultDetails.js b/app/views/search/resultDetails.js index 8a3b208..099344d 100644 --- a/app/views/search/resultDetails.js +++ b/app/views/search/resultDetails.js @@ -91,7 +91,11 @@ export const resultDetails = (searchController, result, emit = () => {}) => {

${!searchController.showShelves ? null : html``}

diff --git a/app/views/shelves/controller.js b/app/views/shelves/controller.js index 153f3ea..9c66d1a 100644 --- a/app/views/shelves/controller.js +++ b/app/views/shelves/controller.js @@ -42,4 +42,37 @@ export class ShelvesController extends ViewController { this.state.loadedShelves[this.targetShelf] = shelf; }); } + + async addItemToShelf (book, shelfId) { + let bookId; + if (typeof book.source !== 'undefined' && typeof book.uri !== 'undefined') { + const bookSearchResult = await fetch('/api/books/getId', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(book), + }).then(response => response.json()); + + if (typeof bookSearchResult.error !== 'undefined') { + console.error(bookSearchResult); + return bookSearchResult; + } + + bookId = bookSearchResult; + } else { + bookId = book.id; + } + + return fetch('/api/shelf/addItem', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + shelfId, + bookId, + }), + }) + } } \ No newline at end of file diff --git a/server/controllers/bookData/index.js b/server/controllers/bookData/index.js index c741932..94a6b2e 100644 --- a/server/controllers/bookData/index.js +++ b/server/controllers/bookData/index.js @@ -93,6 +93,27 @@ class BooksController { ), }; } + + async createBookReference (bookReferencesModel, source, uri) { + const inventaire = new Inventaire(this.language); + const bookData = await inventaire.getBookData(uri); + return await bookReferencesModel.create({ + values: { + name: bookData.name, + description: bookData.description, + sources: { + [source]: uri, + }, + covers: bookData.covers.map(cover => { + return { + sourceId: uri, + url: cover.url, + }; + }), + locale: this.language, + } + }); + } } module.exports = BooksController; \ No newline at end of file diff --git a/server/routes/books.js b/server/routes/books.js index 4957fa2..3294c83 100644 --- a/server/routes/books.js +++ b/server/routes/books.js @@ -1,4 +1,5 @@ const BooksController = require('../controllers/bookData'); +const SearchController = require('../controllers/search'); async function routes(fastify, options) { fastify.get('/api/books', async (request, reply) => { @@ -16,6 +17,33 @@ async function routes(fastify, options) { return await books.getInventaireCovers(); }); + + 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; + } + + const books = new BooksController(request.body.source, request.body.uri, request.language); + const newBookReference = await books.createBookReference(request.body.source, request.body.uri); + console.log('created new bookreference', newBookReference); + return newBookReference.id; + }); } module.exports = routes \ No newline at end of file