Start working on adding items to shelves from search

This commit is contained in:
Robbie Antenesse 2020-06-24 20:56:59 -06:00
parent 1509cf9d43
commit a4d6fc9913
5 changed files with 107 additions and 8 deletions

View File

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

View File

@ -91,7 +91,11 @@ export const resultDetails = (searchController, result, emit = () => {}) => {
</button>
</p>
${!searchController.showShelves ? null : html`<ul>${searchController.shelves.map(shelf => {
return html`<li><a href="javascript:void(0)">${shelf.name}</a></li>`;
return html`<li>
<button class="pseudo" onclick=${() => searchController.addToShelf({source: 'inventaire', uri: result.uri}, shelf.id)}>
${shelf.name}
</button>
</li>`;
})}</ul>`}
<p>
<a class="small button" href=${result.link} target="_blank">

View File

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

View File

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

View File

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