diff --git a/app.js b/app.js index 561ecda..876dfcd 100644 --- a/app.js +++ b/app.js @@ -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"; @@ -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); diff --git a/controller/organization.js b/controller/organization.js new file mode 100644 index 0000000..422c392 --- /dev/null +++ b/controller/organization.js @@ -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, + }; + \ No newline at end of file diff --git a/routes/organization.js b/routes/organization.js new file mode 100644 index 0000000..1d532dd --- /dev/null +++ b/routes/organization.js @@ -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; diff --git a/services/organization.js b/services/organization.js new file mode 100644 index 0000000..f4d4e35 --- /dev/null +++ b/services/organization.js @@ -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, +};