Skip to content

Commit e4e11e2

Browse files
Merge pull request #327 from tcet-opensource/258-endpoints-from-pracs
[Feature]-Added endpoints for practical model
2 parents c4ef262 + 2a0b1d5 commit e4e11e2

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import usersRouter from "#routes/users";
1010
import authRouter from "#routes/auth";
1111
import accreditationRouter from "#routes/accreditation";
1212
import infrastructureRouter from "#routes/infrastructure";
13+
import practicalRouter from "#routes/practical";
1314
import organizationRouter from "#routes/organization";
1415
import studentRouter from "#routes/student";
1516
import tutorialRouter from "#routes/tutorial";
@@ -20,6 +21,7 @@ import moduleRouter from "#routes/module";
2021
import { identifyUser } from "#middleware/identifyUser";
2122
import departmentRouter from "#routes/department";
2223

24+
2325
const app = express();
2426
const currDirName = dirname(fileURLToPath(import.meta.url));
2527

@@ -41,6 +43,7 @@ app.use("/users", usersRouter);
4143
app.use("/auth", authRouter);
4244
app.use("/accreditation", accreditationRouter);
4345
app.use("/infrastructure", infrastructureRouter);
46+
app.use("/practical", practicalRouter);
4447
app.use("/organization", organizationRouter);
4548
app.use("/student", studentRouter);
4649
app.use("/tutorial", tutorialRouter);

controller/practical.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Import Practical-related services and utilities
2+
import {
3+
createPractical,
4+
deletePracticalById,
5+
listPractical,
6+
updatePracticalById,
7+
} from "#services/practical";
8+
9+
import { logger } from "#util"; // Import the logger utility
10+
11+
// Controller function to add a new Practical entity
12+
async function addPractical(req, res) {
13+
const {
14+
no, title, type, hours, cognitiveLevels,
15+
} = req.body;
16+
try {
17+
const newPractical = await createPractical({
18+
no, title, type, hours, cognitiveLevels,
19+
});
20+
res.json({ res: `Added Practical with ID ${newPractical.id}` });
21+
} catch (error) {
22+
logger.error("Error while inserting Practical", error);
23+
res.status(500);
24+
res.json({ err: "Error while inserting Practical in DB" });
25+
}
26+
}
27+
28+
// Controller function to update a Practical entity
29+
async function updatePractical(req, res) {
30+
const {
31+
id, ...data
32+
} = req.body;
33+
try {
34+
await updatePracticalById(id, data);
35+
res.json({ res: `Updated Practical with ID ${id}` });
36+
} catch (error) {
37+
logger.error("Error while updating Practical", error);
38+
res.status(500);
39+
res.json({ err: "Error while updating Practical in DB" });
40+
}
41+
}
42+
43+
// Controller function to get a list of Practical entities
44+
async function getPractical(req, res) {
45+
const filter = req.query;
46+
const practicalList = await listPractical(filter);
47+
res.json({ res: practicalList });
48+
}
49+
50+
// Controller function to delete a Practical entity
51+
async function deletePractical(req, res) {
52+
const { practicalId } = req.params;
53+
try {
54+
await deletePracticalById(practicalId);
55+
res.json({ res: `Deleted Practical with ID ${practicalId}` });
56+
} catch (error) {
57+
logger.error("Error while deleting Practical", error);
58+
res.status(500).json({ error: "Error while deleting Practical from DB" });
59+
}
60+
}
61+
62+
export default {
63+
addPractical,
64+
deletePractical,
65+
getPractical,
66+
updatePractical,
67+
};
68+

routes/practical.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express from "express";
2+
import practicalController from "#controller/practical";
3+
4+
const router = express.Router();
5+
6+
// Create a new Practical
7+
router.post("/practical/create", practicalController.addPractical);
8+
9+
// List Practical entities with optional filters
10+
router.get("/practical/list", practicalController.getPractical);
11+
12+
// Update Practical entities based on filters and update data
13+
router.post("/practical/update", practicalController.updatePractical);
14+
15+
// Delete Practical entities based on filters
16+
router.post("/practical/delete", practicalController.deletePractical);
17+
18+
export default router;

services/practical.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Import Practical-related model and databaseError module
2+
import Practical from "#models/practical";
3+
import databaseError from "#error/database";
4+
5+
// Service function to create a new Practical entity
6+
export async function createPractical({
7+
no, title, type, hours, cognitiveLevels,
8+
}) {
9+
try {
10+
const newPractical = await Practical.create({
11+
no, title, type, hours, cognitiveLevels,
12+
});
13+
return newPractical;
14+
} catch (error) {
15+
throw new databaseError.DataEntryError("practical");
16+
}
17+
}
18+
19+
// Service function to update a Practical entity by ID
20+
export async function updatePracticalById(id, data) {
21+
try {
22+
const updated = await Practical.updateOne({ _id: id }, data);
23+
if (updated.nModified > 0) {
24+
return updated;
25+
}
26+
throw new databaseError.DataEntryError("practical");
27+
} catch (error) {
28+
throw new databaseError.DataEntryError("practical");
29+
}
30+
}
31+
32+
// Service function to retrieve a list of Practical entities based on filters
33+
export async function listPractical(filter) {
34+
try {
35+
const practicalList = await Practical.find(filter);
36+
return practicalList;
37+
} catch (error) {
38+
throw new databaseError.DataRetrievalError("practical");
39+
}
40+
}
41+
42+
// Service function to delete a Practical entity by ID
43+
export async function deletePracticalById(practicalId) {
44+
try {
45+
const deleted = await Practical.deleteOne({ _id: practicalId });
46+
if (deleted.deletedCount > 0) {
47+
return deleted;
48+
}
49+
throw new databaseError.DataDeleteError("practical");
50+
} catch (error) {
51+
throw new databaseError.DataDeleteError("practical");
52+
}
53+
}

0 commit comments

Comments
 (0)