-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
287 lines (258 loc) · 8.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const express = require("express");
const cors = require("cors");
const jwt = require("jsonwebtoken");
const app = express();
const port = process.env.PORT || 12002;
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const cookieParser = require("cookie-parser");
require("dotenv").config();
// Middleware
app.use(
cors({
origin: [
"https://restaurent-managment-client.netlify.app",
"http://localhost:5173",
"http://localhost:5174",
],
credentials: true,
})
);
app.use(express.json());
app.use(cookieParser());
const uri = `mongodb+srv://${process.env.DB_NAME}:${process.env.DB_PASSWORD}@cluster0.dn60035.mongodb.net/?retryWrites=true&w=majority`;
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
// middlewares
const logger = async (req, res, next) => {
console.log("called", req.host, req.originalUrl);
next();
};
const verifyToken = async (req, res, next) => {
const token = req.cookies?.accessToken;
console.log("Value of token in middleware", token);
if (!token) {
return res.status(401).send({ message: "Forbidden" });
}
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET, (err, decoded) => {
// error
if (err) {
console.log("jwt.verify", err);
return res.status(401).send({ message: "Unauthorized it" });
}
// if token is valid then it would be decoded
console.log("value in the token", decoded);
req.user = decoded;
next();
});
};
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
// await client.connect();
const database = client.db("insertDB");
const usersCollection = database.collection("users");
const allFoodCollection = database.collection("allFood");
const orderFoodCollection = database.collection("orderFood");
// Send a ping to confirm a successful connection
await client.db("admin").command({ ping: 1 });
console.log(
"Pinged your deployment. You successfully connected to MongoDB!"
);
// Auth related Api
app.post("/jwt", logger, async (req, res) => {
const user = req.body;
// console.log(user);
const token = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET, {
expiresIn: "1h",
});
res
.cookie("accessToken", token, {
// httpOnly: true,
// secure: false,
// sameSite: "none",
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: process.env.NODE_ENV === "production" ? "none" : "strict",
})
.send({ success: true });
});
// server related Api
app.post("/users", async (req, res) => {
const user = req.body;
console.log("new user", user);
const result = await usersCollection.insertOne(user);
// console.log(result);
res.send(result);
});
app.get("/users", async (req, res) => {
const cursor = usersCollection.find();
const result = await cursor.toArray();
res.send(result);
});
app.post("/all-food", async (req, res) => {
const addedFood = req.body;
// console.log("new added Food", addedFood);
const result = await allFoodCollection.insertOne(addedFood);
// console.log(result);
res.send(result);
});
app.get("/all-food", async (req, res) => {
console.log(req.query);
const page = parseInt(req.query.page);
const size = parseInt(req.query.size);
// console.log("pagination", page, size);
const cursor = allFoodCollection
.find()
.skip(page * size)
.limit(size);
const result = await cursor.toArray();
// console.log(result);
res.send(result);
});
app.get("/all-food-count", async (req, res) => {
const count = await allFoodCollection.estimatedDocumentCount();
res.send({ count });
});
app.get("/all-food/id/:id", logger, async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await allFoodCollection.findOne(query);
res.send(result);
});
app.get("/my-added-food", logger, verifyToken, async (req, res) => {
console.log(req.query.userEmail);
// console.log("ttttt token", req.cookies.accessToken);
console.log("user in the valid token", req.user);
if (req.query.userEmail !== req.user.email) {
return res.status(403).send({ message: "Forbidden access" });
}
let query = {};
if (req.query?.userEmail) {
query = { userEmail: req.query?.userEmail };
}
const cursor = allFoodCollection.find(query);
const result = await cursor.toArray();
res.send(result);
});
app.put("/all-food/id/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const options = { upsert: true };
const updatedFood = req.body;
const product = {
$set: {
food_image: updatedFood.food_image,
food_name: updatedFood.food_name,
food_origin: updatedFood.food_origin,
food_category: updatedFood.food_category,
price: updatedFood.price,
description: updatedFood.description,
made_by: updatedFood.made_by,
add_by: updatedFood.add_by,
quantity: updatedFood.quantity,
},
};
const result = await allFoodCollection.updateOne(
filter,
product,
options
);
res.send(result);
// console.log(updatedFood);
});
app.put("/all-food-purchase/id/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
const options = { upsert: true };
const updatedFood = req.body;
const product = {
$set: {
quantity: updatedFood.quantity,
sold: updatedFood.sold,
},
};
const result = await allFoodCollection.updateOne(
filter,
product,
options
);
res.send(result);
// console.log(updatedFood);
});
app.put("/all-food-purchase-cancel/id/:id", async (req, res) => {
const id = req.params.id;
const filter = { _id: new ObjectId(id) };
// const options = { upsert: true };
const updatedFood = req.body;
const product = {
$set: {
quantity: updatedFood.quantity,
},
};
const result = await allFoodCollection.updateOne(
filter,
product
// options
);
res.send(result);
// console.log(updatedFood);
});
app.post("/order-food", async (req, res) => {
const orderFood = req.body;
// console.log(orderFood);
const result = await orderFoodCollection.insertOne(orderFood);
res.send(result);
});
app.get("/order-food", logger, verifyToken, async (req, res) => {
console.log(req.query.userEmail);
console.log("user in the valid token", req.user);
if (req.query.userEmail !== req.user.email) {
return res.status(403).send({ message: "Forbidden access" });
}
let query = {};
if (req.query?.userEmail) {
query = { userEmail: req.query?.userEmail };
}
const cursor = orderFoodCollection.find(query);
const result = await cursor.toArray();
res.send(result);
});
app.get("/Order-food/details/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await orderFoodCollection.findOne(query);
res.send(result);
});
app.delete("/order-food/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: new ObjectId(id) };
const result = await orderFoodCollection.deleteOne(query);
res.send(result);
});
} finally {
// Ensures that the client will close when you finish/error
// await client.close();
}
}
run().catch(console.dir);
app.get("/", (req, res) => {
res.send("Restaurant server data");
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
/**
* echo "# b8a11-server-side-tayeb012" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/Porgramming-Hero-web-course/b8a11-server-side-tayeb012.git
git push -u origin main
*/