From 0f8c1d3569e7df9a347f8932d1e24d7b74862f14 Mon Sep 17 00:00:00 2001 From: Divlo Date: Mon, 23 Mar 2020 16:50:31 +0100 Subject: [PATCH] backend: GET /functions/:slug --- api/controllers/functions.js | 29 ++++++++++++++++++++++++++--- api/routes/functions.js | 7 +++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/api/controllers/functions.js b/api/controllers/functions.js index 11a462c..9b6f234 100644 --- a/api/controllers/functions.js +++ b/api/controllers/functions.js @@ -43,7 +43,7 @@ exports.getFunctions = (req, res, next) => { .then((result) => { const { count, rows } = result; const hasMore = (page * limit) < count; - res.status(200).json({ totalItems: count, hasMore, rows }); + return res.status(200).json({ totalItems: count, hasMore, rows }); }) .catch((error) => { console.log(error); @@ -51,8 +51,31 @@ exports.getFunctions = (req, res, next) => { }); } -exports.executeFunctionName = (req, res, next) => { - const functionOutput = functionToExecute(req.params.functionName); +exports.getFunctionBySlug = (req, res, next) => { + const { slug } = req.params; + Functions.findOne({ + where: { slug, isOnline: 1 }, + attributes: { + exclude: ["updatedAt", "isOnline"] + }, + 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); + errorHandling(next, serverError); + }); +} + +exports.executeFunctionBySlug = (req, res, next) => { + const functionOutput = functionToExecute(req.params.slug); if (functionOutput !== undefined) { return functionOutput({ res, next }, req.body); } diff --git a/api/routes/functions.js b/api/routes/functions.js index 0e81447..2605107 100644 --- a/api/routes/functions.js +++ b/api/routes/functions.js @@ -8,9 +8,12 @@ FunctionsRouter.route('/') // Récupère les fonctions .get(functionsController.getFunctions); -FunctionsRouter.route('/:functionName') +FunctionsRouter.route('/:slug') + + // Récupère les informations de la fonction par son slug + .get(functionsController.getFunctionBySlug) // Exécute la fonction demandée en paramètre - .post(functionsController.executeFunctionName); + .post(functionsController.executeFunctionBySlug); module.exports = FunctionsRouter; \ No newline at end of file