Skip to content

Commit cac9c58

Browse files
committed
added faker for courses and everything under
1 parent 7537299 commit cac9c58

File tree

17 files changed

+598
-37
lines changed

17 files changed

+598
-37
lines changed

app.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import groupRouter from "#routes/group";
3030
import performarouter from "#routes/performance";
3131
import notificationRouter from "#routes/notification";
3232
import topicRouter from "#routes/topic";
33+
import courseRouter from "#routes/course";
3334

3435
const app = express();
3536
const currDirName = dirname(fileURLToPath(import.meta.url));
@@ -40,10 +41,12 @@ app.use(cors());
4041
app.use(express.json());
4142
app.use(express.urlencoded({ extended: false }));
4243
app.use(cookieParser());
43-
app.use(morgan(
44-
":remote-addr - :remote-user \":method :url HTTP/:http-version\" :status \":referrer\" \":user-agent\"",
45-
{ stream: logger.stream },
46-
));
44+
app.use(
45+
morgan(
46+
":remote-addr - :remote-user \":method :url HTTP/:http-version\" :status \":referrer\" \":user-agent\"",
47+
{ stream: logger.stream },
48+
),
49+
);
4750

4851
app.use(express.static(path.join(currDirName, "public")));
4952

@@ -71,6 +74,7 @@ app.use("/semester", semesterRouter);
7174
app.use("/faculty", facultyRouter);
7275
app.use("/performance", performarouter);
7376
app.use("/notification", notificationRouter);
74-
app.use("/topic",topicRouter);
77+
app.use("/topic", topicRouter);
78+
app.use("/course", courseRouter);
7579

7680
export default app;

controller/course.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import {
2+
createCourse,
3+
courseList,
4+
deleteCourseById,
5+
updateCourseById,
6+
} from "#services/course";
7+
import { logger } from "#util";
8+
9+
async function addCourse(req, res) {
10+
const {
11+
name,
12+
code,
13+
theoryHours,
14+
department,
15+
tutorialHours,
16+
practicalHours,
17+
ISAMarks,
18+
ESEMarks,
19+
tutorialMarks,
20+
practicalMarks,
21+
semester,
22+
subType,
23+
prerequisites,
24+
objective,
25+
outcomes,
26+
modules,
27+
practicals,
28+
tutorials,
29+
assignments,
30+
reccTextbooks,
31+
refBooks,
32+
} = req.body;
33+
try {
34+
const newCourse = await createCourse(
35+
name,
36+
code,
37+
theoryHours,
38+
department,
39+
tutorialHours,
40+
practicalHours,
41+
ISAMarks,
42+
ESEMarks,
43+
tutorialMarks,
44+
practicalMarks,
45+
semester,
46+
subType,
47+
prerequisites,
48+
objective,
49+
outcomes,
50+
modules,
51+
practicals,
52+
tutorials,
53+
assignments,
54+
reccTextbooks,
55+
refBooks,
56+
);
57+
res.json({ res: `added course ${newCourse.ERPID}` });
58+
} catch (error) {
59+
logger.error("Error while inserting", error);
60+
res.status(500);
61+
res.json({ err: "Error while inserting in DB" });
62+
}
63+
}
64+
65+
async function getCourse(req, res) {
66+
try {
67+
const filter = req.body;
68+
const { limit, page } = req.query;
69+
const courselist = await courseList(filter, limit, page);
70+
res.json({ res: courselist });
71+
} catch (error) {
72+
logger.error("Error while fetching", error);
73+
res.status(500);
74+
res.json({ err: "Error while fetching the data" });
75+
}
76+
}
77+
78+
async function deleteCourse(req, res) {
79+
const { courseId } = req.params;
80+
try {
81+
await deleteCourseById(courseId);
82+
res.json({ res: "Course deleted successfully" });
83+
} catch (error) {
84+
logger.error("Error while deleting", error);
85+
res.status(500);
86+
res.json({ err: "Error while deleting from DB" });
87+
}
88+
}
89+
90+
async function updateCourse(req, res) {
91+
const { id, ...data } = req.body;
92+
try {
93+
await updateCourseById(id, data);
94+
res.json({ res: `updated course with id ${id}` });
95+
} catch (error) {
96+
logger.error("Error while updating", error);
97+
res.status(500);
98+
res.json({ err: "Error while updating in DB" });
99+
}
100+
}
101+
102+
export default {
103+
addCourse,
104+
getCourse,
105+
deleteCourse,
106+
updateCourse,
107+
};

misc/initDB.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ import Organization from "#models/organization";
55
import Department from "#models/department";
66
import Topics from "#models/topic";
77
import Module from "#models/module";
8+
import Tutorial from "#models/tutorial";
9+
import Practical from "#models/practical";
10+
import Semester from "#models/semester";
11+
import Course from "#models/course";
812
import generateOrganizations from "#mockDB/orgMock";
913
import ACCREDS from "#mockDB/accredMock";
1014
import TOPICS from "#mockDB/topicMock";
1115
import generateDepartments from "#mockDB/deptMock";
1216
import generateModules from "#mockDB/moduleMock";
1317
import generateInfrastructures from "#mockDB/infraMock";
18+
import generatePracticals from "#mockDB/pracMock";
19+
import generateTutorials from "#mockDB/tutMock";
20+
import generateSemesters from "#mockDB/semMock";
21+
import generateCourses from "#mockDB/courseMock";
1422
/* eslint-disable no-underscore-dangle */
1523
const createdAccreds = await Accreditation.createMultiple(ACCREDS);
1624

@@ -48,14 +56,36 @@ const DEPTS = generateDepartments(
4856
filteredInfrastructures.map((createdInfra) => createdInfra._id),
4957
);
5058

51-
const createdDepts = await Department.createMultiple(DEPTS); // eslint-disable-line no-unused-vars
59+
const createdDepts = await Department.createMultiple(DEPTS);
5260

5361
const createdTopics = await Topics.createMultiple(TOPICS);
5462

5563
const MODULES = await generateModules(
5664
createdTopics.map((createdTopic) => createdTopic._id),
5765
);
5866

59-
const createdModules = await Module.createMultiple(MODULES); // eslint-disable-line no-unused-vars
67+
const createdModules = await Module.createMultiple(MODULES);
68+
69+
const PRACS = generatePracticals();
70+
71+
const createdPracs = await Practical.createMultiple(PRACS);
72+
73+
const TUTS = generateTutorials();
74+
75+
const createdTuts = await Tutorial.createMultiple(TUTS);
76+
77+
const SEMS = generateSemesters();
78+
79+
const createdSems = await Semester.createMultiple(SEMS);
80+
81+
const COURSES = generateCourses(
82+
createdSems,
83+
createdModules,
84+
createdPracs,
85+
createdTuts,
86+
createdDepts,
87+
);
88+
89+
const createdCourses = await Course.createMultiple(COURSES); // eslint-disable-line no-unused-vars
6090

6191
process.exit(0);

misc/mockDB/assignmentMock.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies
2+
3+
const generateRandomLevels = () => {
4+
const levels = [];
5+
const numLevels = faker.number.int({ min: 1, max: 6 });
6+
for (let i = 0; i < numLevels; i += 1) {
7+
levels.push(`L${i + 1}`);
8+
}
9+
return levels;
10+
};
11+
12+
const createRandomTutorials = (i) => ({
13+
no: i,
14+
title: faker.lorem.sentence(),
15+
type: faker.number.int({ min: 3, max: 12 }),
16+
marks: generateRandomLevels(),
17+
});
18+
19+
const generateTutorials = () => {
20+
const tutorials = [];
21+
for (let i = 1, j = 1; j <= 20; i += 1) {
22+
tutorials.push(createRandomTutorials(i));
23+
if (i === 10) {
24+
i = 0;
25+
j += 1;
26+
}
27+
}
28+
return tutorials;
29+
};
30+
31+
export default generateTutorials;

misc/mockDB/courseMock.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies
2+
3+
const generateRandomLevels = () => {
4+
const levels = [];
5+
const numLevels = faker.number.int({ min: 1, max: 6 });
6+
for (let i = 0; i < numLevels; i += 1) {
7+
levels.push(`L${i + 1}`);
8+
}
9+
return levels;
10+
};
11+
12+
const generateRandomOutcomes = () => {
13+
const outcomes = [];
14+
const outcome = faker.lorem.sentence({ min: 3, max: 6 });
15+
for (let i = 0; i <= 6; i += 1) {
16+
outcomes.push(outcome);
17+
}
18+
return outcomes;
19+
};
20+
21+
const createRandomCourses = (type) => ({
22+
name: faker.lorem.sentence({ min: 1, max: 3 }),
23+
code: faker.vehicle.vin(),
24+
theoryHours:
25+
type === "onlypractical" ? null : faker.number.int({ min: 2, max: 6 }),
26+
department: faker.database.mongodbObjectId(), // TODO
27+
tutorialHours:
28+
type === "tutorial" ? faker.number.int({ min: 2, max: 6 }) : null,
29+
practicalHours: faker.helpers.weightedArrayElement([
30+
{ value: faker.number.int({ min: 2, max: 6 }), weight: 6 },
31+
{ value: null, weight: 1 },
32+
]),
33+
ISAMarks: 20,
34+
ESEMarks: 60,
35+
tutorialMarks: type === "tutorial" ? 20 : null,
36+
practicalMarks: type === "nopractical" ? null : 30,
37+
semester: faker.database.mongodbObjectId(),
38+
subType: faker.helpers.arrayElement(["open", "professional", "core"]),
39+
prerequisites: generateRandomOutcomes(),
40+
objective: faker.lorem.sentence({ min: 6, max: 10 }),
41+
outcomes: {
42+
outcome: faker.lorem.sentence({ min: 3, max: 6 }),
43+
RBTLevel: generateRandomLevels(),
44+
},
45+
modules: [faker.database.mongodbObjectId()],
46+
practicals: [faker.database.mongodbObjectId()],
47+
tutorials: [faker.database.mongodbObjectId()],
48+
assignments: [faker.database.mongodbObjectId()],
49+
reccTextbooks: generateRandomOutcomes(),
50+
refBooks: faker.lorem.word({ length: { min: 5, max: 7 } }),
51+
});
52+
53+
const generateCourses = (
54+
createdSems,
55+
createdModules,
56+
createdPracs,
57+
createdTuts,
58+
createdDepts,
59+
) => {
60+
const courses = [];
61+
for (let i = 0, j = 0; j <= createdDepts.length; i += 1) {
62+
const type = faker.helpers.weightedArrayElement([
63+
{ value: "normal", weight: 12 },
64+
{ value: "tutorial", weight: 2 },
65+
{ value: "nopractical", weight: 2 },
66+
{ value: "onlypractical", weight: 1 },
67+
]);
68+
courses.push(createRandomCourses(type));
69+
if (i >= createdSems.length) {
70+
i = 0;
71+
j += 1;
72+
}
73+
}
74+
return courses;
75+
};
76+
77+
export default generateCourses;

misc/mockDB/infraMock.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const createRandomInfrastructure = (i, tcetObjectId) => ({
1919

2020
const generateInfrastructures = (tcetObjectId) => {
2121
const infrastructures = [];
22-
for (let j = 1, i = 0; j < 7; i += 1) {
22+
for (let j = 1, i = 0; j <= 6; i += 1) {
2323
infrastructures.push(createRandomInfrastructure(j * 100 + i, tcetObjectId));
2424
if (i === 25) {
25-
i = 1;
25+
i = 0;
2626
j += 1;
2727
}
2828
}

misc/mockDB/moduleMock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const createRandomModules = (i, topics) => ({
1919

2020
const generateModules = (topics) => {
2121
const modules = [];
22-
for (let i = 1; i < topics.length / 5; i += 1) {
22+
for (let i = 1; i < topics.length / 5 + 1; i += 1) {
2323
const moduleTopics = topics.slice(i, i + 5);
2424
modules.push(createRandomModules(i, moduleTopics));
2525
}

misc/mockDB/pracMock.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { faker } from "@faker-js/faker"; // eslint-disable-line import/no-extraneous-dependencies
2+
3+
const generateRandomLevels = () => {
4+
const levels = [];
5+
const numLevels = faker.number.int({ min: 1, max: 6 });
6+
for (let i = 0; i < numLevels; i += 1) {
7+
levels.push(`L${i + 1}`);
8+
}
9+
return levels;
10+
};
11+
12+
const createRandomPracticals = (i, ptype) => ({
13+
no: i,
14+
type: ptype,
15+
title: faker.lorem.sentence(),
16+
hours: faker.number.int({ min: 3, max: 12 }),
17+
cognitiveLevels: generateRandomLevels(),
18+
});
19+
20+
const generatePracticals = () => {
21+
const practicals = [];
22+
for (let i = 1, j = 1; j <= 60; i += 1) {
23+
let type;
24+
if (i <= 5) type = "BASIC";
25+
else if (i <= 10) type = "DESIGN";
26+
else type = "PROJECT";
27+
practicals.push(createRandomPracticals(i, type));
28+
if (i === 12) {
29+
i = 0;
30+
j += 1;
31+
}
32+
}
33+
return practicals;
34+
};
35+
36+
export default generatePracticals;

misc/mockDB/semMock.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const createRandomSemesters = (i, year, ptype) => ({
2+
number: i,
3+
academicYear: year,
4+
type: ptype,
5+
startDate: `${year}-${ptype === "ODD" ? "06" : "01"}-01T00:00:00.000Z`,
6+
endDate: `${year}-${ptype === "ODD" ? "12" : "06"}-01T00:00:00.000Z`,
7+
});
8+
9+
const generateSemesters = () => {
10+
const semesters = [];
11+
let year;
12+
let type;
13+
for (let i = 7, j = 4; j > 0; i += 1) {
14+
year = `${2024 + Math.floor(i / 2 - j)}`;
15+
type = i % 2 ? "ODD" : "EVEN";
16+
semesters.push(createRandomSemesters(i, year, type));
17+
if (i >= 8) {
18+
j -= 1;
19+
i = j * 2 - 2;
20+
}
21+
}
22+
return semesters;
23+
};
24+
25+
export default generateSemesters;

0 commit comments

Comments
 (0)