-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (121 loc) · 3.41 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require("dotenv").config();
const express = require("express");
//mongoose connection
const connectDB = require("./connection");
// mongoose model
const deviceModel = require("./device");
const app = express();
// configuration
app.use(express.json());
// GET
// route: /
// description: To get all device
// parameter: none
app.get("/", async (req, res) => {
const device = await deviceModel.find();
return res.json({device});
});
// GET
// route: /device/:_id
// description: To get device with 'id'
// parameter: _id
app.get("/device/:_id", async (req, res) => {
try{
const { _id } = req.params;
const device = await deviceModel.findById(_id);
if (!device){
return res.json ({message: "invalid ID"});
}
return res.json ({ device });
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// GET
// route: /device/name/:name
// description: To get all device with 'name'
// parameter: name
app.get("/device/name/:name", async (req, res) => {
try {
const { name } = req.params;
const device = await deviceModel.find({name: name});
if (!device){
return res.json({message: "No device found"});
}
return res.json ({ device });
}
catch(error) {
return res.status(500).json({error: error.message});
}
});
// POST
// route: /device/new
// description: To add new device
// parameter: none
// request body: device object
app.post("/device/new", async(req, res) => {
try{
const { newdevice } = req.body;
await deviceModel.create(newdevice);
const { _id } = req.params;
return res.json({message: "Device Created"});
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// PUT
// route: /device/update/:_id
// description: To update a device
// parameter: _id
// request body: device object
app.put("/device/update/:_id", async (req, res) => {
try {
const { _id } = req.params;
const { deviceData } = req.body;
const updatedevice = await deviceModel.findByIdAndUpdate(
_id,
{ $set: deviceData},
{ new: true}
);
return res.json({device: updatedevice});
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// DELETE
// route: /device/delete/:_id
// description: To delete a device
// parameter:
// request body: none
app.delete("/device/delete/:_id", async (req, res) => {
try{
const { _id } = req.params;
await deviceModel.findByIdAndDelete(_id);
return res.json({message: "device Deleted 🔪"});
}
catch(error){
return res.status(500).json({error: error.message});
}
});
// route: /device/delete/name/:name
// description: To delete a device with name
// parameter: name
// request body: none
app.delete("/device/delete/name/:name", async (req, res) => {
try {
const { name } = req.params;
await deviceModel.findOneAndDelete({ name });
return res.json({message: "Device Deleted 🔪"})
}
catch(error){
return res.status(500).json({error: error.message});
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => connectDB()
.then(() => console.log("Server is Running 🚀"))
.catch((error) => console.log(error))
);