Skip to content

Commit 81a3509

Browse files
Merge pull request #444 from tcet-opensource/426-added-validation-for-module
[ADDED]Added validation for module.
2 parents 1956e3b + eb4bf72 commit 81a3509

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

controller/module.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
deleteModuleById,
66
} from "#services/module";
77
import { logger } from "#util";
8+
import { isEntityIdValid } from "#middleware/entityIdValidation";
9+
import Topic from "#models/topic";
810

911
async function showModule(req, res) {
1012
try {
@@ -21,15 +23,20 @@ async function showModule(req, res) {
2123

2224
async function addModule(req, res) {
2325
const { no, name, contents, hrsPerModule, cognitiveLevels } = req.body;
26+
const isTopicValid = await isEntityIdValid(contents, Topic);
2427
try {
25-
const newModule = await addNewModule(
26-
no,
27-
name,
28-
contents,
29-
hrsPerModule,
30-
cognitiveLevels,
31-
);
32-
res.json({ res: `added module ${newModule.name}` });
28+
if (isTopicValid) {
29+
const newModule = await addNewModule(
30+
no,
31+
name,
32+
contents,
33+
hrsPerModule,
34+
cognitiveLevels,
35+
);
36+
res.json({ res: `added module ${newModule.name} ${newModule.id}` });
37+
} else {
38+
res.status(400).json({ err: "Invalid name" , err: "Invalid id"});
39+
}
3340
} catch (error) {
3441
logger.error("Error while inserting", error);
3542
res.status(500);

0 commit comments

Comments
 (0)