From efd1d12ba767068d1254f8afc798d8753e5c9b09 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Fri, 11 Jul 2025 12:55:30 +0200 Subject: [PATCH] - Import di un file XLS contenente una lista di libri, all'interno di un catalogo. --- src/server/router/api_router.js | 68 +++++++++++++++++++++++++++++++-- src/server/version.txt | 2 +- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/server/router/api_router.js b/src/server/router/api_router.js index 6c81a8d..c34e281 100644 --- a/src/server/router/api_router.js +++ b/src/server/router/api_router.js @@ -9,6 +9,9 @@ const XLSX = require('xlsx'); const upload = multer({ dest: 'uploads/' }); +const Product = require('../models/product'); +const ProductInfo = require('../models/productInfo'); +const Author = require('../models/author'); router.post('/test-lungo', authenticate, (req, res) => { const timeout = req.body.timeout; @@ -304,7 +307,7 @@ router.get('/pageviews/weekly-top-pages', authenticate_noerror, async (req, res) router.post('/api/convert-csv-to-xls', upload.single('csv'), (req, res) => { try { const csvFilePath = req.file.path; - + // Leggi il CSV con SheetJS const csvData = fs.readFileSync(csvFilePath, 'utf-8'); const worksheet = XLSX.utils.csv_to_sheet(csvData); @@ -323,15 +326,74 @@ router.post('/api/convert-csv-to-xls', upload.single('csv'), (req, res) => { console.error('Errore nel download del file:', err); res.status(500).send('Errore nel download del file'); } - + // Pulisci il file temporaneo fs.unlinkSync(csvFilePath); fs.unlinkSync(xlsFilePath); }); } catch (error) { - console.error("Errore nella conversione del file:", error); + console.error('Errore nella conversione del file:', error); res.status(500).send('Errore nella conversione del file'); } }); +// API per la ricerca dei libri +router.post('/search-books', authenticate, async (req, res) => { + const { books } = req.body; + + if (!books || books.length === 0) { + return res.status(400).json({ error: 'Nessun dato fornito per la ricerca' }); + } + + try { + // Crea un array per raccogliere i risultati + let results = []; + + let productInfo = null; + let product = null; + + for (const book of books) { + for (const field of book) { + let valido = typeof field === 'string' && field.length > 4 && field.length < 50; + if (valido) { + // Cerca il primo record che corrisponde per ISBN o titolo + if (true) { + productInfo = await ProductInfo.findOne({ + $or: [{ code: field.toUpperCase() }, { name: new RegExp(`.*${field.toUpperCase()}.*`, 'i') }], + }).exec(); + if (productInfo) { + product = await Product.findOne({ idProductInfo: productInfo._id }).exec(); + if (product) { + const existingResult = results.find((r) => r._id.toString() === product._id.toString()); + if (!existingResult) { + let titolo = productInfo.name; + results.push({ + ...product, + productInfo, + _id: product._id, + title: titolo, + isbn: product.isbn, + authors: await Promise.all( + productInfo.idAuthors.map(async (authorId) => { + const author = await Author.findById(authorId).exec(); + return author ? `${author.name} ${author.surname}`.trim() : ''; + }) + ), + select: true, + }); + } + } + } + } + } + } + } + + res.status(200).json(results); // Restituisci i risultati trovati + } catch (err) { + console.error('Errore durante la ricerca dei libri:', err); + res.status(500).json({ error: 'Errore interno del server' }); + } +}); + module.exports = router; diff --git a/src/server/version.txt b/src/server/version.txt index 6579f9d..e0bb42e 100644 --- a/src/server/version.txt +++ b/src/server/version.txt @@ -1 +1 @@ -1.2.63 \ No newline at end of file +1.2.65 \ No newline at end of file