Skip to content

Commit 0f0e834

Browse files
Merge pull request #440 from harsh31dangi/430-Student-Validation
Feat : 430 Adds validation before creating a new Student
2 parents 81a3509 + eea7c70 commit 0f0e834

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

controller/student.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,33 @@ import {
55
updateStudentById,
66
} from "#services/student";
77
import { logger } from "#util";
8+
import { isEntityIdValid } from "#middleware/entityIdValidation";
9+
import Department from "#models/department";
10+
import Course from "#models/course";
811

912
async function addStudent(req, res) {
1013
const { ERPID, name, joiningYear, branch, division, rollNo, coursesOpted } =
1114
req.body;
1215
try {
13-
const newStudent = await createStudent(
14-
ERPID,
15-
name,
16-
joiningYear,
17-
branch,
18-
division,
19-
rollNo,
20-
coursesOpted,
21-
);
22-
res.json({ res: `added user ${newStudent.id}`, id: newStudent.id });
16+
const isBranchValid = await isEntityIdValid(branch, Department);
17+
const isCourseValid = await isEntityIdValid(coursesOpted, Course);
18+
if (isBranchValid && isCourseValid) {
19+
const newStudent = await createStudent(
20+
ERPID,
21+
name,
22+
joiningYear,
23+
branch,
24+
division,
25+
rollNo,
26+
coursesOpted,
27+
);
28+
res.json({ res: `added user ${newStudent.id}`, id: newStudent.id });
29+
} else {
30+
var error = "";
31+
if (!isBranchValid) error.concat('Invalid branch');
32+
if (!isCourseValid) error.concat(' Invalid course opted');
33+
res.status(400).json({ err: error });
34+
}
2335
} catch (error) {
2436
logger.error("Error while inserting", error);
2537
res.status(500);

0 commit comments

Comments
 (0)