Skip to content
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
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import authRouter from "#routes/auth";
import accreditationRouter from "#routes/accreditation";
import infrastructureRouter from "#routes/infrastructure";
import { identifyUser } from "#middleware/identifyUser";
import departmentRouter from "#routes/department";

const app = express();
const currDirName = dirname(fileURLToPath(import.meta.url));
Expand All @@ -33,5 +34,5 @@ app.use("/users", usersRouter);
app.use("/auth", authRouter);
app.use("/accreditation", accreditationRouter);
app.use("/infrastructure", infrastructureRouter);

app.use("/department", departmentRouter);
export default app;
78 changes: 78 additions & 0 deletions controller/department.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
updateDepartmentbyid, createnewdepartment, listdepartment, deletedepartment,
} from "#services/department";

import { logger } from "#util";

async function addDepartment(req, res) {
const {
name,
acronym,
yearOfStarting,
accreditations,
infrastructures,
} = req.body;
try {
const department = await createnewdepartment(
name,
acronym,
yearOfStarting,
accreditations,
infrastructures,
);
res.json({
res: `added Department successfully ${department.name}`,
});
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}

async function removedepartmentbyid(req, res) {
const { departmentId } = req.params;
try {
await deletedepartment(departmentId);
res.json({
res: "Department deleted successfully",
});
} catch (error) {
logger.error("Error while deleting", error);
res.status(500);
res.json({ err: "Error while deleting from DB" });
}
}

async function showdepartments(req, res) {
try {
const departments = await listdepartment(req.query);
return res.json({
res: departments,
});
} catch (error) {
logger.error("Error while fetching", error);
res.status(500);
return res.json({ err: "Error while fetching the data" });
}
}

async function updatedDepartment(req, res) {
const {
id, data,
} = req.body;
try {
await updateDepartmentbyid(id, data);
res.json({
res: "department updated successfully",
});
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}

export default {
updatedDepartment, showdepartments, removedepartmentbyid, addDepartment,
};
11 changes: 11 additions & 0 deletions routes/department.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from "express";
import departmentContoller from "#controller/department";

const router = express.Router();

router.get("/list", departmentContoller.showdepartments);
router.post("/create", departmentContoller.addDepartment);
router.delete("/delete/:departmentId", departmentContoller.removedepartmentbyid);
router.post("/update", departmentContoller.updatedDepartment);

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

export async function createnewdepartment(
name,
acronym,
yearOfStarting,
accreditations,
infrastructures,
) {
const newdepartment = await department.create({
name,
acronym,
yearOfStarting,
accreditations,
infrastructures,
});
if (newdepartment.name === name) {
return newdepartment;
}
throw new databaseError.DataEntryError("Add department");
}

export async function listdepartment(filter) {
const listeddepartment = await department.read(filter);
if (listeddepartment) {
return listeddepartment;
}
throw new databaseError.DataNotFoundError("Department");
}

export async function deletedepartment(departmentId) {
const deletedDepartment = await department.remove({
_id: departmentId,
});
if (deletedDepartment) {
return deletedDepartment;
}
throw new databaseError.DataDeleteError("department");
}

export async function updateDepartmentbyid(id, data) {
const updatedDepartment = await department.update({
_id: id,
}, data);
if (updatedDepartment) {
return updatedDepartment;
}
throw new databaseError.DataEntryError("department");
}

export default {
updateDepartmentbyid, createnewdepartment, listdepartment, deletedepartment,
};