Files
PortalApi/routes/actualizarLibro.js
T
2021-01-06 04:31:31 -06:00

138 lines
5.2 KiB
JavaScript

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;