-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (134 loc) · 4.82 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
const express = require('express');
const mongoose = require('mongoose');
const mongoAtlasUri = "";
mongoose.connect(mongoAtlasUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(x => {
console.log(
`Connected to Mongo! Database name: "${x.connections[0].name}"`
);
}).catch(err => {
console.error("Error connecting to mongo", err);
});
const User = require('./mongo/models/user');
const Restaurant = require('./mongo/models/restaurant');
const Table = require('./mongo/models/table');
const Booking = require('./mongo/models/booking');
const app = express();
app.use(express.json());
//CONTROLLERS
app.get('/', async (req, res) => {
res.send("Hey")
})
app.get('/user/:userid', async (req, res) => {
const userId = req.params.userid;
let foundUser = await User.findById(userId);
if(foundUser){
res.status(200).json(foundUser)
} else {
res.status(400).json({message: "Not Found"})
}
})
app.post('/user', async (req, res) => {
try {
const newUser = new User(req.body);
let saveUser = await newUser.save();
res.status(200).json(saveUser);
} catch(err){
res.status(500).send(err.message);
}
})
app.get('/user/bookings/:userid', async (req, res) => {
const userId = req.params.userid;
let foundBooking = await Booking.find({ user_id: userId});
if (foundBooking) {
res.status(200).json(foundBooking)
} else {
res.status(400).json({ message: "Not Found" })
}
})
app.get('/bookings/:bookingid', async (req, res) => {
const bookingId = req.params.bookingid;
let foundBooking = await Booking.findById(bookingId);
if (foundBooking) {
res.status(200).json(foundBooking)
} else {
res.status(400).json({ message: "Not Found" })
}
})
app.post('/book', async (req, res) => {
const bookObject = req.body;
const restaurant = await Restaurant.findById(bookObject.restaurant_id);
const requestedDay = req.body.day;
const restaurantOpen = restaurant.working_hrs[requestedDay].start;
const restaurantClose = restaurant.working_hrs[requestedDay].end;
if (bookObject.start >= restaurantOpen && bookObject.end <= restaurantClose) {
const restaurantTables = await Table.find({ restaurant_id: bookObject.restaurant_id, 'capacity': { $gte: bookObject.capacity}});
if (restaurantTables.length > 0) {
const bookedTablesInThisTimeRange = await Booking.find({
day: requestedDay, restaurant_id: bookObject.restaurant_id,
$and: [
{ 'start': { $lt: bookObject.end}},
{ 'end': { $gt: bookObject.start }}
]
});
const freeTables = [];
restaurantTables.forEach((table) => {
if (!bookedTablesInThisTimeRange.some(bookedTable => bookedTable.table_id === table._id.toString())) {
freeTables.push(table);
}
});
if (freeTables.length < 1) {
res.status(400).send(`Sorry!! All tables booked in this time range`);
}
const newBooking = new Booking({ ...req.body, table_id: freeTables[0].id});
let saveBooking = await newBooking.save();
res.status(200).json(saveBooking);
} else {
res.status(400).send(`Sorry!! No tables found for this restaurant. Try to reduce capacity according to tables`);
}
} else {
res.status(400).send(`Please book b/w timings ${restaurantOpen} to ${restaurantClose}`);
}
})
app.get('/user/restaurants/:userid', async (req, res) => {
const userId = req.params.userid;
let foundRestaurants = await Restaurant.find({ owner_id: userId});
if (foundRestaurants) {
res.status(200).json(foundRestaurants)
} else {
res.status(400).json({ message: "Not Found" })
}
})
app.get('/restaurant/:restaurantid', async (req, res) => {
const restaurantId = req.params.restaurantid;
let foundRestaurant = await Restaurant.findById(restaurantId);
if (foundRestaurant) {
res.status(200).json(foundRestaurant)
} else {
res.status(400).json({ message: "Not Found" })
}
})
app.post('/restaurant', async (req, res) => {
try {
const newRestaurant = new Restaurant(req.body);
let saveRestaurnt = await newRestaurant.save();
res.status(200).json(saveRestaurnt);
} catch (err) {
res.status(500).send(err.message);
}
})
app.post('/table', async (req, res) => {
//check if user is owner of restaurant mention in body
try {
const newTable = new Table(req.body);
let saveTable = await newTable.save();
res.status(200).json(saveTable);
} catch (err) {
res.status(500).send(err.message);
}
})
app.listen(3000, () => {
console.log('Hey');
})