Skip to content

Commit 9ea9565

Browse files
committed
#118 Added new files and add route in app file
1 parent 05572c4 commit 9ea9565

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
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

1213
const app = express();
1314
const currDirName = dirname(fileURLToPath(import.meta.url));
@@ -26,5 +27,6 @@ app.use(express.static(path.join(currDirName, "public")));
2627
app.use("/", indexRouter);
2728
app.use("/users", usersRouter);
2829
app.use("/auth", authRouter);
30+
app.use("/accreditation", accreditationRouter);
2931

3032
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 };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
2323
"test:watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --watch",
2424
"test:openHandels": "NODE_OPTIONS=--experimental-vm-modules npx jest --detectOpenHandles",
25-
"eslint": "eslint '**/*.js'"
25+
"eslint": "eslint ."
2626
},
2727
"dependencies": {
2828
"apidoc": "^1.0.3",

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)