back: DELETE /admin/functions/:id
This commit is contained in:
parent
cec6881308
commit
49da9d5d48
1
api/.gitignore
vendored
1
api/.gitignore
vendored
@ -19,6 +19,7 @@
|
|||||||
.env.test.local
|
.env.test.local
|
||||||
.env.production.local
|
.env.production.local
|
||||||
/temp
|
/temp
|
||||||
|
/assets/images/
|
||||||
|
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
const { validationResult } = require('express-validator');
|
const { validationResult } = require('express-validator');
|
||||||
const errorHandling = require('../assets/utils/errorHandling');
|
const errorHandling = require('../assets/utils/errorHandling');
|
||||||
const { serverError } = require('../assets/config/errors');
|
const { serverError } = require('../assets/config/errors');
|
||||||
@ -30,4 +31,23 @@ exports.postFunction = (req, res, next) => {
|
|||||||
errorHandling(next, serverError);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,9 +10,7 @@ const Categories = require('../models/categories');
|
|||||||
const AdminRouter = Router();
|
const AdminRouter = Router();
|
||||||
|
|
||||||
// Permet de créé une fonction
|
// Permet de créé une fonction
|
||||||
AdminRouter.post('/functions',
|
AdminRouter.post('/functions', isAuth, isAdmin,
|
||||||
isAuth,
|
|
||||||
isAdmin,
|
|
||||||
fileUpload({
|
fileUpload({
|
||||||
useTempFiles: true,
|
useTempFiles: true,
|
||||||
safeFileNames: true,
|
safeFileNames: true,
|
||||||
@ -76,4 +74,7 @@ AdminRouter.post('/functions',
|
|||||||
adminController.postFunction
|
adminController.postFunction
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Supprime une fonction avec son id
|
||||||
|
AdminRouter.delete('/functions/:id', isAuth, isAdmin, adminController.deleteFunction);
|
||||||
|
|
||||||
module.exports = AdminRouter;
|
module.exports = AdminRouter;
|
Reference in New Issue
Block a user