-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (64 loc) · 2.01 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
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
require('dotenv').config();
const ObjectId = require('mongodb').ObjectId;
const port = process.env.PORT || 5000
const MongoClient = require('mongodb').MongoClient;
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASS}@cluster0.4czm1.mongodb.net/${process.env.DB_NAME}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
const app = express()
app.use(cors())
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.send('Hello World!')
})
client.connect(err => {
const productCollection = client.db("freshVally").collection("product");
const orders = client.db("freshVally").collection("order");
app.post('/admin/addProduct',(req,res)=>{
productCollection.insertOne(req.body)
.then(result => {
res.send(result.insertedCount > 0)
})
})
app.get('/products',(req,res)=>{
productCollection.find({})
.toArray((err,documents)=>{
res.send(documents)
})
})
app.get('/product/:id', (req, res)=>{
productCollection.find({_id : ObjectId(req.params.id)})
.toArray((err, documents)=>{
res.send(documents[0])
})
})
app.delete('/delete/:id',(req,res)=>{
productCollection.deleteOne({_id : ObjectId(req.params.id)})
.then(result => {
res.send(result.deletedCount > 0)
})
})
app.post('/placeOrder',(req,res)=>{
orders.insertOne(req.body)
.then(result => {
res.send(result.insertedCount > 0)
})
})
app.get('/getOrders',(req,res)=>{
orders.find({email : req.query.email})
.toArray((err,document)=>{
res.send(document)
})
})
app.delete('/deleteOrder/:id',(req,res)=>{
orders.deleteOne({_id : ObjectId(req.params.id)})
.then(result => {
res.send(result.deletedCount > 0)
})
})
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})