-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (111 loc) · 3.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const express = require('express');
const app = express();
const port = 3500;
const fs = require("fs");
app.use(express.json());
// middleware to read file
function readFile(req, res, next)
{
req.data = JSON.parse(fs.readFileSync("./db.json","utf-8"));
next();
}
//middleware to write file
function writeFile(req, res, next)
{
fs.writeFileSync("./db.json", JSON.stringify(req.data));
next();
}
//middleware to find student index
function findIndexStudent( req, res, next){
const student = req.data["students"].find((student) => student.id === parseInt(req.params.id));
if(student)
{
req.studentIndex = req.data["students"].indexOf(student);
}
else
{
req.status(404).json({"message":"Not found"});
}
next();
}
//middleware to find admin index
function findIndexAdmin( req, res, next){
const admin = req.data["admins"].find((admin) => admin.id === parseInt(req.params.id));
if(admin)
{
req.adminIndex = req.data["admins"].indexOf(admin);
}
else
{
req.status(404).json({"message":"Not found"});
}
next();
}
app.get("/students", readFile, (req, res) =>{
res.status(200).json(req.data["students"]);
});
app.get("/admins", readFile, (req, res) =>{
res.status(200).json(req.data["admins"]);
});
app.post("/students", readFile, (req, res, next) =>{
console.log(req.body.id);
const {body} = req;
req.data["students"].push(body);
next();
}, writeFile, (req, res) =>{
console.log("Successfully inserted");
res.end();
})
app.post("/admins", readFile, (req, res, next) =>{
const {body} = req;
req.data["admins"].push(body);
next();
}, writeFile, (req, res) =>{
res.end();
});
app.put("/students/:id", readFile, findIndexStudent, (req, res, next) =>{
const {body} = req;
console.log(body);
req.data["students"][req.studentIndex] = {id:parseInt(req.params.id),...body};
next();
}, writeFile, (req, res) =>{
res.end();
})
app.put("/admins/:id", readFile, findIndexAdmin, (req, res, next) =>{
const {body} = req;
req.data["admins"][req.adminIndex] = { id:parseInt(req.params.id), ...body};
next();
}, writeFile, (req, res) =>{
res.end();
})
app.patch("/students/:id", readFile, findIndexStudent, (req, res, next) =>{
const {body} = req;
req.data["students"][req.studentIndex] = { ...req.data["students"][req.studentIndex], ...body};
next();
},writeFile, (req, res) =>{
res.end();
})
app.patch("/admins/:id", readFile, findIndexAdmin, (req, res, next) =>{
const {body} = req;
req.data["admins"][req.adminIndex] = { ...req.data["admins"][req.adminIndex], ...body};
next();
},writeFile, (req, res) =>{
res.end();
});
app.delete("/students/:id", readFile,findIndexStudent, (req, res, next) =>{
console.log()
req.data["students"].splice(res.studentIndex,1);
next();
}, writeFile, (req, res) =>{
res.end();
});
app.delete("/admins/:id", readFile,findIndexAdmin, (req, res, next) =>{
console.log()
req.data["admins"].splice(res.adminIndex,1);
next();
}, writeFile, (req, res) =>{
res.end();
})
app.listen(port, () =>{
console.log(`listening to server at port : ${port}`);
})