back: DELETE /admin/functions/:id
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user