Skip to content

Commit 9c1f945

Browse files
authored
Merge pull request #344 from tcet-opensource/287-Create-all-endpoints-for-faculty
287-created all endpoints for faculty
2 parents 2cb8ff9 + 25ac7ae commit 9c1f945

File tree

5 files changed

+166
-9
lines changed

5 files changed

+166
-9
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import timetableRouter from "#routes/timetable";
1919
import courseworkRouter from "#routes/coursework";
2020
import activityRouter from "#routes/activity";
2121
import moduleRouter from "#routes/module";
22+
import facultyRouter from "#routes/faculty";
2223
import { identifyUser } from "#middleware/identifyUser";
2324
import departmentRouter from "#routes/department";
2425

@@ -54,4 +55,5 @@ app.use("/timetable", timetableRouter);
5455
app.use("/department", departmentRouter);
5556
app.use("/coursework", courseworkRouter);
5657
app.use("/module", moduleRouter);
58+
app.use("/faculty", facultyRouter);
5759
export default app;

controller/faculty.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import {
2+
createFaculty, facultyList, deleteFacultyById, updateFacultyById,
3+
} from "#services/faculty";
4+
import { logger } from "#util";
5+
6+
async function addFaculty(req, res) {
7+
const {
8+
ERPID,
9+
dateOfJoining,
10+
dateOfLeaving,
11+
profileLink,
12+
qualifications,
13+
totalExperience,
14+
achievements,
15+
areaOfSpecialization,
16+
papersPublishedPG,
17+
papersPublishedUG,
18+
department,
19+
preferredSubjects,
20+
designation,
21+
natureOfAssociation,
22+
additionalResponsibilities,
23+
} = req.body;
24+
try {
25+
const newFaculty = await createFaculty(
26+
ERPID,
27+
dateOfJoining,
28+
dateOfLeaving,
29+
profileLink,
30+
qualifications,
31+
totalExperience,
32+
achievements,
33+
areaOfSpecialization,
34+
papersPublishedPG,
35+
papersPublishedUG,
36+
department,
37+
preferredSubjects,
38+
designation,
39+
natureOfAssociation,
40+
additionalResponsibilities,
41+
);
42+
res.json({ res: `added faculty ${newFaculty.ERPID}` });
43+
} catch (error) {
44+
logger.error("Error while inserting", error);
45+
res.status(500);
46+
res.json({ err: "Error while inserting in DB" });
47+
}
48+
}
49+
50+
async function getFaculty(req, res) {
51+
const filter = req.query;
52+
const facultylist = await facultyList(filter);
53+
res.json({ res: facultylist });
54+
}
55+
56+
async function deleteFaculty(req, res) {
57+
const { facultyId } = req.params;
58+
try {
59+
await deleteFacultyById(facultyId);
60+
res.json({ res: "Faculty deleted successfully" });
61+
} catch (error) {
62+
logger.error("Error while deleting", error);
63+
res.status(500);
64+
res.json({ err: "Error while deleting from DB" });
65+
}
66+
}
67+
68+
async function updateFaculty(req, res) {
69+
const {
70+
id, ...data
71+
} = req.body;
72+
try {
73+
await updateFacultyById(id, data);
74+
res.json({ res: `updated faculty with id ${id}` });
75+
} catch (error) {
76+
logger.error("Error while updating", error);
77+
res.status(500);
78+
res.json({ err: "Error while updating in DB" });
79+
}
80+
}
81+
82+
export default {
83+
addFaculty, getFaculty, deleteFaculty, updateFaculty,
84+
};

models/faculty.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ const facultySchema = {
1818
additionalResponsibilities: { type: String, required: true },
1919
};
2020

21-
facultySchema.virtual("tcetexperience").get(() => {
22-
const currentDate = new Date();
23-
const joiningYear = this.dateOfJoining.getFullYear();
24-
const leavingYear = this.dateOfLeaving
25-
? this.dateOfLeaving.getFullYear
26-
: currentDate.getFullYear;
27-
return leavingYear - joiningYear;
28-
});
29-
3021
const Faculty = connector.model("Faculty", facultySchema);
3122

3223
// CRUD Operations

routes/faculty.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import express from "express";
2+
import facultyController from "#controller/faculty";
3+
4+
const router = express.Router();
5+
router.post("/create", facultyController.addFaculty);
6+
router.get("/list", facultyController.getFaculty);
7+
router.post("/update/:id", facultyController.updateFaculty);
8+
router.delete("/delete/:id", facultyController.deleteFaculty);
9+
10+
export default router;

services/faculty.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import Faculty from "#models/faculty";
2+
import databaseError from "#error/database";
3+
4+
export async function createFaculty(
5+
ERPID,
6+
dateOfJoining,
7+
dateOfLeaving,
8+
profileLink,
9+
qualifications,
10+
totalExperience,
11+
achievements,
12+
areaOfSpecialization,
13+
papersPublishedPG,
14+
papersPublishedUG,
15+
department,
16+
preferredSubjects,
17+
designation,
18+
natureOfAssociation,
19+
additionalResponsibilities,
20+
) {
21+
const newFaculty = await Faculty.create({
22+
ERPID,
23+
dateOfJoining,
24+
dateOfLeaving,
25+
profileLink,
26+
qualifications,
27+
totalExperience,
28+
achievements,
29+
areaOfSpecialization,
30+
papersPublishedPG,
31+
papersPublishedUG,
32+
department,
33+
preferredSubjects,
34+
designation,
35+
natureOfAssociation,
36+
additionalResponsibilities,
37+
});
38+
if (newFaculty.ERPID === ERPID) {
39+
return newFaculty;
40+
}
41+
throw new databaseError.DataEntryError("Faculty");
42+
}
43+
44+
export async function facultyList(filter) {
45+
const facList = await Faculty.read(filter);
46+
if (facList) {
47+
return facList;
48+
}
49+
throw new databaseError.DataNotFoundError("Faculty");
50+
}
51+
52+
export async function deleteFacultyById(facultyId) {
53+
const deleted = await Faculty.remove({ _id: facultyId });
54+
if (deleted) {
55+
return deleted;
56+
}
57+
throw new databaseError.DataDeleteError("Faculty");
58+
}
59+
60+
export async function updateFacultyById(id, data) {
61+
const updated = await Faculty.update({ _id: id }, data);
62+
if (updated) {
63+
return updated;
64+
}
65+
throw new databaseError.DataEntryError("Faculty");
66+
}
67+
68+
export default {
69+
createFaculty, facultyList, deleteFacultyById, updateFacultyById,
70+
};

0 commit comments

Comments
 (0)