-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
382 lines (315 loc) · 12.6 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const express = require ("express");
const cors = require('cors');
require('dotenv').config();
const ObjectId = require('mongodb').ObjectId;
const { MongoClient, ServerApiVersion} = require('mongodb');
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const app = express();
const PORT = process.env.PORT || 5000;
//middleware/
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.5f7tq.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
async function run (){
try{
await client.connect();
console.log('database connected successfully');
const database = client.db("Manufacturer");
const toolsCollection = database.collection('Tools');
const userCollection = database.collection('Users');
const blogsCollection = database.collection('Blogs');
const reviewCollection = database.collection('Review');
const profileCollection = database.collection('Profile');
const ordersCollection = database.collection('Orders');
const paymentCollection = database.collection('Payments');
const messageCollection = database.collection('messages');
//orders collection
app.get("/orders",async(req,res)=>{
const orders= await ordersCollection.find().toArray();
res.send(orders);
})
app.get("/orders/:email",async(req,res)=>{
const email = req.params.email;
const filter ={email:email};
const orders= await ordersCollection.find(filter).toArray();
res.send(orders);
})
app.delete("/orders/:id",async(req,res)=>{
const id = req.params.id;
const filter ={_id:ObjectId(id)};
const result= await ordersCollection.deleteOne(filter);
res.send(result);
})
app.put('/orders', async (req, res) => {
const email = req.query.email;
const productName = req.query.productName;
const order = req.body;
const filter = {
email: email,
productName : productName,
};
const options = { upsert: true };
const updateDoc = {
$set: order,
};
const result = await ordersCollection.updateOne(filter, updateDoc, options);
// const token = jwt.sign({email:email},process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1h' });
// res.send({ result,token});
res.send(result);
})
app.patch('/orders/:id', async (req, res) => {
const id = req.params.id;
const payment = req.body;
const order = req.body;
const filter = {
_id: (ObjectId(id)),
};
const updateDoc = {
$set: {
paymentStatus: "complete",
transecionId : order.transecionId,
},
};
const payments = await paymentCollection.insertOne(payment);
const result = await ordersCollection.updateOne(filter, updateDoc);
// const token = jwt.sign({email:email},process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1h' });
// res.send({ result,token});
res.send(updateDoc);
});
//client get with email api
app.get('/orders/email/productName', async (req, res)=>{
let query = {}
const email = req.query.email;
const productName = req.query.productName;
if(email){
query = {
email : email,
productName : productName,
};
}
const result =await ordersCollection.findOne(query);
res.send(result);
});
app.get("/paymentOrder/:id",async(req,res)=>{
const id = req.params.id;
const filter ={_id:ObjectId(id)};
const result= await ordersCollection.findOne(filter);
res.send(result);
})
// app.post('/orders', async (req, res) => {
// const order = req.body;
// const result = await ordersCollection.insertOne(order);
// // const token = jwt.sign({email:email},process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1h' });
// // res.send({ result,token});
// res.send(result);
// })
// create payment-intent api
app.post("/create-payment-intent", async (req, res) => {
const service = req.body;
const price = service.price;
const amount = price*100;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: "usd",
payment_method_types:['card'],
// automatic_payment_methods: {
// enabled: true,
// },
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
//users.collection
app.get("/user",async(req,res)=>{
const users= await userCollection.find().toArray();
res.send(users);
})
app.delete("/user/:id",async(req,res)=>{
const id = req.params.id;
const filter ={_id : ObjectId(id)};
const result= await userCollection.deleteOne(filter);
res.send(result);
})
app.put('/user/:email', async (req, res) => {
const email = req.params.email;
const user = req.body;
const filter = { email: email };
const options = { upsert: true };
const updateDoc = {
$set: user,
};
const result = await userCollection.updateOne(filter, updateDoc, options);
// const token = jwt.sign({email:email},process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1h' });
// res.send({ result,token});
res.send(result);
})
//Admin Api
app.put('/user/admin/:email',async (req, res) => {
const email = req.params.email;
const filter = { email: email };
const updateDoc = {
$set: {role:'admin'}, //here we set a role for admins
};
const result = await userCollection.updateOne(filter, updateDoc);
res.send({ result});
})
app.get('/admin/:email',async(req,res)=>{
const email = req.params.email;
const query = {email:email};
const user = await userCollection.findOne(query);
const isAdmin = user.role ==='admin';
res.send({admin : isAdmin})
})
//messages
app.post('/message', async (req, res) => {
const message = req.body;
const result = await messageCollection.insertOne(message);
res.send(result);
});
app.get('/message', async (req, res) => {
const message = await messageCollection.find().toArray();
res.send(message);
});
app.delete("/message/:id",async(req,res)=>{
const id = req.params.id;
const filter ={_id : ObjectId(id)};
const result= await messageCollection.deleteOne(filter);
res.send(result);
})
//review
app.post('/review', async (req, res) => {
const review = req.body;
const result = await reviewCollection.insertOne(review);
res.send(result);
});
app.get('/review', async (req, res) => {
const review = await reviewCollection.find().toArray();
res.send(review);
});
app.delete("/review/:id",async(req,res)=>{
const id = req.params.id;
const filter ={_id : ObjectId(id)};
const result= await reviewCollection.deleteOne(filter);
res.send(result);
})
//blogs
app.post('/blog', async (req, res) => {
const blog = req.body;
const result = await blogsCollection.insertOne(blog);
res.send(result);
});
app.get('/blog', async (req, res) => {
const blog = await blogsCollection.find().toArray();
res.send(blog);
});
app.delete("/blog/:id",async(req,res)=>{
const id = req.params.id;
const filter ={_id : ObjectId(id)};
const result= await blogsCollection.deleteOne(filter);
res.send(result);
})
app.put('/blog/:id', async (req, res) => {
const id = req.params.id;
const updateBlog = req.body;
const filter ={_id : ObjectId(id)};
const options={upsert:true};
const updatedDoc ={
$set:{
title : updateBlog.title,
details : updateBlog.details,
img : updateBlog.img,
date : updateBlog.date,
}
};
const result = await blogsCollection.updateOne(filter,updatedDoc,options);
res.send(result);
});
app.get('/blog/:id', async (req, res) => {
const id = req.params.id;
const filter ={_id:ObjectId(id)};
const result = await blogsCollection.findOne(filter);
res.send(result);
});
// tools
app.get('/tools',async(req,res)=>{
const query={};
const cursor = toolsCollection.find(query);
const services = await cursor.toArray();
res.send(services);
})
app.post('/tools', async (req, res) => {
const tools = req.body;
const result = await toolsCollection.insertOne(tools);
res.send(result);
});
app.put('/tools/:id', async (req, res) => {
const id = req.params.id;
const updateTools = req.body;
const filter ={_id : ObjectId(id)};
const options={upsert:true};
const updatedDoc ={
$set:{
name : updateTools.name,
pricePerUnit: updateTools.pricePerUnit,
minOrderQuantity :updateTools.minOrderQuantity,
availableQuantity : updateTools.availableQuantity,
img : updateTools.img,
details : updateTools.details,
catagory : updateTools.catagory,
// or just - updateTools
}
};
const result = await toolsCollection.updateOne(filter,updatedDoc,options);
res.send(result);
});
app.delete('/tools/:id', async (req, res) => {
const id = req.params.id;
const filter ={_id:ObjectId(id)};
const result = await toolsCollection.deleteOne(filter);
res.send(result);
});
app.get('/tools/:catagory', async (req, res) => {
const catagory = req.params.catagory;
const filter ={catagory:catagory};
const cursor = toolsCollection.find(filter);
const result = await cursor.toArray();
res.send(result);
});
app.get('/tools/product/:name', async (req, res) => {
const name = req.params.name;
const filter ={name:name};
const result = await toolsCollection.findOne(filter);
res.send(result);
});
//history
app.get('/history',async(req,res)=>{
const query={};
const cursor = paymentCollection.find(query);
const payments = await cursor.toArray();
res.send(payments);
})
app.get('/myhistory/:email', async (req, res) => {
const email = req.params.email;
const filter ={email:email};
const cursor = paymentCollection.find(filter);
const result = await cursor.toArray();
res.send(result);
});
app.delete('/history/:id', async (req, res) => {
const id = req.params.id;
const filter ={_id:ObjectId(id)};
const result = await paymentCollection.deleteOne(filter);
res.send(result);
});
}
finally{
}
}
run().catch(console.dir);
app.get('/',(req,res)=>{
res.send('Hello World!')
});
app.listen(PORT, ()=>console.log(`server is running on Port ${PORT}`))