-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBD-2.5-Assignment3.js
292 lines (268 loc) · 5.69 KB
/
BD-2.5-Assignment3.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
const express = require("express");
const cors = require("cors");
const app = express();
app.use(cors());
let hotels = [
{
id: 1,
name: "Romantic Getaway",
category: "Resort",
rating: 2.2,
reviews: 4572,
amenity: "Spa",
price: 10464,
country: "South Africa",
},
{
id: 2,
name: "Wellness Retreat",
category: "Family",
rating: 2.8,
reviews: 2114,
amenity: "Pool",
price: 13243,
country: "Australia",
},
{
id: 3,
name: "Romantic Getaway",
category: "Luxury",
rating: 3.1,
reviews: 4359,
amenity: "Restaurant",
price: 3299,
country: "Germany",
},
{
id: 4,
name: "Luxury Suites",
category: "Family",
rating: 4.9,
reviews: 3651,
amenity: "Bar",
price: 16359,
country: "United Kingdom",
},
{
id: 5,
name: "Luxury Suites",
category: "Budget",
rating: 4.6,
reviews: 688,
amenity: "Gym",
price: 15570,
country: "France",
},
{
id: 6,
name: "Cultural Heritage Hotel",
category: "Boutique",
rating: 2.0,
reviews: 219,
amenity: "Pet Friendly",
price: 2321,
country: "USA",
},
{
id: 7,
name: "Business Hotel",
category: "Mid-Range",
rating: 3.7,
reviews: 1040,
amenity: "Free WiFi",
price: 4523,
country: "India",
},
{
id: 8,
name: "Historic Plaza Hotel",
category: "Mid-Range",
rating: 3.5,
reviews: 300,
amenity: "Parking",
price: 8543,
country: "Australia",
},
{
id: 9,
name: "Adventure Resort",
category: "Boutique",
rating: 4.2,
reviews: 1222,
amenity: "Gym",
price: 11894,
country: "South Africa",
},
{
id: 10,
name: "Mountain Retreat",
category: "Resort",
rating: 4.8,
reviews: 4015,
amenity: "Spa",
price: 17560,
country: "India",
},
{
id: 11,
name: "Eco Friendly Lodge",
category: "Family",
rating: 2.4,
reviews: 528,
amenity: "Restaurant",
price: 3124,
country: "Germany",
},
{
id: 12,
name: "Urban Boutique Hotel",
category: "Mid-Range",
rating: 3.9,
reviews: 1401,
amenity: "Free WiFi",
price: 9245,
country: "France",
},
{
id: 13,
name: "Beachfront Hotel",
category: "Luxury",
rating: 4.5,
reviews: 489,
amenity: "Pool",
price: 14567,
country: "USA",
},
{
id: 14,
name: "Ocean View Resort",
category: "Budget",
rating: 3.3,
reviews: 783,
amenity: "Spa",
price: 7432,
country: "United Kingdom",
},
{
id: 15,
name: "City Central Hotel",
category: "Boutique",
rating: 4.1,
reviews: 2133,
amenity: "Bar",
price: 9823,
country: "Australia",
},
{
id: 16,
name: "Casino Resort",
category: "Luxury",
rating: 4.9,
reviews: 5000,
amenity: "Bar",
price: 18900,
country: "South Africa",
},
{
id: 17,
name: "Golf Resort",
category: "Mid-Range",
rating: 4.7,
reviews: 789,
amenity: "Gym",
price: 16340,
country: "France",
},
{
id: 18,
name: "Family Fun Hotel",
category: "Family",
rating: 3.2,
reviews: 1322,
amenity: "Pool",
price: 7500,
country: "Germany",
},
{
id: 19,
name: "Spa and Relaxation Hotel",
category: "Luxury",
rating: 4.4,
reviews: 2314,
amenity: "Spa",
price: 14900,
country: "United Kingdom",
},
{
id: 20,
name: "Country House Hotel",
category: "Budget",
rating: 3.6,
reviews: 1876,
amenity: "Parking",
price: 6234,
country: "Australia",
},
];
app.get('/hotels', (req, res) => {
res.json({ hotels: hotels });
});
app.get('/hotels/sort/pricing', (req, res) => {
const pricing = req.query.pricing;
let sortedHotels = hotels.sort((a, b) => {
if (pricing === 'low-to-high') {
return a.price - b.price;
} else if (pricing === 'high-to-low') {
return b.price - a.price;
}
});
res.json({ hotels: sortedHotels });
});
app.get('/hotels/sort/rating', (req, res) => {
const rating = req.query.rating;
let sortedHotels = hotels.sort((a, b) => {
if (rating === 'low-to-high') {
return a.rating - b.rating;
} else if (rating === 'high-to-low') {
return b.rating - a.rating;
}
});
res.json({ hotels: sortedHotels });
});
app.get('/hotels/sort/reviews', (req, res) => {
const reviews = req.query.reviews;
let sortedHotels = hotels.sort((a, b) => {
if (reviews === 'least-to-most') {
return a.reviews - b.reviews;
} else if (reviews === 'most-to-least') {
return b.reviews - a.reviews;
}
});
res.json({ hotels: sortedHotels });
});
app.get('/hotels/filter/amenity', (req, res) => {
const amenity = req.query.amenity.toLowerCase();
let filteredHotels = hotels.filter((hotel) => hotel.amenity.toLowerCase() === amenity);
res.json({ hotels: filteredHotels });
});
app.get('/hotels/filter/country', (req, res) => {
const country = req.query.country.toLowerCase();
function filterByCountry(hotels, country) {
return hotels.filter(hotel => hotel.country.toLowerCase() === country);
}
let filteredHotels = filterByCountry(hotels, country);
res.json({ hotels: filteredHotels });
});
app.get('/hotels/filter/category', (req, res) => {
const category = req.query.category.toLowerCase();
function filterByCategory(hotels, category) {
return hotels.filter(hotel => hotel.category.toLowerCase() === category);
}
let filteredHotels = filterByCategory(hotels, category);
res.json({ hotels: filteredHotels });
});
app.get('/hotels', (req, res) => {
res.json({ hotels: hotels });
});
const PORT = 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));