Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]-Added endpoints for practical model #306

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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;
68 changes: 68 additions & 0 deletions controller/practical.js
Original file line number Diff line number Diff line change
@@ -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,
};

18 changes: 18 additions & 0 deletions routes/practical.js
Original file line number Diff line number Diff line change
@@ -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;
53 changes: 53 additions & 0 deletions services/practical.js
Original file line number Diff line number Diff line change
@@ -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");
}
}
Loading