Skip to content

Commit e2db853

Browse files
authored
Merge pull request #153 from tcet-opensource/118-create-add-endpoint-for-accreditation
#118 Added new files and added a new route in app.js file
2 parents ac06611 + eda2eb9 commit e2db853

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { logger } from "#util";
88
import indexRouter from "#routes/index";
99
import usersRouter from "#routes/users";
1010
import authRouter from "#routes/auth";
11+
import accreditationRouter from "#routes/accreditation";
1112
import infrastructureRouter from "#routes/infrastructure";
1213

1314
const app = express();
@@ -27,6 +28,7 @@ app.use(express.static(path.join(currDirName, "public")));
2728
app.use("/", indexRouter);
2829
app.use("/users", usersRouter);
2930
app.use("/auth", authRouter);
31+
app.use("/accreditation", accreditationRouter);
3032
app.use("/infrastructure", infrastructureRouter);
3133

3234
export default app;

controller/accreditation.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { addNewAccreditation } from "#services/accreditation";
2+
import { logger } from "#util";
3+
4+
async function addAccreditation(req, res) {
5+
const {
6+
name, agencyName, dateofAccreditation, dateofExpiry,
7+
} = req.body;
8+
try {
9+
// eslint-disable-next-line max-len
10+
const accreditation = await addNewAccreditation(name, agencyName, dateofAccreditation, dateofExpiry);
11+
res.json({ res: `added accreditation ${accreditation.name}` });
12+
} catch (error) {
13+
logger.error("Error while inserting", error);
14+
res.status(500);
15+
res.json({ err: "Error while inserting in DB" });
16+
}
17+
}
18+
19+
export default { addAccreditation };

routes/accreditation.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import express from "express";
2+
import accreditationController from "#controller/accreditation";
3+
4+
const router = express.Router();
5+
router.post("/add", accreditationController.addAccreditation);
6+
export default router;

services/accreditation.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import accreditation from "#models/accreditation";
2+
import databaseError from "#error/database";
3+
4+
export async function addNewAccreditation(name, agencyName, dateofAccreditation, dateofExpiry) {
5+
const newAccreditation = await accreditation.create(
6+
name,
7+
agencyName,
8+
dateofAccreditation,
9+
dateofExpiry,
10+
);
11+
if (newAccreditation.name === name) {
12+
return newAccreditation;
13+
}
14+
throw new databaseError.DataEntryError("Accreditation");
15+
}

0 commit comments

Comments
 (0)