-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
145 lines (131 loc) · 4.15 KB
/
api.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
var express = require('express');
var app = express();
var port = process.env.PORT || 7800;
var bodParser = require('body-parser');
var mongo = require('mongodb');
var MongoClient = mongo.MongoClient
var mongourl = "mongodb+srv://ajay123:ajay123@cluster0ajay-77.kfyoa.mongodb.net/Edumato?retryWrites=true&w=majority";
var cors = require('cors');
var db;
app.use(cors());
app.use(bodParser.urlencoded({extended:true}));
app.use(bodParser.json())
app.get('/health',(req,res) => {
res.send("Api is working")
});
app.get('/',(req,res) => {
res.send(`<a href="http://localhost:7800/location" target="_blank">City</a> <br/> <a href="http://localhost:7800/mealtype" target="_blank">MealType</a> <br/> <a href="http://localhost:7800/cuisine" target="_blank">Cuisine</a> <br/> <a href="http://localhost:7800/restaurants" target="_blank">Restaurants</a> <br/> <a href="http://localhost:7800/orders" target="_blank">Orders</a>`)
})
//List of city
app.get('/location',(req,res) => {
db.collection('city').find({}).toArray((err,result) => {
if(err) throw err;
res.send(result)
})
})
//mealtype
app.get('/mealtype',(req,res) => {
db.collection('mealtype').find({}).toArray((err,result) => {
if(err) throw err;
res.send(result)
})
})
//cusine
app.get('/cuisine',(req,res) => {
db.collection('cuisine').find({}).toArray((err,result) => {
if(err) throw err;
res.send(result)
})
})
//restaurents
app.get('/restaurants',(req,res) => {
var condition = {};
if(req.query.city && req.query.mealtype){
condition = {city:req.query.city,"type.mealtype":req.query.mealtype}
}
else if(req.query.city){
condition={city:req.query.city}
} else if(req.query.mealtype){
condition={"type.mealtype":req.query.mealtype}
}
else{
condition={}
}
db.collection('restaurant').find(condition).toArray((err,result) => {
if(err) throw err;
res.send(result)
})
})
//RestaurentDetai+
app.get('/restaurantdetails/:id',(req,res) => {
var query = {_id:req.params.id}
db.collection('restaurant').find(query).toArray((err,result) => {
res.send(result)
})
})
//RestaurentList
app.get('/restaurantList/:mealtype',(req,res) => {
var condition = {};
var sort = {cost:1};
if(req.query.cuisine){
condition={"type.mealtype":req.params.mealtype,"Cuisine.cuisine":req.query.cuisine}
}else if(req.query.city){
condition={"type.mealtype":req.params.mealtype,city:req.query.city}
}else if(req.query.lcost && req.query.hcost){
condition={"type.mealtype":req.params.mealtype,cost:{$lt:Number(req.query.hcost),$gt:Number(req.query.lcost)}}
}else if(req.query.sort){
condition={"type.mealtype":req.params.mealtype}
sort = {cost:Number(req.query.sort)}
}
else{
condition= {"type.mealtype":req.params.mealtype}
}
db.collection('restaurant').find(condition).sort(sort).toArray((err,result) => {
if(err) throw err;
res.send(result)
})
})
//PlaceOrder
app.post('/placeorder',(req,res) => {
console.log(req.body);
db.collection('orders').insert(req.body,(err,result) => {
if(err) throw err;
res.send('posted')
})
})
//order
app.get('/orders',(req,res) => {
db.collection('orders').find({}).toArray((err,result) => {
if(err) throw err;
res.send(result)
})
})
///Not Require in project
//Delete Orders
app.delete('/deleteorders',(req,res) => {
db.collection('orders').remove({_id:req.body.id},(err,result) => {
if(err) throw err;
res.send('data deleted')
})
})
//Update orders
app.put('/updateorders',(req,res) => {
db.collection('orders').update({_id:req.body._id},
{
$set:{
name:req.body.name,
address:req.body.address
}
},(err,result) => {
if(err) throw err;
res.send('data updated')
})
})
MongoClient.connect(mongourl,(err,connection) => {
if(err) throw err;
db = connection.db('Edumato');
app.listen(port,(err) => {
if(err) throw err;
console.log(`Server is running on port ${port}`)
})
})