diff --git a/.gitignore b/.gitignore index 9b31034..d150cbe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ node_modules -.env \ No newline at end of file +.env +routes/imagenesCarrusel +routes/imagenLibro +routes/pdf + + diff --git a/routes/actualizarLibro.js b/routes/actualizarLibro.js new file mode 100644 index 0000000..1f1fbed --- /dev/null +++ b/routes/actualizarLibro.js @@ -0,0 +1,137 @@ + +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'); +var path = require('path'); + +const readXlsxFile = require('read-excel-file/node'); +const { type } = require('os'); + + +app.use(busboy()); + +function query(consulta) { + return new Promise(function(resolve, reject) { + connection.query(`${consulta}`, (error, results, files) => { + if (error) reject(error); + resolve(results); + }); + }) +} + +app.post('/actualizarLibro', async(req, res) => { + + let data = req.body; + if(typeof(data.idLibro) === 'undefined'){ + res.status(400).json({error: true, msj: 'Falta el Id del libro como parametro'}); + return; + } + try{ + let stringUpdate = `Update Libro set `; + let con=0; + if ( typeof(data.titulo) !== 'undefined'){ + stringUpdate += `titulo = '${data.titulo}'`; + con++; + } + if ( typeof(data.autor) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` autor = '${data.autor}'`; + con++; + } + if ( typeof(data.categoria) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` categoria = '${data.categoria}'`; + con++; + } + if ( typeof(data.paginas) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` paginas = '${data.paginas}'`; + con++; + } + if ( typeof(data.isbn) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` isbn = '${data.isbn}'`; + con++; + } + if ( typeof(data.precio) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` precio = '${data.precio}'`; + con++; + } + if ( typeof(data.edicion) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` impresion = '${data.impresion}'`; + con++; + } + if ( typeof(data.year) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` year = '${data.year}'`; + con++; + } + if ( typeof(data.descripcion) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` descripcion = '${data.descripcion}'`; + con++; + } + if ( typeof(data.version) !== 'undefined'){ + stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate; + stringUpdate += ` version = '${data.version}'`; + } + + stringUpdate += ` where idLibro = ${data.idLibro};`; + + try{ + await query(stringUpdate); + } + catch (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}); + }); + } + res.status(200).json({error: false, msj: 'Actualizacion terminada'}); + return; + } + catch(error){ + console.log(error); + } + }); + } + catch(error){ + res.status(400).json({error: true, msj: 'Sucedio un error al Actualizar el libro', desc: error}); + return; + } +}); + +module.exports = app; + + + diff --git a/routes/agregarImagenCarrusel.js b/routes/agregarImagenCarrusel.js new file mode 100644 index 0000000..2e7395a --- /dev/null +++ b/routes/agregarImagenCarrusel.js @@ -0,0 +1,29 @@ +const express = require('express') +var app = express(); +var fs = require('fs'); +var path = require('path'); + +app.post('/agregarImagenCarrusel', async(req, res) => { + try{ + req.pipe(req.busboy); + req.busboy.on('file',async function (fieldname, file, filename) { + try{ + let saveTo = path.join('.', `/routes/imagenesCarrusel/${filename}`); + file.pipe(fs.createWriteStream(saveTo)); + } + catch(error){ + console.log(error); + } + }); + res.status(200).json({error: false, msj: `Se inserto con exito`}); + return; + } + catch(error){ + console.log(`No se pudo guardar la imagen`); + 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 new file mode 100644 index 0000000..349b00d --- /dev/null +++ b/routes/agregarLibro.js @@ -0,0 +1,105 @@ + +const connection = require('../conf/connection.js'); +const express = require('express') +var app = express(); +var fs = require('fs'); +var busboy = require('connect-busboy'); +var path = require('path'); + +app.use(busboy()); + +function query(consulta) { + return new Promise(function(resolve, reject) { + connection.query(`${consulta}`, (error, results, files) => { + if (error) reject(error); + resolve(results); + }); + }) +} + + +function corregir(cad){ + var recor = String.fromCharCode(13); + cad = cad.split(recor); + let nuevocad=""; + for(let i=0; i { + + let data = req.body; + try{ + + let titulo = corregir(data.titulo); + let autor = corregir(data.autor); + let categoria = data.categoria; + let paginas = data.paginas; + let isbn = data.isbn; + let precio = data.precio; + let edicion = corregir(data.edicion); + let year = data.year; + let descripcion = corregir(data.descripcion); + let version = data.version; + let sqlInsertString = `INSERT INTO Libro values ( null, '${isbn}' , '${titulo}', '${categoria}', '${autor}', ${paginas}, '${edicion}', '${precio}', '${version}', '${descripcion}', ${year}); `; + let idLibro = null; + try{ + + await query(sqlInsertString); + let sqlString = `SELECT LAST_INSERT_ID() as idLibro;`; + let sqlAnswer = await query(sqlString); + idLibro = sqlAnswer[0].idLibro; + } + catch (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}); + }); + } + }); + + res.status(200).json({error: false, msj: 'Se agrego con exito el libro'}); + return; + } + catch (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; + } +}); + +module.exports = app; + + + diff --git a/routes/cargaLibro.js b/routes/cargaLibro.js index b51f347..8f8a8b0 100644 --- a/routes/cargaLibro.js +++ b/routes/cargaLibro.js @@ -36,8 +36,6 @@ function corregir(cad){ } return nuevocad; - - } diff --git a/routes/eliminarImagenCarrusel.js b/routes/eliminarImagenCarrusel.js new file mode 100644 index 0000000..8934c2c --- /dev/null +++ b/routes/eliminarImagenCarrusel.js @@ -0,0 +1,28 @@ +const express = require('express') +var app = express(); +var fs = require('fs'); +var path = require('path'); + +app.post('/eliminarImagenCarrusel', async(req, res) => { + + let data = req.body; + if( typeof( data.nombre ) === 'undefined' ){ + res.status(400).json({error: true, msj: 'Falta el nombre de la imagen del carrusel'}); + return; + } + console.log(` Este es el nombre: ${data.nombre}`); + try{ + + let ruta = path.join('.', `/routes/imagenesCarrusel/${data.nombre}`); + fs.unlinkSync(ruta); + res.status(200).json({error: false, msj: 'Imagen Eliminada con exito'}); + return; + } + catch(error){ + console.log(`ESte es el error : ${error}`); + res.status(400).json({error: true, msj: 'No se pudo eliminar la imagen', desc: error}); + return; + } + +}); +module.exports = app diff --git a/routes/eliminarLibro.js b/routes/eliminarLibro.js new file mode 100644 index 0000000..71abc17 --- /dev/null +++ b/routes/eliminarLibro.js @@ -0,0 +1,39 @@ +const { query } = require('express'); +const express = require('express') +var app = express(); +var fs = require('fs'); +var path = require('path'); + +app.post('/eliminarLibro', async(req, res) => { + + let data = req.body; + + try{ + let sqlSelectImage = `SELECT ruta from Imagen where idLibro = ${data.idLibro};`; + 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};`; + await query(sqlDeleteStrign); + sqlDeleteStrign = `DELETE FROM Imagen where id=${data.idLibro};`; + await query(sqlDeleteStrign); + sqlDeleteStrign = `DELETE FROM Visualizacion where id=${data.idLibro};`; + await query(sqlDeleteStrign); + sqlDeleteStrign = `DELETE FROM Libro where id=${data.idLibro};`; + await query(sqlDeleteStrign); + let rutaVizualizavcion = path.join('.', `/routes/pdf/${VizualizacionName}`); + fs.unlinkSync(rutaVizualizavcion); + let rutaImage = path.join('.', `/routes/imagenLibro/${imageName}`); + fs.unlinkSync(rutaImage); + res.status(200).json({error: false, msj: 'Imagen Eliminada con exito'}); + return; + } + catch(error){ + console.log(`Este es el error : ${error}`); + res.status(400).json({error: true, msj: 'No se pudo eliminar la libro', desc: error}); + return; + } + +}); +module.exports = app diff --git a/routes/imagenesCarrusel/imagen1.png b/routes/imagenesCarrusel/imagen1.png deleted file mode 100644 index 4494197..0000000 Binary files a/routes/imagenesCarrusel/imagen1.png and /dev/null differ diff --git a/routes/index.js b/routes/index.js index 7e3ff11..9a73454 100644 --- a/routes/index.js +++ b/routes/index.js @@ -12,5 +12,8 @@ app.use(require('./getLibros')); app.use(require('./temaLibro')); app.use(require('./getPDF')); app.use(require('./email')); - +app.use(require('./eliminarImagenCarrusel')); +app.use(require('./actualizarLibro')); +app.use(require('./agregarLibro')); +app.use(require('./eliminarLibro')); module.exports = app; \ No newline at end of file