-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
86 lines (74 loc) · 2.46 KB
/
routes.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
const express = require("express");
const router = express.Router();
const model = require("./data-model");
const MessagingResponse = require("twilio").twiml.MessagingResponse;
const bodyParser = require("body-parser");
router.use(bodyParser.urlencoded({ extended: false }));
// Helper function: wraps another function in try/catch and passes errors to middleware
function asyncHandler(cb) {
return async (req, res, next) => {
try {
await cb(req, res, next);
} catch (err) {
next(err);
}
};
}
// Greeting
router.get(
"/",
asyncHandler(async (req, res) => {
const restaurants = await model.getRestaurants();
res.json({message: "Welcome! Add /restaurants to the end of this url to see Bay Area restaurants. A zip after that will show you some by zip."});
})
);
// Get all restaurant names
router.get(
"/restaurants",
asyncHandler(async (req, res) => {
const restaurants = await model.getRestaurants();
res.json(restaurants);
})
);
// Get all restaurants in a zip code
router.get(
"/restaurants/:zip",
asyncHandler(async (req, res) => {
const restaurants_in_zip = await model.getZipRestaurants(req.params.zip);
if (restaurants_in_zip) {
res.json(restaurants_in_zip);
} else {
res.status(404).json({
message: "No restaurants found in your zip code! Please enter another.",
});
}
})
);
// Receive a zip code, and return a list of restaurants
router.post(
"/sms",
asyncHandler(async (req, res) => {
const twiml = new MessagingResponse();
// Store the body of the user's text as a variable
const zip = req.body.Body;
// Load zip codes
const validZips = await model.getZips();
// If the zip is not in our valid zip codes list, return an error
if (!validZips.includes(zip)) {
twiml.message(`Hmmm, I'm not finding any restaurants open in ${req.body.Body}`);
twiml.message(`Could you please try another five-digit Bay Area zip code?`);
}
// But if it is, return the list of restaurants
else {
const restaurants_in_zip = await model.getZipRestaurants(zip);
twiml.message(
`Thanks for eating local❣️ Here are the restaurants open in ${req.body.Body}:`
);
// Formatting: remove the commas in the returned array with an empty space
twiml.message(restaurants_in_zip.join("").toString());
}
res.writeHead(200, { "Content-Type": "text/xml" });
res.end(twiml.toString());
})
);
module.exports = router;