From 5d791f34d603f48c6624cbb666538e87cb142c24 Mon Sep 17 00:00:00 2001 From: Divlo Date: Wed, 15 Apr 2020 14:17:57 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20PUT=20/admin/functions/fo?= =?UTF-8?q?rm=20et=20article/:id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controllers/admin.js | 58 ++++++++++++++++++++++++++++++++++++++++ api/routes/admin.js | 11 ++++++++ 2 files changed, 69 insertions(+) diff --git a/api/controllers/admin.js b/api/controllers/admin.js index fce67bf..f3802cc 100644 --- a/api/controllers/admin.js +++ b/api/controllers/admin.js @@ -63,6 +63,26 @@ exports.getFunctions = (req, res, next) => { }); } +exports.getFunctionById = (req, res, next) => { + const { id } = req.params; + Functions.findOne({ + where: { id }, + include: [ + { model: Categories, attributes: ["name", "color"] } + ] + }) + .then((result) => { + if (!result) { + return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 }); + } + return res.status(200).json(result); + }) + .catch((error) => { + console.log(error); + return errorHandling(next, serverError); + }); +} + exports.postFunction = (req, res, next) => { const { title, slug, description, type, categorieId } = req.body; const image = req.files.image; @@ -142,6 +162,44 @@ exports.putFunction = async (req, res, next) => { } } +exports.putFunctionArticle = async (req, res, next) => { + const { id } = req.params; + const { article } = req.body; + + try { + // Vérifie si la fonction existe + const resultFunction = await Functions.findOne({ where: { id } }); + if (!resultFunction) { + return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 }); + } + resultFunction.article = article; + const result = await resultFunction.save(); + return res.status(200).json(result); + } catch (error) { + console.log(error); + return errorHandling(next, serverError); + } +} + +exports.putFunctionForm = async (req, res, next) => { + const { id } = req.params; + const { form } = req.body; + + try { + // Vérifie si la fonction existe + const resultFunction = await Functions.findOne({ where: { id } }); + if (!resultFunction) { + return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 }); + } + resultFunction.form = form; + const result = await resultFunction.save(); + return res.status(200).json(result); + } catch (error) { + console.log(error); + return errorHandling(next, serverError); + } +} + exports.deleteFunction = async (req, res, next) => { const { id } = req.params; try { diff --git a/api/routes/admin.js b/api/routes/admin.js index a2f78f0..2695ed6 100644 --- a/api/routes/admin.js +++ b/api/routes/admin.js @@ -97,6 +97,9 @@ AdminRouter.route('/functions') AdminRouter.route('/functions/:id') + // Récupère les informations d'une fonction + .get(isAuth, isAdmin, adminController.getFunctionById) + // Modifie information basique d'une fonction .put(isAuth, isAdmin, fileUpload({ @@ -170,6 +173,14 @@ AdminRouter.route('/functions/:id') // Supprime une fonction avec son id .delete(isAuth, isAdmin, adminController.deleteFunction); +AdminRouter.route('/functions/article/:id') + + .put(isAuth, isAdmin, adminController.putFunctionArticle); + +AdminRouter.route('/functions/form/:id') + + .put(isAuth, isAdmin, adminController.putFunctionForm); + AdminRouter.route('/categories') // Crée une catégorie