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