From 15b64dbf4a0e19ff08e5a860d0b0f939a7f88bde Mon Sep 17 00:00:00 2001 From: Deepti Date: Sat, 4 Nov 2023 19:58:41 +0530 Subject: [PATCH 1/2] [added] --- controller/module.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/controller/module.js b/controller/module.js index 3f1ab13..a3293c5 100644 --- a/controller/module.js +++ b/controller/module.js @@ -5,6 +5,8 @@ import { deleteModuleById, } from "#services/module"; import { logger } from "#util"; +import { isEntityIdValid } from "#middleware/entityIdValidation"; +import Topic from "#models/topic"; async function showModule(req, res) { try { @@ -21,15 +23,22 @@ async function showModule(req, res) { async function addModule(req, res) { const { no, name, contents, hrsPerModule, cognitiveLevels } = req.body; + const isTopicValid = await isEntityIdValid(Topic, Topic); try { - const newModule = await addNewModule( - no, - name, - contents, - hrsPerModule, - cognitiveLevels, - ); - res.json({ res: `added module ${newModule.name}` }); + if (isTopicValid) { + const newModule = await addNewModule( + no, + name, + contents, + hrsPerModule, + cognitiveLevels, + ); + res.json({ res: `added module ${newModule.name}` }); + res.json({ res: `added module ${newModule.id}` }); + } else { + res.status(400).json({ err: "Invalid name" }); + res.status(400).json({ err: "Invalid id" }); + } } catch (error) { logger.error("Error while inserting", error); res.status(500); From eb4bf7212a980a6f478708de5c07d9503de99e8c Mon Sep 17 00:00:00 2001 From: Deepti Singh <136986628+deepti-50@users.noreply.github.com> Date: Sat, 4 Nov 2023 22:32:42 +0530 Subject: [PATCH 2/2] Updated module.js --- controller/module.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/controller/module.js b/controller/module.js index a3293c5..9340291 100644 --- a/controller/module.js +++ b/controller/module.js @@ -23,7 +23,7 @@ async function showModule(req, res) { async function addModule(req, res) { const { no, name, contents, hrsPerModule, cognitiveLevels } = req.body; - const isTopicValid = await isEntityIdValid(Topic, Topic); + const isTopicValid = await isEntityIdValid(contents, Topic); try { if (isTopicValid) { const newModule = await addNewModule( @@ -33,11 +33,9 @@ async function addModule(req, res) { hrsPerModule, cognitiveLevels, ); - res.json({ res: `added module ${newModule.name}` }); - res.json({ res: `added module ${newModule.id}` }); + res.json({ res: `added module ${newModule.name} ${newModule.id}` }); } else { - res.status(400).json({ err: "Invalid name" }); - res.status(400).json({ err: "Invalid id" }); + res.status(400).json({ err: "Invalid name" , err: "Invalid id"}); } } catch (error) { logger.error("Error while inserting", error);