-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
364 lines (309 loc) · 11.4 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const express = require('express');
const cors = require('cors')
require('dotenv').config();
const jwt = require("jsonwebtoken");
const stripe = require("stripe")(process.env.STRIPE_KEY);
const app = express();
const port = process.env.PORT || 5000;
//middle ware
app.use(cors());
app.use(express.json());
app.use(express.static("public"));
app.get('/', (req, res) => {
res.send('book worm server is running')
});
//token verify
const verifyToken = (req,res,next) => {
const authorization = req.headers.authorization;
if (!authorization) {
return res.sendStatus(403);
}
const token = jwt.verify(
authorization,
process.env.TOKEN_KEY,
(err, decoded) => {
if (err) {
return res.status(403).send({message:'Please log in again'});
}
req.decoded = decoded;
return next();
}
);
}
// mongodb
const { MongoClient, ServerApiVersion, ObjectId } = require("mongodb");
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.00o20sl.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
async function run() {
try {
const categoriesCollection = client.db('books-worm').collection('categories');
const BooksCollection = client.db('books-worm').collection('books');
const UsersCollection = client.db('books-worm').collection('users');
const soldProductCollection = client.db('books-worm').collection('soldProduct');
const WishlistCollection = client.db('books-worm').collection('wishlist');
const PaymentCollection = client.db('books-worm').collection('payment');
const BlogsCollection = client.db('books-worm').collection('blogs');
app.get("/category",async (req, res) => {
const result = await categoriesCollection.find({}).toArray();
res.send(result);
});
// get all books list
app.get('/allproducts', async (req, res) => {
const result = await BooksCollection.find({}).toArray();
res.send(result);
})
// get books by category
app.get("/category/:books", async (req, res) => {
const books = req.params.books;
console.log(books)
const query = { genre: books };
const result = await BooksCollection.find(query).toArray();
res.send(result);
});
// get book details by book id
app.get("/books/:id", async (req, res) => {
const id = req.params.id;
const query = { _id:ObjectId(id) };
const result = await BooksCollection.findOne(query);
res.send(result);
});
// user stored in data base
app.post('/users', async (req, res) => {
const data = req.body.data;
const query = {email: data?.email}
const result = await UsersCollection.insertOne(data);
const token = jwt.sign(query, process.env.TOKEN_KEY, {
expiresIn: "1h",
});
return res.send({ token, result });
})
//user get
app.get("/users", async (req, res) => {
const email = req.query.email;
// const token = jwt.sign(email, process.env.TOKEN_KEY, {expiresIn:'1h'});
const query = {email}
const result = await UsersCollection.findOne(query);
if (result?.email) {
const token = jwt.sign(query, process.env.TOKEN_KEY, {
expiresIn: "1h",
});
return res.send({ token, result });
}
return res.sendStatus(403);
});
//get users with google signup
// I set different email and token so that if user log in again again with google , its doesn't add database multiple time
app.get('/google_user',async (req, res) => {
const email = req.query.email;
const query = { email };
const token = jwt.sign(query, process.env.TOKEN_KEY, {
expiresIn: "1h",
});
const result = await UsersCollection.findOne(query);
res.send({ result, token });
})
// Delete users
app.delete('/users/:id',verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await UsersCollection.deleteOne(query);
res.send(result);
})
// verify seller
app.put('/users', async (req, res) => {
const id = req.query.id;
const email = req.query.email;
const query = { _id: ObjectId(id) };
const query2 = { seller_email :email};
const updateDoc = {
$set: {
user_verified: true
}
}
const update = await BooksCollection.updateMany(query2, updateDoc, { upsert: true });
const result = await UsersCollection.updateOne(query, updateDoc, { upsert: true });
res.send(result);
})
// collect sold product
app.post("/buy", verifyToken, async (req, res) => {
const token = req.decoded.email;
// console.log(token);
const data = req.body.buyProduct;
const query = { _id: ObjectId(data?.ProductId) };
const updateDoc = {
$set: {
sold: true,
advertise:false,
},
};
const product = await BooksCollection.updateOne(query, updateDoc, {
upsert: true,
});
const result = await soldProductCollection.insertOne(data);
res.send(result);
});
// get all my product
app.get('/buy',verifyToken, async (req, res) => {
const email = req?.query.email;
const query = { BuyerEmail: email };
const result = await soldProductCollection.find(query).toArray();
res.send(result)
});
//delete product
app.delete('/buy/:id',verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const product = await soldProductCollection.findOne(query);
const query2 = { _id: ObjectId(product?.ProductId) };
const updateDoc = {
$set: {
sold: false
}
}
const update = await BooksCollection.updateOne(query2, updateDoc, { upsert: true })
const result = await soldProductCollection.deleteOne(query);
res.send(result);
});
// add product
app.post('/books',verifyToken, async (req, res) => {
const data = req.body;
const result = await BooksCollection.insertOne(data);
res.send(result);
});
// get seller product data
app.get("/products",verifyToken, async (req, res) => {
const email = req.query.email;
const query = { seller_email: email };
const result = await BooksCollection.find(query).toArray();
res.send(result);
});
//get buyer information
app.get('/buyer',verifyToken, async (req, res) => {
const email = req.query.email;
const query = { sellerEmail: email };
const result = await soldProductCollection.find(query).toArray();
res.send(result);
});
app.get('/users_type',verifyToken, async (req, res) => {
const type = req.query.type;
const query = { role: type };
// console.log(query);
const result = await UsersCollection.find(query).toArray();
// console.log(result)
res.send(result);
});
// advertise Products
app.put('/advertise/:id',verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const updateDoc = {
$set: {
advertise: true
}
};
const result = await BooksCollection.updateOne(query, updateDoc, { upsert: true });
res.send(result);
});
// delete Product
app.delete('/products/:id',verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await BooksCollection.deleteOne(query);
res.send(result);
});
// advertised product
app.get('/advertised', async (req, res) => {
const query = { advertise: true };
const result = await BooksCollection.find(query).toArray();
res.send(result);
});
// add product to wishlist
app.post('/wishlist',verifyToken, async (req, res) => {
const data = req.body;
const id = req.query.id;
const email = req.query.email;
// check data exist or not
const query = { ProductId: id, buyerEmail :email};
const checkData = await WishlistCollection.findOne(query);
console.log(checkData);
if (checkData) {
return res.send({message: 'This product is already in your wishlist'})
}
//post data
const result = await WishlistCollection.insertOne(data);
return res.send(result);
})
// get wishlist product
app.get('/wishlist',verifyToken, async (req, res) => {
const email = req.query.email;
const query = { buyerEmail: email };
const result = await WishlistCollection.find(query).toArray();
res.send(result);
})
// delete wishlist product
app.delete("/wishlist/:id",verifyToken, async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await WishlistCollection.deleteOne(query);
res.send(result);
});
// get single my product
app.get('/myproduct/:id', async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await soldProductCollection.findOne(query);
res.send(result);
})
//wishlist
app.get('/wishlist_payment/:id', async (req, res) => {
const id = req?.params?.id;
const query = { _id: ObjectId(id) };
const result = await WishlistCollection.findOne(query);
res.send(result);
})
// payments
app.post('/payments', async (req, res) => {
const data = req.body.ProductPrice;
const payment = await stripe.paymentIntents.create({
amount: `${data * 100}`,
currency: "usd",
payment_method_types: ["card"],
});
res.send({
clientSecret: payment.client_secret,
});
});
// payment stored in database
app.post('/payment_success', verifyToken, async (req, res) => {
const data = req.body;
const ProductId = data.ProductId;
const query = { _id: ObjectId(ProductId) };
const query2 = { ProductId }
const updateDoc = {
$set: {
payment: true
}
};
const product = await BooksCollection.updateOne(query, updateDoc, { upsert: true });
const soldProduct = await soldProductCollection.updateOne(query2, updateDoc, { upsert: true });
const result = await PaymentCollection.insertOne(data);
res.send(result);
});
/// blogs get
app.get('/blogs', async (req, res) => {
const query = {};
const result = await BlogsCollection.find(query).toArray();
res.send(result);
});
}
finally {
}
}
run().catch(err => console.log(err));
app.listen(port, () => {
console.log('server is running')
});