From 1405e003e76423a465ae2cd3536435da1b488ba2 Mon Sep 17 00:00:00 2001 From: DanielRamirezGe Date: Thu, 7 Jan 2021 23:57:29 -0600 Subject: [PATCH] panel admin --- package-lock.json | 8 +++ package.json | 1 + routes/actualizarLibro.js | 87 +++++++++++++++----------------- routes/agregarImagenCarrusel.js | 6 +-- routes/agregarLibro.js | 73 ++++++++++++++------------- routes/eliminarImagenCarrusel.js | 2 +- routes/eliminarLibro.js | 54 +++++++++++++++----- routes/getLibros.js | 9 ++-- routes/getTodosLibros.js | 32 ++++++++++++ routes/index.js | 2 + 10 files changed, 170 insertions(+), 104 deletions(-) create mode 100644 routes/getTodosLibros.js diff --git a/package-lock.json b/package-lock.json index 99af825..6d865e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -362,6 +362,14 @@ "vary": "~1.1.2" } }, + "express-fileupload": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.2.0.tgz", + "integrity": "sha512-oe4WpKcSppXnl5peornawWUa6tKmIc1/kJxMNRGJR1A0v4zyLL6VsFR6wZ8P2a4Iq3aGx8xae3Vlr+MOMQhFPw==", + "requires": { + "busboy": "^0.3.1" + } + }, "finalhandler": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz", diff --git a/package.json b/package.json index fe9ecba..0066ed6 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", + "express-fileupload": "^1.2.0", "mysql": "^2.18.1", "node-xlsx": "^0.15.0", "nodemailer": "^6.4.13", diff --git a/routes/actualizarLibro.js b/routes/actualizarLibro.js index 1f1fbed..bafae46 100644 --- a/routes/actualizarLibro.js +++ b/routes/actualizarLibro.js @@ -1,17 +1,15 @@ -const connection = require('../conf/connection.js'); -const express = require('express') -var app = express(); -var xlsx = require('node-xlsx'); -var fs = require('fs'); -var busboy = require('connect-busboy'); +const connection = require('../conf/connection.js');; +const express = require('express'); +const fileUpload = require('express-fileupload'); var path = require('path'); - -const readXlsxFile = require('read-excel-file/node'); -const { type } = require('os'); +const nodemailer = require("nodemailer"); +const fs = require("fs"); +const { commit } = require('../conf/connection.js'); +var app = express() +app.use(fileUpload()); -app.use(busboy()); function query(consulta) { return new Promise(function(resolve, reject) { @@ -20,10 +18,11 @@ function query(consulta) { resolve(results); }); }) + } app.post('/actualizarLibro', async(req, res) => { - + console.log('entro a actulizar'); let data = req.body; if(typeof(data.idLibro) === 'undefined'){ res.status(400).json({error: true, msj: 'Falta el Id del libro como parametro'}); @@ -63,7 +62,7 @@ app.post('/actualizarLibro', async(req, res) => { } if ( typeof(data.edicion) !== 'undefined'){ stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; - stringUpdate += ` impresion = '${data.impresion}'`; + stringUpdate += ` impresion = '${data.edicion}'`; con++; } if ( typeof(data.year) !== 'undefined'){ @@ -84,48 +83,46 @@ app.post('/actualizarLibro', async(req, res) => { stringUpdate += ` where idLibro = ${data.idLibro};`; try{ - await query(stringUpdate); + if(con>0) + await query(stringUpdate); } catch (error){ + console.log(error); res.status(400).json({error: true, msj: 'Error al actualizar en la base de datos '}); return; } + - req.pipe(req.busboy); - req.busboy.on('file',async function (fieldname, file, filename) { - try{ - - if(fieldname === 'imagen'){ - stringUpdate = `Update Imagen ruta = ${filename} where idLibro = ${data.idLibro};`; - query(stringUpdate).then( ()=>{ - let saveTo = path.join('.', `/routes/imagenLibro/${filename}`); - file.pipe(fs.createWriteStream(saveTo)).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar la imagen del libro', desc: error}); - }) - }).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar la ruta del libro en la BD', desc: error}); - }); - } - if(fieldname === 'pdf'){ - stringUpdate = `Update Visualizacion ruta = ${filename} where idLibro = ${data.idLibro};`; - query(stringUpdate).then( ()=>{ - let saveTo = path.join('.', `/routes/pdf/${filename}`); - file.pipe(fs.createWriteStream(saveTo)).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar el pdf del libro', desc: error}); - }) - }).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar la ruta del pdf del libro en la BD', desc: error}); - }); + if (req.files !== null && req.files.imagen !== undefined) { + let imagen = req.files.imagen; + console.log(imagen); + let stringUpdate =`Update Imagen set ruta = "${imagen.name}" where idLibro = ${data.idLibro};`; + console.log(stringUpdate) + await query(stringUpdate); + imagen.mv( path.join('.', `/routes/imagenLibro/${imagen.name}`), function(err) { + if (err){ + console.log(err); + return res.status(400).json({ error: true, msj: "Error al guardar la imagen del libro" }); } - res.status(200).json({error: false, msj: 'Actualizacion terminada'}); - return; - } - catch(error){ - console.log(error); - } - }); + }); + } + + if (req.files !== null && req.files.pdf !== undefined) { + let pdf = req.files.pdf; + stringUpdate = `Update Visualizacion set ruta = "${pdf.name}" where idLibro = ${data.idLibro};`; + await query(stringUpdate); + pdf.mv( path.join('.', `/routes/pdf/${pdf.name}`), function(err) { + if (err) { + console.log(err); + return res.status(400).json({ error: true, msj: "Error al guardar el pdf del libro" }); + } + }); + } + res.status(200).json({error: false, msj: 'Se agrego con exito el libro'}); + return; } catch(error){ + console.log(error); res.status(400).json({error: true, msj: 'Sucedio un error al Actualizar el libro', desc: error}); return; } diff --git a/routes/agregarImagenCarrusel.js b/routes/agregarImagenCarrusel.js index 2e7395a..96be98f 100644 --- a/routes/agregarImagenCarrusel.js +++ b/routes/agregarImagenCarrusel.js @@ -4,6 +4,7 @@ var fs = require('fs'); var path = require('path'); app.post('/agregarImagenCarrusel', async(req, res) => { + console.log(req.body); try{ req.pipe(req.busboy); req.busboy.on('file',async function (fieldname, file, filename) { @@ -12,7 +13,8 @@ app.post('/agregarImagenCarrusel', async(req, res) => { file.pipe(fs.createWriteStream(saveTo)); } catch(error){ - console.log(error); + console.log(error);res.status(400).json({error: true, msj: `No se pudo guardar la imagen`}); + return; } }); res.status(200).json({error: false, msj: `Se inserto con exito`}); @@ -23,7 +25,5 @@ app.post('/agregarImagenCarrusel', async(req, res) => { res.status(400).json({error: true, msj: `No se pudo cargar el libro`}); return; } - - }); module.exports = app diff --git a/routes/agregarLibro.js b/routes/agregarLibro.js index 349b00d..bc17f67 100644 --- a/routes/agregarLibro.js +++ b/routes/agregarLibro.js @@ -1,12 +1,14 @@ -const connection = require('../conf/connection.js'); -const express = require('express') -var app = express(); -var fs = require('fs'); -var busboy = require('connect-busboy'); +const connection = require('../conf/connection.js');; +const express = require('express'); +const fileUpload = require('express-fileupload'); var path = require('path'); +const nodemailer = require("nodemailer"); +const fs = require("fs"); +const { commit } = require('../conf/connection.js'); +var app = express() +app.use(fileUpload()); -app.use(busboy()); function query(consulta) { return new Promise(function(resolve, reject) { @@ -34,8 +36,12 @@ function corregir(cad){ } app.post('/agregarLibro', async(req, res) => { - + console.log(req.files); + console.log(req.body); let data = req.body; + console.log(data) + for(d in data) + console.log(d, data[d ]); try{ let titulo = corregir(data.titulo); @@ -58,45 +64,40 @@ app.post('/agregarLibro', async(req, res) => { idLibro = sqlAnswer[0].idLibro; } catch (error){ + console.log(error); res.status(400).json({error: true, mensaje: 'Ocurrio un error al guardar en la BD', descripcion: error}); return; } - - req.pipe(req.busboy); - req.busboy.on('file',async function (fieldname, file, filename) { - - if(fieldname === 'imagen'){ - stringUpdate = `INSERT INTO Imagen values (null, ${idLibro}, '${fieldname}' )`; - query(stringUpdate).then( ()=>{ - let saveTo = path.join('.', `/routes/imagenLibro/${filename}`); - file.pipe(fs.createWriteStream(saveTo)).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar la imagen del libro', desc: error}); - }) - }).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar la ruta del libro en la BD', desc: error}); - }); - } - if(fieldname === 'pdf'){ - stringUpdate = `INSERT INTO Visualizacion values (null, ${idLibro}, '${fieldname}' );`; - query(stringUpdate).then( ()=>{ - let saveTo = path.join('.', `/routes/pdf/${filename}`); - file.pipe(fs.createWriteStream(saveTo)).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar el pdf del libro', desc: error}); - }) - }).catch(error =>{ - res.status(400).json({error: true, msj: 'No se pudo guardar la ruta del pdf del libro en la BD', desc: error}); - }); + if (req.files.imagen !== undefined) { + let imagen = req.files.imagen; + console.log(imagen); + let stringUpdate = `INSERT INTO Imagen values (null, ${idLibro}, '${imagen.name}' )`; + console.log(stringUpdate) + await query(stringUpdate); + imagen.mv( path.join('.', `/routes/imagenLibro/${imagen.name}`), function(err) { + if (err) return res.status(400).json({ error: true, msj: "Error al guardar la imagen del libro" }); + }); + } + + if (req.files.pdf !== undefined) { + let pdf = req.files.pdf; + stringUpdate = `INSERT INTO Visualizacion values (null, ${idLibro}, '${pdf.name}' );`; + await query(stringUpdate); + pdf.mv( path.join('.', `/routes/pdf/${pdf.name}`), function(err) { + if (err) return res.status(400).json({ error: true, msj: "Error al guardar el pdf del libro" }); + }); + res.status(200).json({error: false, msj: 'Se agrego con exito el libro'}); + return; } - }); - - res.status(200).json({error: false, msj: 'Se agrego con exito el libro'}); - return; } catch (error){ + console.log(error); res.status(400).json({error: true, msj: `No se pudo agregar el libro`, desc: error}); console.log(`Ocurrio un error al insertar en la base de datos msj: ${error}`); return; } + res.status(200).json({error: false, msj: 'Se agrego con exito el libro'}); + return; }); module.exports = app; diff --git a/routes/eliminarImagenCarrusel.js b/routes/eliminarImagenCarrusel.js index 8934c2c..09de611 100644 --- a/routes/eliminarImagenCarrusel.js +++ b/routes/eliminarImagenCarrusel.js @@ -19,7 +19,7 @@ app.post('/eliminarImagenCarrusel', async(req, res) => { return; } catch(error){ - console.log(`ESte es el error : ${error}`); + console.log(`Este es el error : ${error}`); res.status(400).json({error: true, msj: 'No se pudo eliminar la imagen', desc: error}); return; } diff --git a/routes/eliminarLibro.js b/routes/eliminarLibro.js index 71abc17..5652207 100644 --- a/routes/eliminarLibro.js +++ b/routes/eliminarLibro.js @@ -1,8 +1,24 @@ -const { query } = require('express'); -const express = require('express') -var app = express(); -var fs = require('fs'); + +const connection = require('../conf/connection.js');; +const express = require('express'); +const fileUpload = require('express-fileupload'); var path = require('path'); +const nodemailer = require("nodemailer"); +const fs = require("fs"); +const { commit } = require('../conf/connection.js'); +var app = express() +app.use(fileUpload()); + + +function query(consulta) { + return new Promise(function(resolve, reject) { + connection.query(`${consulta}`, (error, results, files) => { + if (error) reject(error); + resolve(results); + }); + }) + +} app.post('/eliminarLibro', async(req, res) => { @@ -10,22 +26,34 @@ app.post('/eliminarLibro', async(req, res) => { try{ let sqlSelectImage = `SELECT ruta from Imagen where idLibro = ${data.idLibro};`; - let imageName = await query(sqlSelectImage); + let imageName = await query(sqlSelectImage); let sqlSelectVizualizacion= `SELECT ruta from Visualizacion where idLibro = ${data.idLibro};`; let vizualizacionName = await query(sqlSelectVizualizacion); - - let sqlDeleteStrign = `DELETE FROM PdfLibro where id=${data.idLibro};`; + + let sqlDeleteStrign = `DELETE FROM PdfLibro where idLibro=${data.idLibro};`; await query(sqlDeleteStrign); - sqlDeleteStrign = `DELETE FROM Imagen where id=${data.idLibro};`; + sqlDeleteStrign = `DELETE FROM Imagen where idLibro=${data.idLibro};`; await query(sqlDeleteStrign); - sqlDeleteStrign = `DELETE FROM Visualizacion where id=${data.idLibro};`; + sqlDeleteStrign = `DELETE FROM Visualizacion where idLibro=${data.idLibro};`; await query(sqlDeleteStrign); - sqlDeleteStrign = `DELETE FROM Libro where id=${data.idLibro};`; + sqlDeleteStrign = `DELETE FROM Libro where idLibro=${data.idLibro};`; await query(sqlDeleteStrign); - let rutaVizualizavcion = path.join('.', `/routes/pdf/${VizualizacionName}`); - fs.unlinkSync(rutaVizualizavcion); + let rutaVizualizavcion = path.join('.', `/routes/pdf/${vizualizacionName}`); + try{ + vizualizacionName = vizualizacionName[0].ruta; + fs.unlinkSync(rutaVizualizavcion); + } + catch(error){ + console.log(error); + } let rutaImage = path.join('.', `/routes/imagenLibro/${imageName}`); - fs.unlinkSync(rutaImage); + try{ + imageName = imageName[0].ruta; + fs.unlinkSync(rutaImage); + } + catch(error){ + console.log(error); + } res.status(200).json({error: false, msj: 'Imagen Eliminada con exito'}); return; } diff --git a/routes/getLibros.js b/routes/getLibros.js index 8042bf1..db6854f 100644 --- a/routes/getLibros.js +++ b/routes/getLibros.js @@ -19,12 +19,9 @@ function query(consulta) { app.get('/getLibros', async(req, res) => { try{ - let sqlString = `SELECT idLibro, titulo, categoria, autor FROM Libro;`; - console.log(` Esta es la cadena : ${sqlString}`); - let respuestaSql = await query(sqlString); - - - + let sqlString = `SELECT idLibro, titulo, categoria, autor FROM Libro;`; + console.log(` Esta es la cadena : ${sqlString}`); + let respuestaSql = await query(sqlString); res.status(200).json({error:false, msj: 'exito', data: respuestaSql}) } catch(error){ diff --git a/routes/getTodosLibros.js b/routes/getTodosLibros.js new file mode 100644 index 0000000..dad2c65 --- /dev/null +++ b/routes/getTodosLibros.js @@ -0,0 +1,32 @@ +const connection = require('../conf/connection.js'); +const express = require('express') +var app = express(); + + + + +function query(consulta) { + return new Promise(function(resolve, reject) { + connection.query(`${consulta}`, (error, results, files) => { + if (error) reject(error); + resolve(results); + }); + }) +} + + + +app.get('/getTodosLibros', async(req, res) => { + + try{ + let sqlString = `SELECT * FROM Libro ORDER BY year DESC;`; + let respuestaSql = await query(sqlString); + res.status(200).json({error:false, msj: 'exito', data: respuestaSql}) + } + catch(error){ + res.status(400).json({error: true, msj: 'No se pudo obtener los libros similares', desc: error}); + return; + } + +}); +module.exports = app; diff --git a/routes/index.js b/routes/index.js index 9a73454..547315d 100644 --- a/routes/index.js +++ b/routes/index.js @@ -13,7 +13,9 @@ app.use(require('./temaLibro')); app.use(require('./getPDF')); app.use(require('./email')); app.use(require('./eliminarImagenCarrusel')); +app.use(require('./agregarImagenCarrusel')); app.use(require('./actualizarLibro')); +app.use(require('./getTodosLibros')); app.use(require('./agregarLibro')); app.use(require('./eliminarLibro')); module.exports = app; \ No newline at end of file