-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (101 loc) · 3.13 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
const express = require('express')
require('dotenv').config()
const cors = require('cors')
const app = express()
const bodyParser = require('body-parser')
const port = process.env.PORT || 4200
//Database
const MongoClient = require('mongodb').MongoClient;
const uri = process.env.DB_PATH
let client = new MongoClient(uri, { useNewUrlParser: true });
//Middleware
app.use(bodyParser.json())
app.use(cors())
app.get('/', (req, res) => res.send('Server is running! Get Foods in this link /foodItems'))
app.post('/addItems', (req, res) => {
const items = req.body
client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("hot-onion").collection("foodItems");
collection.insert(items, (err, result) => {
if(err){
console.log(err);
res.status(500).send({message:err})
}
else{
res.send(result.ops[0])
}
})
client.close();
});
})
app.get('/foodItems', (req, res) => {
client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("hot-onion").collection("foodItems");
collection.find().toArray((err, documents) => {
if(err){
console.log(err);
res.status(500).send({message:err})
}
else{
res.send(documents)
}
})
client.close();
});
})
app.get('/foodItems/:key', (req, res) => {
const key = req.params.key
console.log(key)
client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("hot-onion").collection("foodItems");
collection.find({key}).toArray((err, documents) => {
if(err){
console.log(err);
res.status(500).send({message:err})
}
else{
res.send(documents)
}
})
client.close();
});
})
app.post('/itemsReviewByKey', (req, res) => {
const itemsKeys = req.body
client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("hot-onion").collection("foodItems");
collection.find({'key' : { $in :itemsKeys }}).toArray((err, documents) => {
if(err){
console.log(err);
res.status(500).send({message:err})
}
else{
res.send(documents)
}
})
client.close();
});
})
app.post('/orders', (req, res) => {
const orderDetails = req.body
orderDetails.orderTime = new Date()
client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("hot-onion").collection("orders");
collection.insert(orderDetails, (err, result) => {
if(err){
console.log(err);
res.status(500).send({message:err})
}
else{
res.send(result.ops[0])
}
})
client.close();
});
})
app.listen(port, () => console.log(` Server listening at port: ${port}`))