Skip to content

Commit 24cb4ef

Browse files
authored
Merge pull request #149 from Vikrantsingh22/123-create_add_endpoint_for_infrastructure
#123 feat: Add infrastructure route, controller, model, and service files
2 parents d6ce031 + e20750d commit 24cb4ef

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-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 infrastructureRouter from "#routes/infrastructure";
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("/infrastructure", infrastructureRouter);
2931

3032
export default app;

controller/infrastructure.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createinfrastructure } from "#services/infrastructure";
2+
import { logger } from "#util";
3+
4+
async function addinfrastructure(req, res) {
5+
const {
6+
name, type, wing, floor, capacity,
7+
} = req.body;
8+
try {
9+
const newinfrastructure = await createinfrastructure(name, type, wing, floor, capacity);
10+
res.json({ res: `added user ${newinfrastructure.id}` });
11+
} catch (error) {
12+
logger.error("Error while inserting", error);
13+
res.status(500);
14+
res.json({ err: "Error while inserting in DB" });
15+
}
16+
}
17+
18+
export default { addinfrastructure };

models/infrastructure.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import connector from "#models/databaseUtil";
2+
3+
const infrastructureSchema = {
4+
name: { type: String, required: true },
5+
type: { type: String, required: true },
6+
wing: { type: String, required: true },
7+
floor: { type: Number, required: true },
8+
capacity: { type: Number, required: true },
9+
};
10+
11+
const Infrastructure = connector.model("Infrastructure", infrastructureSchema);
12+
13+
async function remove(filter) {
14+
const res = await Infrastructure.findOneAndDelete(filter);
15+
return res;
16+
}
17+
18+
async function create(name, type, wing, floor, capacity) {
19+
const infrastructure = new Infrastructure({
20+
name,
21+
type,
22+
wing,
23+
floor,
24+
capacity,
25+
});
26+
const infrastructureDoc = await infrastructure.save();
27+
return infrastructureDoc;
28+
}
29+
30+
async function read(filter, limit = 1) {
31+
const infrastructureData = await Infrastructure.find(filter).limit(limit);
32+
return infrastructureData;
33+
}
34+
35+
async function update(filter, updateObject) {
36+
const infrastructure = await Infrastructure.findOneAndUpdate(filter, updateObject, { new: true });
37+
return infrastructure;
38+
}
39+
40+
export default {
41+
create, read, update, remove,
42+
};

routes/infrastructure.js

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

services/infrastructure.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import infrastructure from "#models/infrastructure";
2+
import databaseError from "#error/database";
3+
4+
export async function createinfrastructure(name, type, wing, floor, capacity) {
5+
const newinfrastructure = await infrastructure.create(name, type, wing, floor, capacity);
6+
if (newinfrastructure.name === name) {
7+
return newinfrastructure;
8+
}
9+
throw new databaseError.DataEntryError("infrastructure");
10+
}

0 commit comments

Comments
 (0)