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
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 organizationRouter from "#routes/organization";
import studentRouter from "#routes/student";
import tutorialRouter from "#routes/tutorial";
import assignmentRouter from "#routes/assignment";
Expand Down Expand Up @@ -40,6 +41,7 @@ app.use("/users", usersRouter);
app.use("/auth", authRouter);
app.use("/accreditation", accreditationRouter);
app.use("/infrastructure", infrastructureRouter);
app.use("/organization", organizationRouter);
app.use("/student", studentRouter);
app.use("/tutorial", tutorialRouter);
app.use("/assignment", assignmentRouter);
Expand Down
60 changes: 60 additions & 0 deletions controller/organization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
addNewOrganization, deleteOrganizationById, updateOrganizationById, getOrganizations,
} from "#services/organization";
import { logger } from "#util";

async function addOrganization(req, res) {
const {
parent, startDate, name, accreditation,
} = req.body;
try {
const organization = await addNewOrganization( parent, startDate, name, accreditation,);
res.json({ res: `added organization${organization.name}` });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}
async function deleteOrganization(req, res) {
const { organizationId } = req.params;
try {
await deleteOrganizationById(organizationId);
res.json({ res: "Organization deleted successfully" });
} catch (error) {
logger.error("Error while deleting", error);
res.status(500);
res.json({ err: "Error while deleting from DB" });
}
}

async function updateOrganization(req, res) {
const {
id, ...data
} = req.body;

try {
await updateOrganizationById(id, data);
res.json({ res: "organization updated" });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
res.json({ err: "Error while inserting in DB" });
}
}

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

export default {
addOrganization, updateOrganization, deleteOrganization, showOrganization,
};

10 changes: 10 additions & 0 deletions routes/organization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from "express";
import organizationController from "#controller/organization";

const router = express.Router();
router.get("/list", organizationController.showOrganization);
router.post("/add", organizationController.addOrganization);
router.delete("/delete/:organizationId", organizationController.deleteOrganization);
router.post("/update", organizationController.updateOrganization);

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

export async function addNewOrganization(parent, startDate, name, accreditation) {
const newOrganization = await Organization.create({
parent, startDate, name, accreditation,
});
if (newOrganization.name === name) {
return newOrganization;
}
throw new databaseError.DataEntryError("Add Organization");
}

export async function getOrganizations(filter) {
const organization = await Organization.read(filter);
if (organization) {
return organization;
}
throw new databaseError.DataNotFoundError("Organization");
}

export async function deleteOrganizationById(organizationId) {
const deleted = await Organization.remove({ _id: organizationId });
if (deleted) {
return deleted;
}
throw new databaseError.DataDeleteError("Organization");
}

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

export default {
deleteOrganizationById, addNewOrganization, updateOrganizationById,getOrganizations,
};