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