140 lines
5.4 KiB
JavaScript
140 lines
5.4 KiB
JavaScript
const connection = require('../conf/connection');
|
|
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);
|
|
});
|
|
})
|
|
}
|
|
|
|
function twoDigits(d) {
|
|
if (0 <= d && d < 10) return '0' + d.toString();
|
|
if (-10 < d && d < 0) return '-0' + (-1 * d).toString();
|
|
return d.toString();
|
|
}
|
|
|
|
function dataJsToMysql(dat) {
|
|
console.log(dat);
|
|
console.log(dat.getUTCFullYear());
|
|
let ne = dat.getUTCFullYear() + '-' + twoDigits(1 + dat.getUTCMonth()) + '-' + twoDigits(dat.getUTCDate()) + ' ' + twoDigits(dat.getUTCHours()) + ':' + twoDigits(dat.getUTCMinutes()) + ':' + twoDigits(dat.getUTCSeconds());
|
|
console.log(ne);
|
|
return ne;
|
|
}
|
|
app.post('/datos_proyecto_1', async(req, res) => {
|
|
|
|
console.log(req.body);
|
|
|
|
if (req.body.nombre_proyecto == undefined || req.body.rfc == undefined || req.body.nombre_responsable == undefined || req.body.disciplina == undefined || req.body.objetivo_general == undefined ||
|
|
req.body.campo_conocimiento == undefined || req.body.linea_investigacion == undefined || req.body.objetivo_particular == undefined) {
|
|
res.status(400).json({ error: true, msj: 'No estan los datos que requiere la appi' });
|
|
return;
|
|
}
|
|
|
|
req.body.nombre_proyecto = req.body.nombre_proyecto.replace(/'/g, "''");
|
|
req.body.rfc = req.body.rfc.replace(/'/g, "''");
|
|
req.body.nombre_responsable = req.body.nombre_responsable.replace(/'/g, "''");
|
|
req.body.disciplina = req.body.disciplina.replace(/'/g, "''");
|
|
req.body.objetivo_general = req.body.objetivo_general.replace(/'/g, "''");
|
|
req.body.linea_investigacion = req.body.linea_investigacion.replace(/'/g, "''");
|
|
for (let i = 0; i < req.body.objetivo_particular.length; i++) {
|
|
req.body.objetivo_particular[i] = req.body.objetivo_particular[i].replace(/'/g, "''");
|
|
console.log(req.body.objetivo_particular[i]);
|
|
}
|
|
|
|
|
|
|
|
let copia;
|
|
try {
|
|
let verificar = `select id_proyecto from proyecto where nombre_proyecto='${req.body.nombre_proyecto}';`;
|
|
copia = query(verificar);
|
|
copia.catch(function(reason) {
|
|
console.log(reason);
|
|
res.status(400).json({ error: true, msj: 'Error al verificar el nombre del proyecto' });
|
|
return;
|
|
})
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(400).json({ error: true, msj: 'Error al verificar el nombre del proyecto' });
|
|
return;
|
|
}
|
|
let unico;
|
|
await copia.then(function(v) {
|
|
//console.log(v);
|
|
unico = v.length
|
|
})
|
|
if (unico >= 1) {
|
|
console.log('ya existe el nombre');
|
|
res.status(400).json({ error: true, msj: 'El nombre del proyecto ya existe en la base de datos' });
|
|
return;
|
|
}
|
|
|
|
let respuesta;
|
|
let creacion = new Date();
|
|
creacion = dataJsToMysql(creacion);
|
|
|
|
console.log(creacion);
|
|
try {
|
|
// insert into proyecto (id_proyecto,nombre_proyecto, rfc_responsable, nombre_responsable, disciplina, objetivo_general) values(null, 'prueba', 'prueba', 'prueba','prueba', 'prueba');
|
|
let cad = `insert into proyecto (id_proyecto,nombre_proyecto, rfc_responsable, nombre_responsable, disciplina, objetivo_general,fecha_creacion, fecha_modificiacion, id_usuario, linea_investigacion) values(null, '${req.body.nombre_proyecto}', '${req.body.rfc}', '${req.body.nombre_responsable}',
|
|
'${req.body.disciplina}', '${req.body.objetivo_general}', '${creacion}' ,'${creacion}', ${req.body.id_usuario}, '${req.body.linea_investigacion}');`;
|
|
respuesta = query(cad);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(400).json({ error: true, msj: 'Error al insertar en la base de datos' });
|
|
return;
|
|
}
|
|
let respuesta_id_proyecto;
|
|
try {
|
|
let cad = `select id_proyecto from proyecto where nombre_proyecto='${req.body.nombre_proyecto}';`;
|
|
respuesta_id_proyecto = query(cad);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(400).json({ error: true, msj: 'Error al obtener el id del proyecto' });
|
|
return;
|
|
}
|
|
let id_proyecto;
|
|
await respuesta_id_proyecto.then(function(v) {
|
|
// console.log(v);
|
|
id_proyecto = v[0].id_proyecto;
|
|
});
|
|
//campo de conocimiento array[]
|
|
await req.body.campo_conocimiento.forEach(element => {
|
|
let cad = `insert into proyecto_campo values (null, ${id_proyecto}, ${element} ); `;
|
|
try {
|
|
respuesta = query(cad);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(400).json({ error: true, msj: 'Error al insertar en la base de datos - campos' });
|
|
return;
|
|
}
|
|
});
|
|
//linea de investigacion array[]
|
|
|
|
// objetivo particular array[]
|
|
let i = 1;
|
|
await req.body.objetivo_particular.forEach(element => {
|
|
let cad = `insert into proyecto_objetivo values ( null, ${id_proyecto}, '${element}' ); `;
|
|
try {
|
|
respuesta = query(cad);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(400).json({ error: true, msj: 'Error al insertar en la base de datos - particular' });
|
|
return;
|
|
}
|
|
if (i == req.body.objetivo_particular.length) {
|
|
res.status(200).json({ error: false, msj: 'exito', id: id_proyecto });
|
|
return;
|
|
}
|
|
i++;
|
|
});
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
module.exports = app; |