From 53f9d04778187518af4993cfee17735122039f8d Mon Sep 17 00:00:00 2001 From: Aryanzs Date: Sat, 2 Sep 2023 17:39:04 +0530 Subject: [PATCH 1/2] create endpoints for practical model --- app.js | 4 ++- controller/practical.js | 68 +++++++++++++++++++++++++++++++++++++++++ routes/practical.js | 18 +++++++++++ services/practical.js | 53 ++++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 controller/practical.js create mode 100644 routes/practical.js create mode 100644 services/practical.js diff --git a/app.js b/app.js index 769f3985..bbd9448a 100644 --- a/app.js +++ b/app.js @@ -10,8 +10,10 @@ import usersRouter from "#routes/users"; import authRouter from "#routes/auth"; import accreditationRouter from "#routes/accreditation"; import infrastructureRouter from "#routes/infrastructure"; +import practicalRouter from "#models/practical"; import { identifyUser } from "#middleware/identifyUser"; + const app = express(); const currDirName = dirname(fileURLToPath(import.meta.url)); @@ -33,5 +35,5 @@ app.use("/users", usersRouter); app.use("/auth", authRouter); app.use("/accreditation", accreditationRouter); app.use("/infrastructure", infrastructureRouter); - +app.use("/practical", practicalRouter); export default app; diff --git a/controller/practical.js b/controller/practical.js new file mode 100644 index 00000000..2216bb9a --- /dev/null +++ b/controller/practical.js @@ -0,0 +1,68 @@ +// Import Practical-related services and utilities +import { + createPractical, + deletePracticalById, + listPractical, + updatePracticalById, + } from "#services/practical"; + + import { logger } from "#util"; // Import the logger utility + + // Controller function to add a new Practical entity + async function addPractical(req, res) { + const { + no, title, type, hours, cognitiveLevels, + } = req.body; + try { + const newPractical = await createPractical({ + no, title, type, hours, cognitiveLevels, + }); + res.json({ res: `Added Practical with ID ${newPractical.id}` }); + } catch (error) { + logger.error("Error while inserting Practical", error); + res.status(500); + res.json({ err: "Error while inserting Practical in DB" }); + } + } + + // Controller function to update a Practical entity + async function updatePractical(req, res) { + const { + id, ...data + } = req.body; + try { + await updatePracticalById(id, data); + res.json({ res: `Updated Practical with ID ${id}` }); + } catch (error) { + logger.error("Error while updating Practical", error); + res.status(500); + res.json({ err: "Error while updating Practical in DB" }); + } + } + + // Controller function to get a list of Practical entities + async function getPractical(req, res) { + const filter = req.query; + const practicalList = await listPractical(filter); + res.json({ res: practicalList }); + } + + // Controller function to delete a Practical entity + async function deletePractical(req, res) { + const { practicalId } = req.params; + try { + await deletePracticalById(practicalId); + res.json({ res: `Deleted Practical with ID ${practicalId}` }); + } catch (error) { + logger.error("Error while deleting Practical", error); + res.status(500).json({ error: "Error while deleting Practical from DB" }); + } + } + + export default { + addPractical, + deletePractical, + getPractical, + updatePractical, + }; + \ No newline at end of file diff --git a/routes/practical.js b/routes/practical.js new file mode 100644 index 00000000..44771bea --- /dev/null +++ b/routes/practical.js @@ -0,0 +1,18 @@ +import express from "express"; +import practicalController from "#controller/practical"; + +const router = express.Router(); + +// Create a new Practical +router.post("/practical/create", practicalController.createPractical); + +// List Practical entities with optional filters +router.get("/practical/list", practicalController.listPractical); + +// Update Practical entities based on filters and update data +router.post("/practical/update", practicalController.updatePractical); + +// Delete Practical entities based on filters +router.post("/practical/delete", practicalController.deletePractical); + +export default router; diff --git a/services/practical.js b/services/practical.js new file mode 100644 index 00000000..e20063c7 --- /dev/null +++ b/services/practical.js @@ -0,0 +1,53 @@ +// Import Practical-related model and databaseError module +import Practical from "#models/practical"; +import databaseError from "#error/database"; + +// Service function to create a new Practical entity +export async function createPractical({ + no, title, type, hours, cognitiveLevels, +}) { + try { + const newPractical = await Practical.create({ + no, title, type, hours, cognitiveLevels, + }); + return newPractical; + } catch (error) { + throw new databaseError.DataEntryError("practical"); + } +} + +// Service function to update a Practical entity by ID +export async function updatePracticalById(id, data) { + try { + const updated = await Practical.updateOne({ _id: id }, data); + if (updated.nModified > 0) { + return updated; + } + throw new databaseError.DataEntryError("practical"); + } catch (error) { + throw new databaseError.DataEntryError("practical"); + } +} + +// Service function to retrieve a list of Practical entities based on filters +export async function listPractical(filter) { + try { + const practicalList = await Practical.find(filter); + return practicalList; + } catch (error) { + throw new databaseError.DataRetrievalError("practical"); + } +} + +// Service function to delete a Practical entity by ID +export async function deletePracticalById(practicalId) { + try { + const deleted = await Practical.deleteOne({ _id: practicalId }); + if (deleted.deletedCount > 0) { + return deleted; + } + throw new databaseError.DataDeleteError("practical"); + } catch (error) { + throw new databaseError.DataDeleteError("practical"); + } +} From 65c0d3b6bc9694c95b13746c7d0a65b2607d94cd Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:43:32 +0530 Subject: [PATCH 2/2] fixed problems with practical endpoint --- app.js | 2 +- routes/practical.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index bbd9448a..17f56aa2 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,7 @@ import usersRouter from "#routes/users"; import authRouter from "#routes/auth"; import accreditationRouter from "#routes/accreditation"; import infrastructureRouter from "#routes/infrastructure"; -import practicalRouter from "#models/practical"; +import practicalRouter from "#routes/practical"; import { identifyUser } from "#middleware/identifyUser"; diff --git a/routes/practical.js b/routes/practical.js index 44771bea..34d94a1c 100644 --- a/routes/practical.js +++ b/routes/practical.js @@ -4,10 +4,10 @@ import practicalController from "#controller/practical"; const router = express.Router(); // Create a new Practical -router.post("/practical/create", practicalController.createPractical); +router.post("/practical/create", practicalController.addPractical); // List Practical entities with optional filters -router.get("/practical/list", practicalController.listPractical); +router.get("/practical/list", practicalController.getPractical); // Update Practical entities based on filters and update data router.post("/practical/update", practicalController.updatePractical);