Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created February 8, 2022 05:56
Show Gist options
  • Save jpalala/07bb08a71b2676adc70de855267b3f4f to your computer and use it in GitHub Desktop.
Save jpalala/07bb08a71b2676adc70de855267b3f4f to your computer and use it in GitHub Desktop.
modular nodejs notes
const getDB = require('../../bbdd/db');
const { formatDate, validate } = require('../../helpers');
const {
newSchemaEditExperience,
} = require('../../validations/newSchemaEditExperience');
const editExperience = async (req, res, next) => {
let connection;
let sqlExperience = 'SELECT * FROM experiences';
try {
connection = await getDB();
const { idExp } = req.params;
await validate(newSchemaEditExperience, req.body);
if (req.userAuth.rol !== 'admin') {
const error = new Error(
'No tienes permisos para editar la experiencia'
);
error.httpStatus = 401;
throw error;
}
let {
description,
name,
category,
sDate,
} = req.body;
const [experience] = await connection.query(
`
${sqlExperience} WHERE id = ?;
`,
[idExp]
);
description = description || experience[0].descripcion;
name = name || experience[0].nombre;
category = category || experience[0].categoria;
sDate = sDate || experience[0].fecha_creacion;
const now = new Date();
await connection.query(
`UPDATE notices SET descripcion = ?, nombre = ?, categoria = ?,
fecha_creacion = ?, modifiedAt = ? WHERE id = ?;`,
[description, name, category, sDate, formatDate(now), idExp]
);
res.send({
status: 200,
data: {
id: idExp,
description,
name,
category,
sDate,
modifiedAt: now,
},
});
} catch (error) {
next(error);
} finally {
if (connection) connection.release();
}
};
module.exports = editExperience;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment