change routes of update Lib
This commit is contained in:
Generated
+82
@@ -27,6 +27,11 @@
|
||||
"printj": "~1.1.0"
|
||||
}
|
||||
},
|
||||
"append-field": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz",
|
||||
"integrity": "sha1-HjRA6RXwsSA9I3SOeO3XubW0PlY="
|
||||
},
|
||||
"array-flatten": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
||||
@@ -189,6 +194,17 @@
|
||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
|
||||
},
|
||||
"concat-stream": {
|
||||
"version": "1.6.2",
|
||||
"resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz",
|
||||
"integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==",
|
||||
"requires": {
|
||||
"buffer-from": "^1.0.0",
|
||||
"inherits": "^2.0.3",
|
||||
"readable-stream": "^2.2.2",
|
||||
"typedarray": "^0.0.6"
|
||||
}
|
||||
},
|
||||
"connect-busboy": {
|
||||
"version": "0.0.2",
|
||||
"resolved": "https://registry.npmjs.org/connect-busboy/-/connect-busboy-0.0.2.tgz",
|
||||
@@ -644,6 +660,62 @@
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
},
|
||||
"multer": {
|
||||
"version": "1.4.2",
|
||||
"resolved": "https://registry.npmjs.org/multer/-/multer-1.4.2.tgz",
|
||||
"integrity": "sha512-xY8pX7V+ybyUpbYMxtjM9KAiD9ixtg5/JkeKUTD6xilfDv0vzzOFcCp4Ljb1UU3tSOM3VTZtKo63OmzOrGi3Cg==",
|
||||
"requires": {
|
||||
"append-field": "^1.0.0",
|
||||
"busboy": "^0.2.11",
|
||||
"concat-stream": "^1.5.2",
|
||||
"mkdirp": "^0.5.1",
|
||||
"object-assign": "^4.1.1",
|
||||
"on-finished": "^2.3.0",
|
||||
"type-is": "^1.6.4",
|
||||
"xtend": "^4.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"busboy": {
|
||||
"version": "0.2.14",
|
||||
"resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz",
|
||||
"integrity": "sha1-bCpiLvz0fFe7vh4qnDetNseSVFM=",
|
||||
"requires": {
|
||||
"dicer": "0.2.5",
|
||||
"readable-stream": "1.1.x"
|
||||
}
|
||||
},
|
||||
"dicer": {
|
||||
"version": "0.2.5",
|
||||
"resolved": "https://registry.npmjs.org/dicer/-/dicer-0.2.5.tgz",
|
||||
"integrity": "sha1-WZbAhrszIYyBLAkL3cCc0S+stw8=",
|
||||
"requires": {
|
||||
"readable-stream": "1.1.x",
|
||||
"streamsearch": "0.1.2"
|
||||
}
|
||||
},
|
||||
"isarray": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz",
|
||||
"integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8="
|
||||
},
|
||||
"readable-stream": {
|
||||
"version": "1.1.14",
|
||||
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",
|
||||
"integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=",
|
||||
"requires": {
|
||||
"core-util-is": "~1.0.0",
|
||||
"inherits": "~2.0.1",
|
||||
"isarray": "0.0.1",
|
||||
"string_decoder": "~0.10.x"
|
||||
}
|
||||
},
|
||||
"string_decoder": {
|
||||
"version": "0.10.31",
|
||||
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz",
|
||||
"integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ="
|
||||
}
|
||||
}
|
||||
},
|
||||
"mysql": {
|
||||
"version": "2.18.1",
|
||||
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
|
||||
@@ -918,6 +990,11 @@
|
||||
"mime-types": "~2.1.24"
|
||||
}
|
||||
},
|
||||
"typedarray": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
|
||||
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c="
|
||||
},
|
||||
"unpipe": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
|
||||
@@ -982,6 +1059,11 @@
|
||||
"version": "0.0.27",
|
||||
"resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.27.tgz",
|
||||
"integrity": "sha512-fg03WRxtkCV6ohClePNAECYsmpKKTv5L8y/X3Dn1hQrec3POx2jHZ/0P2qQ6HvsrU1BmeqXcof3NGGueG6LxwQ=="
|
||||
},
|
||||
"xtend": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
||||
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
"dotenv": "^8.2.0",
|
||||
"express": "^4.17.1",
|
||||
"express-fileupload": "^1.2.0",
|
||||
"multer": "^1.4.2",
|
||||
"mysql": "^2.18.1",
|
||||
"node-xlsx": "^0.15.0",
|
||||
"nodemailer": "^6.4.13",
|
||||
|
||||
+186
-97
@@ -1,16 +1,11 @@
|
||||
|
||||
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) => {
|
||||
@@ -18,113 +13,207 @@ function query(consulta) {
|
||||
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('/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'});
|
||||
return;
|
||||
}
|
||||
try{
|
||||
let stringUpdate = `Update Libro set `;
|
||||
let con=0;
|
||||
if ( typeof(data.titulo) !== 'undefined'){
|
||||
stringUpdate += `titulo = '${data.titulo}'`;
|
||||
con++;
|
||||
if(req.body.type==='addLibro'){
|
||||
|
||||
if (req.files===null)
|
||||
{
|
||||
res.status(400).json({error: true, msj: `No se pudo cargar los archivos de imagen y pdf`});
|
||||
return;
|
||||
}
|
||||
if ( typeof(data.autor) !== 'undefined'){
|
||||
stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` autor = '${data.autor}'`;
|
||||
con++;
|
||||
if (req.files.imagen===null)
|
||||
{
|
||||
res.status(400).json({error: true, msj: `No se pudo cargar el archivo de la imagen`});
|
||||
return;
|
||||
}
|
||||
if ( typeof(data.categoria) !== 'undefined'){
|
||||
stringUpdate = (con>1) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` categoria = '${data.categoria}'`;
|
||||
con++;
|
||||
if (req.files.pdf===null)
|
||||
{
|
||||
res.status(400).json({error: true, msj: `No se pudo cargar el archivo del pdf`});
|
||||
return;
|
||||
}
|
||||
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.edicion}'`;
|
||||
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{
|
||||
if(con>0)
|
||||
await query(stringUpdate);
|
||||
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){
|
||||
console.log(error);
|
||||
res.status(400).json({error: true, mensaje: 'Ocurrio un error al guardar en la BD', descripcion: error});
|
||||
return;
|
||||
}
|
||||
if (req.files!==null && req.files.imagen !== null) {
|
||||
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!==null && req.files.pdf !== null) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
catch (error){
|
||||
console.log(error);
|
||||
res.status(400).json({error: true, msj: 'Error al actualizar en la base de datos '});
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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" });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
else{
|
||||
console.log('entro a actulizar: ');
|
||||
console.log('archivos', req.files);
|
||||
console.log('datos', 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>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` autor = '${data.autor}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.categoria) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` categoria = '${data.categoria}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.paginas) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` paginas = '${data.paginas}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.isbn) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` isbn = '${data.isbn}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.precio) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` precio = '${data.precio}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.edicion) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` impresion = '${data.edicion}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.year) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` year = '${data.year}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.descripcion) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` descripcion = '${data.descripcion}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.version) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` version = '${data.version}'`;
|
||||
}
|
||||
|
||||
stringUpdate += ` where idLibro = ${data.idLibro};`;
|
||||
|
||||
try{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
let answerUpdata = await query(stringUpdate);
|
||||
console.log(answerUpdata);
|
||||
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" });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
const connection = require('../conf/connection.js');;
|
||||
const express = require('express');
|
||||
const fileUpload = require('express-fileupload');
|
||||
const path = require('path');
|
||||
const fs = require("fs");
|
||||
const 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('/addLibro', async(req, res) => {
|
||||
req.pipe(req.busboy);
|
||||
req.busboy.on('file',async function (fieldname, file, filename) {
|
||||
console.log(fieldname, file, filename);
|
||||
/*try{
|
||||
let saveTo = path.join('.', `/routes/imagenesCarrusel/${filename}`);
|
||||
file.pipe(fs.createWriteStream(saveTo));
|
||||
}
|
||||
catch(error){
|
||||
console.log(error);res.status(400).json({error: true, msj: `No se pudo guardar la imagen`});
|
||||
return;
|
||||
}*/
|
||||
});
|
||||
console.log('entro a addlibro');
|
||||
console.log('archivos', req.files);
|
||||
console.log('datos', req.body)
|
||||
let data = req.body;
|
||||
res.send({ss:'ss'});
|
||||
/*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>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` autor = '${data.autor}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.categoria) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` categoria = '${data.categoria}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.paginas) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` paginas = '${data.paginas}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.isbn) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` isbn = '${data.isbn}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.precio) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` precio = '${data.precio}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.edicion) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` impresion = '${data.edicion}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.year) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` year = '${data.year}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.descripcion) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` descripcion = '${data.descripcion}'`;
|
||||
con++;
|
||||
}
|
||||
if ( typeof(data.version) !== 'undefined'){
|
||||
stringUpdate = (con>0) ? `${stringUpdate},`: stringUpdate;
|
||||
stringUpdate += ` version = '${data.version}'`;
|
||||
}
|
||||
|
||||
stringUpdate += ` where idLibro = ${data.idLibro};`;
|
||||
|
||||
try{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
let answerUpdata = await query(stringUpdate);
|
||||
console.log(answerUpdata);
|
||||
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" });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}*/
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
|
||||
|
||||
|
||||
+20
-7
@@ -36,12 +36,25 @@ function corregir(cad){
|
||||
}
|
||||
|
||||
app.post('/agregarLibro', async(req, res) => {
|
||||
console.log(req.files);
|
||||
console.log(req.body);
|
||||
console.log('archivos: ',req.files);
|
||||
// console.log(req.body);
|
||||
let data = req.body;
|
||||
console.log(data)
|
||||
for(d in data)
|
||||
console.log(d, data[d ]);
|
||||
|
||||
if (req.files===null)
|
||||
{
|
||||
res.status(400).json({error: true, msj: `No se pudo cargar los archivos de imagen y pdf`});
|
||||
return;
|
||||
}
|
||||
if (req.files.imagen===null)
|
||||
{
|
||||
res.status(400).json({error: true, msj: `No se pudo cargar el archivo de la imagen`});
|
||||
return;
|
||||
}
|
||||
if (req.files.pdf===null)
|
||||
{
|
||||
res.status(400).json({error: true, msj: `No se pudo cargar el archivo del pdf`});
|
||||
return;
|
||||
}
|
||||
try{
|
||||
|
||||
let titulo = corregir(data.titulo);
|
||||
@@ -68,7 +81,7 @@ app.post('/agregarLibro', async(req, res) => {
|
||||
res.status(400).json({error: true, mensaje: 'Ocurrio un error al guardar en la BD', descripcion: error});
|
||||
return;
|
||||
}
|
||||
if (req.files.imagen !== undefined) {
|
||||
if (req.files!==null && req.files.imagen !== null) {
|
||||
let imagen = req.files.imagen;
|
||||
console.log(imagen);
|
||||
let stringUpdate = `INSERT INTO Imagen values (null, ${idLibro}, '${imagen.name}' )`;
|
||||
@@ -79,7 +92,7 @@ app.post('/agregarLibro', async(req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (req.files.pdf !== undefined) {
|
||||
if (req.files!==null && req.files.pdf !== null) {
|
||||
let pdf = req.files.pdf;
|
||||
stringUpdate = `INSERT INTO Visualizacion values (null, ${idLibro}, '${pdf.name}' );`;
|
||||
await query(stringUpdate);
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ app.get('/getLibros', async(req, res) => {
|
||||
|
||||
try{
|
||||
let sqlString = `SELECT idLibro, titulo, categoria, autor FROM Libro;`;
|
||||
console.log(` Esta es la cadena : ${sqlString}`);
|
||||
//console.log(` Esta es la cadena : ${sqlString}`);
|
||||
let respuestaSql = await query(sqlString);
|
||||
res.status(200).json({error:false, msj: 'exito', data: respuestaSql})
|
||||
}
|
||||
|
||||
@@ -18,4 +18,5 @@ app.use(require('./actualizarLibro'));
|
||||
app.use(require('./getTodosLibros'));
|
||||
app.use(require('./agregarLibro'));
|
||||
app.use(require('./eliminarLibro'));
|
||||
app.use(require('./addLibro'));
|
||||
module.exports = app;
|
||||
@@ -19,27 +19,22 @@ function query(consulta) {
|
||||
|
||||
|
||||
app.get('/libroImagen', async(req, res) => {
|
||||
console.log(`Entro a imagenes`);
|
||||
|
||||
let data = req.query;
|
||||
if( typeof( data.idLibro ) === 'undefined' ){
|
||||
res.status(400).json({error: true, msj: 'Falta el ID del libro como parametro'});
|
||||
return;
|
||||
}
|
||||
console.log(` Este es el id: ${data.idLibro}`);
|
||||
//console.log(` Este es el id: ${data.idLibro}`);
|
||||
try{
|
||||
let sqlString = `SELECT * from Imagen where idLibro=${data.idLibro}`;
|
||||
console.log(` Esta es la cadena : ${sqlString}`);
|
||||
//console.log(` Esta es la cadena : ${sqlString}`);
|
||||
let respuestaSql = await query(sqlString);
|
||||
if(respuestaSql.length === 0 ){
|
||||
console.log(`No se encontro el libro msj:`);
|
||||
//console.log(`No se encontro el libro msj:`);
|
||||
res.status(400).json({error: true, msj: 'No se encontro el libro solicitado'});
|
||||
return;
|
||||
}
|
||||
respuestaSql = respuestaSql[0];
|
||||
console.log(` esta es la respuesta ${respuestaSql}`);
|
||||
for(let key in respuestaSql)
|
||||
console.log(` __ ${key} : ${respuestaSql[key]}`);
|
||||
let img = fs.readFileSync(path.join('.', `/routes/imagenLibro/${respuestaSql.ruta}`));
|
||||
res.writeHead(200, {'Content-Type': 'image/jpg' });
|
||||
res.end(img, 'binary')
|
||||
|
||||
Reference in New Issue
Block a user