agregar endpoint panel admin

This commit is contained in:
2021-01-06 04:31:31 -06:00
parent a8025afd4c
commit 90d07aa1c2
9 changed files with 348 additions and 4 deletions
+6 -1
View File
@@ -1,2 +1,7 @@
node_modules
.env
.env
routes/imagenesCarrusel
routes/imagenLibro
routes/pdf
+137
View File
@@ -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;
+29
View File
@@ -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
+105
View File
@@ -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<cad.length; i++){
if(i!==0 && cad[i]!=="\n" )
nuevocad = nuevocad + cad[i];
else
if(cad[i]!== "\n")
nuevocad = cad[i]
}
return nuevocad;
}
app.post('/agregarLibro', async(req, res) => {
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;
-2
View File
@@ -36,8 +36,6 @@ function corregir(cad){
}
return nuevocad;
}
+28
View File
@@ -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
+39
View File
@@ -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
Binary file not shown.

Before

Width:  |  Height:  |  Size: 153 KiB

+4 -1
View File
@@ -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;