backend: /admin/categories
This commit is contained in:
		| @@ -22,7 +22,7 @@ En plus de présenter des fonctions, FunctionProject est un **blog** ce qui perm | ||||
|  | ||||
| Si vous aimez le projet, vous pouvez aider à **le faire connaître** en utilisant [#FunctionProject](https://twitter.com/hashtag/FunctionProject) sur **Twitter**. 🐦 | ||||
|  | ||||
| Le projet est disponible sur function.divlo.fr. | ||||
| Le projet est disponible sur [function.divlo.fr](https://function.divlo.fr/). | ||||
|  | ||||
| ## 🚀 Open Source  | ||||
|  | ||||
|   | ||||
| @@ -69,8 +69,8 @@ exports.postFunction = (req, res, next) => { | ||||
|     image.mv(path.join(__dirname, '..', 'assets', 'images', 'functions') + '/' + imageName, async (error) => { | ||||
|         if (error) return errorHandling(next, serverError); | ||||
|         try { | ||||
|             await Functions.create({ title, slug, description, type, categorieId, image: `/images/functions/${imageName}` }); | ||||
|             return res.status(201).json({ message: "La fonction a été correctement ajouté!"}); | ||||
|             const result = await Functions.create({ title, slug, description, type, categorieId, image: `/images/functions/${imageName}` }); | ||||
|             return res.status(201).json({ message: "La fonction a été correctement ajouté!", result }); | ||||
|         } catch (error) { | ||||
|             console.log(error); | ||||
|             return errorHandling(next, serverError); | ||||
| @@ -95,4 +95,54 @@ exports.deleteFunction = async (req, res, next) => { | ||||
|         console.log(error); | ||||
|         return errorHandling(next, serverError); | ||||
|     } | ||||
| } | ||||
|  | ||||
| exports.postCategory = async (req, res, next) => { | ||||
|     const { name, color } = req.body; | ||||
|     if (!(name && color)) { | ||||
|         return errorHandling(next, { message: "La catégorie doit avoir un nom et une couleur." }); | ||||
|     } | ||||
|     try { | ||||
|         const result = await Categories.create({ name, color }); | ||||
|         return res.status(201).json({ message: "La catégorie a bien été crée!", result }); | ||||
|     } catch (error) { | ||||
|         console.log(error); | ||||
|         return errorHandling(next, serverError); | ||||
|     } | ||||
| } | ||||
|  | ||||
| exports.putCategory = async (req, res, next) => { | ||||
|     const { name, color } = req.body; | ||||
|     const { id }          = req.params; | ||||
|     if (!(name && color && id)) { | ||||
|         return errorHandling(next, { message: "La catégorie doit avoir un nom, une couleur et un id." }); | ||||
|     } | ||||
|     try { | ||||
|         const category = await Categories.findOne({ where: { id } }); | ||||
|         if (!category) { | ||||
|             return errorHandling(next, { message: "La catégorie n'existe pas." }); | ||||
|         } | ||||
|         category.name = name; | ||||
|         category.color = color; | ||||
|         const result = await category.save(); | ||||
|         return res.status(200).json({ message: "La catégorie a bien été modifié!", result }); | ||||
|     } catch (error) { | ||||
|         console.log(error); | ||||
|         return errorHandling(next, serverError); | ||||
|     } | ||||
| } | ||||
|  | ||||
| exports.deleteCategory = async (req, res, next) => { | ||||
|     const { id } = req.params; | ||||
|     try { | ||||
|         const category = await Categories.findOne({ where: { id } }); | ||||
|         if (!category) { | ||||
|             return errorHandling(next, { message: "La catégorie n'existe pas." }); | ||||
|         } | ||||
|         await category.destroy(); | ||||
|         return res.status(200).json({ message: "La catégorie a bien été supprimé!" }); | ||||
|     } catch (error) { | ||||
|         console.log(error); | ||||
|         return errorHandling(next, serverError); | ||||
|     } | ||||
| } | ||||
| @@ -100,4 +100,17 @@ AdminRouter.route('/functions/:id') | ||||
|     // Supprime une fonction avec son id | ||||
|     .delete(isAuth, isAdmin, adminController.deleteFunction); | ||||
|  | ||||
| AdminRouter.route('/categories') | ||||
|  | ||||
|     // Crée une catégorie | ||||
|     .post(isAuth, isAdmin, adminController.postCategory); | ||||
|  | ||||
| AdminRouter.route('/categories/:id') | ||||
|  | ||||
|     // Modifier une catégorie avec son id | ||||
|     .put(isAuth, isAdmin, adminController.putCategory) | ||||
|  | ||||
|     // Supprime une fonction avec son id | ||||
|     .delete(isAuth, isAdmin, adminController.deleteCategory); | ||||
|  | ||||
| module.exports = AdminRouter; | ||||
		Reference in New Issue
	
	Block a user