-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (89 loc) · 2.96 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
require('dotenv').config();
const yelp = require('yelp-fusion');
const express = require('express');
const app = express();
const apiKey = process.env.apiKey;
const client = yelp.client(apiKey);
const PORT = process.env.PORT || 3001;
//mimicing in-memory db;
let user = {};
//list all registered routes
const listRoutes = (req, res) => {
let routes = [];
app._router.stack.forEach(function (middleware) {
if(middleware.route) {
routes.push(Object.keys(middleware.route.methods) + " -> " + middleware.route.path);
}
});
res.send(JSON.stringify(routes, null, 4));
}
//resuable search function
const searchLunch = (searchParams) => {
var res = client.search(searchParams).then(response => {
return(response.jsonBody.businesses);
}).catch(e => {
return {status: 400, Message: JSON.parse(e.response.body)}
})
return res;
}
//Lunch finder
const findLunch = async (req, res) => {
const results = await searchLunch(req.query);
res.send(results)
}
//Business reviews
const getReviews = (req, res) => {
client.reviews(req.params.id).then(response => {
res.send(response.jsonBody.reviews);
}).catch(e => {
res.send({status: 400, Message: JSON.parse(e.response.body)})
});
}
//full details for the business
const fullDetails = (req, res) => {
client.business(req.params.id).then(response => {
res.send(response.jsonBody);
}).catch(e => {
res.send({status: 400, Message: JSON.parse(e.response.body)})
});
}
// Keeping user history in-memory in user variable
const ateAt = (req, res) => {
if (req.query.userName === undefined)
res.send({status:400, Message: 'Enter your UserName'})
else{
const userName = req.query.userName;
let business = req.query.business;
if (!(userName in user))
user[userName] = {};
user[userName][business] = true;
console.log(user);
res.send({status: 200, Message: {'history': user[userName]}})
}
}
// Tired of eating at same place? Exclude previous places where you ate
const tryNew = async (req, res) => {
const places = await searchLunch(req.query);
if (req.query.userName === undefined)
res.send({status:400, Message: 'Enter your UserName'})
else if(user[req.query.userName] === undefined){
res.send({status:400, Message: 'No history found. Please add a place to your history at /ateat/:id'})
}
else{
const userName = req.query.userName;
let newPlaces = [];
await places.map(place => {
(place.id in user[userName]) ? null : newPlaces.push(place)
})
res.send(newPlaces);
}
}
//routes
app.get('/', listRoutes);
app.get('/lunch', findLunch);
app.get('/reviews/:id', getReviews);
app.get('/details/:id', fullDetails);
app.get('/ateat', ateAt);
app.get('/trynew', tryNew);
//listener
app.listen(PORT, () => console.log(`listening on port ${PORT}`))