Skip to content

Commit a3005b3

Browse files
Merge pull request #311 from tcet-opensource/250-endpoints-for-organization
250 Create all endpoints for organization
2 parents cad89da + db56f7b commit a3005b3

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import usersRouter from "#routes/users";
1010
import authRouter from "#routes/auth";
1111
import accreditationRouter from "#routes/accreditation";
1212
import infrastructureRouter from "#routes/infrastructure";
13+
import organizationRouter from "#routes/organization";
1314
import studentRouter from "#routes/student";
1415
import tutorialRouter from "#routes/tutorial";
1516
import assignmentRouter from "#routes/assignment";
@@ -40,6 +41,7 @@ app.use("/users", usersRouter);
4041
app.use("/auth", authRouter);
4142
app.use("/accreditation", accreditationRouter);
4243
app.use("/infrastructure", infrastructureRouter);
44+
app.use("/organization", organizationRouter);
4345
app.use("/student", studentRouter);
4446
app.use("/tutorial", tutorialRouter);
4547
app.use("/assignment", assignmentRouter);

controller/organization.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
addNewOrganization, deleteOrganizationById, updateOrganizationById, getOrganizations,
3+
} from "#services/organization";
4+
import { logger } from "#util";
5+
6+
async function addOrganization(req, res) {
7+
const {
8+
parent, startDate, name, accreditation,
9+
} = req.body;
10+
try {
11+
const organization = await addNewOrganization( parent, startDate, name, accreditation,);
12+
res.json({ res: `added organization${organization.name}` });
13+
} catch (error) {
14+
logger.error("Error while inserting", error);
15+
res.status(500);
16+
res.json({ err: "Error while inserting in DB" });
17+
}
18+
}
19+
async function deleteOrganization(req, res) {
20+
const { organizationId } = req.params;
21+
try {
22+
await deleteOrganizationById(organizationId);
23+
res.json({ res: "Organization deleted successfully" });
24+
} catch (error) {
25+
logger.error("Error while deleting", error);
26+
res.status(500);
27+
res.json({ err: "Error while deleting from DB" });
28+
}
29+
}
30+
31+
async function updateOrganization(req, res) {
32+
const {
33+
id, ...data
34+
} = req.body;
35+
36+
try {
37+
await updateOrganizationById(id, data);
38+
res.json({ res: "organization updated" });
39+
} catch (error) {
40+
logger.error("Error while inserting", error);
41+
res.status(500);
42+
res.json({ err: "Error while inserting in DB" });
43+
}
44+
}
45+
46+
async function showOrganization(req, res) {
47+
try {
48+
const organization = await getOrganizations(req.query);
49+
return res.json({ res: organization });
50+
} catch (error) {
51+
logger.error("Error while fetching", error);
52+
res.status(500);
53+
return res.json({ err: "Error while fetching the data" });
54+
}
55+
}
56+
57+
export default {
58+
addOrganization, updateOrganization, deleteOrganization, showOrganization,
59+
};
60+

routes/organization.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import express from "express";
2+
import organizationController from "#controller/organization";
3+
4+
const router = express.Router();
5+
router.get("/list", organizationController.showOrganization);
6+
router.post("/add", organizationController.addOrganization);
7+
router.delete("/delete/:organizationId", organizationController.deleteOrganization);
8+
router.post("/update", organizationController.updateOrganization);
9+
10+
export default router;

services/organization.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Organization from "#models/organization";
2+
import databaseError from "#error/database";
3+
4+
export async function addNewOrganization(parent, startDate, name, accreditation) {
5+
const newOrganization = await Organization.create({
6+
parent, startDate, name, accreditation,
7+
});
8+
if (newOrganization.name === name) {
9+
return newOrganization;
10+
}
11+
throw new databaseError.DataEntryError("Add Organization");
12+
}
13+
14+
export async function getOrganizations(filter) {
15+
const organization = await Organization.read(filter);
16+
if (organization) {
17+
return organization;
18+
}
19+
throw new databaseError.DataNotFoundError("Organization");
20+
}
21+
22+
export async function deleteOrganizationById(organizationId) {
23+
const deleted = await Organization.remove({ _id: organizationId });
24+
if (deleted) {
25+
return deleted;
26+
}
27+
throw new databaseError.DataDeleteError("Organization");
28+
}
29+
30+
export async function updateOrganizationById(id, data) {
31+
const updated = await Organization.update({ _id: id }, data);
32+
if (updated) {
33+
return updated;
34+
}
35+
throw new databaseError.DataEntryError("Organization");
36+
}
37+
38+
export default {
39+
deleteOrganizationById, addNewOrganization, updateOrganizationById,getOrganizations,
40+
};

0 commit comments

Comments
 (0)