back: DELETE /admin/functions/:id

This commit is contained in:
Divlo
2020-03-25 23:30:33 +01:00
parent cec6881308
commit 49da9d5d48
3 changed files with 25 additions and 3 deletions

View File

@ -1,4 +1,5 @@
const path = require('path');
const fs = require('fs');
const { validationResult } = require('express-validator');
const errorHandling = require('../assets/utils/errorHandling');
const { serverError } = require('../assets/config/errors');
@ -30,4 +31,23 @@ exports.postFunction = (req, res, next) => {
errorHandling(next, serverError);
}
});
}
exports.deleteFunction = async (req, res, next) => {
const { id } = req.params;
try {
const result = await Functions.findOne({ where: { id } });
if (!result) {
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
}
if (result.image !== "/images/functions/default.png") {
const filePath = path.join(__dirname, '..', 'assets', result.image);
fs.unlinkSync(filePath); // supprime le fichier
}
await Functions.destroy({ where: { id } });
res.status(200).json({ message: "La fonction a été correctement supprimé!"});
} catch (error) {
console.log(error);
errorHandling(next, serverError);
}
}