Skip to content

Commit

Permalink
Merge pull request #302 from tcet-opensource/277-create-all-endpoints…
Browse files Browse the repository at this point in the history
…-for-assingment

277 create all endpoints for assingment
  • Loading branch information
TejasNair9977 authored Sep 9, 2023
2 parents 9bbeff2 + a8a6709 commit 9758649
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import usersRouter from "#routes/users";
import authRouter from "#routes/auth";
import accreditationRouter from "#routes/accreditation";
import infrastructureRouter from "#routes/infrastructure";
import assignmentRouter from "#routes/assignment";
import timetableRouter from "#routes/timetable";
import courseworkRouter from "#routes/coursework";
import moduleRouter from "#routes/module";
Expand Down Expand Up @@ -37,6 +38,7 @@ app.use("/users", usersRouter);
app.use("/auth", authRouter);
app.use("/accreditation", accreditationRouter);
app.use("/infrastructure", infrastructureRouter);
app.use("/assignment", assignmentRouter);
app.use("/timetable", timetableRouter);
app.use("/department", departmentRouter);
app.use("/coursework", courseworkRouter);
Expand Down
53 changes: 53 additions & 0 deletions controller/assignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
createAssignment, deleteAssignmentById, assignmentList, updateAssignmentById,
} from "#services/assignment";
import { logger } from "#util";

async function addAssignment(req, res) {
const {
no, title, type, marks,
} = req.body;
try {
const newAssignment = await createAssignment(no, title, type, marks);
res.json({ res: `added user ${newAssignment.id}` });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}

async function updateAssignment(req, res) {
const {
id, ...data
} = req.body;
try {
await updateAssignmentById(id, data);
res.json({ res: `updated assignment with id ${id}` });
} catch (error) {
logger.error("Error while updating", error);
res.status(500);
res.json({ err: "Error while updaing in DB" });
}
}

async function getAssignment(req, res) {
const filter = req.query;
const assingmentlist = await assignmentList(filter);
res.json({ res: assingmentlist });
}

async function deleteAssignment(req, res) {
const { assignmentId } = req.params;
try {
await deleteAssignmentById(assignmentId);

res.json({ res: `Deleted assignment with ID ${assignmentId}` });
} catch (error) {
logger.error("Error while deleting", error);
res.status(500).json({ error: "Error while deleting from DB" });
}
}
export default {
addAssignment, deleteAssignment, getAssignment, updateAssignment,
};
10 changes: 10 additions & 0 deletions routes/assignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from "express";
import assingmentController from "#controller/assignment";

const router = express.Router();
router.post("/add", assingmentController.addAssignment);
router.get("/list", assingmentController.getAssignment);
router.post("/update", assingmentController.updateAssignment);
router.post("/delete", assingmentController.deleteAssignment);

export default router;
33 changes: 33 additions & 0 deletions services/assignment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Assignment from "#models/assignment";
import databaseError from "#error/database";

export async function createAssignment(no, title, type, marks) {
const newAssignment = await Assignment.create({
no, title, type, marks,
});
if (newAssignment.name === name) {
return newAssignment;
}
throw new databaseError.DataEntryError("assignment");
}

export async function updateAssignmentById(id, data) {
const updated = await Assignment.update({ _id: id }, data);
if (updated) {
return updated;
}
throw new databaseError.DataEntryError("assignment");
}

export async function assignmentList(filter) {
const assignmentlist = await Assignment.read(filter, 0);
return assignmentList;
}

export async function deleteAssignmentById(assignmentId) {
const deleted = await Assignment.remove({ _id: assignmentId});
if (deleted) {
return deleted;
}
throw new databaseError.DataDeleteError("assingment");
}

0 comments on commit 9758649

Please sign in to comment.