forked from CodeWritingCow/NYC-restaurant-inspection-records
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search-controller.js
71 lines (60 loc) · 2.09 KB
/
search-controller.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
const path = require("path");
const _ = require("lodash");
const axios = require("axios");
const querystring = require("querystring");
const token = process.env.API_TOKEN || require("../../token.js");
// token acquired by OpenNYC Data API
const socrataUrl = "https://data.cityofnewyork.us/resource/9w7m-hzhe.json";
exports.get = (req, res) => {
res.render("search.hbs", {
pageTitle: "Advanced Search"
});
};
exports.post = (req, res) => {
var { zipcode } = req.body;
var { borough } = req.body;
var data = req.body;
var businessName = data.dba.toUpperCase();
// If businessName includes single quote mark, change it to double quote
// This prevents socrataQuery from throwing an error
if (_.includes(businessName, "'")) {
businessName = businessName.replace(/'/g, "''");
}
var socrataQuery = `$$app_token=${token}`;
// if data.dba contains a value, add socrataQuery to urlQuery
if (data.dba.length > 0) {
socrataQuery += `&$where=DBA%20like%20%27%25${businessName}%25%27`;
}
// Remove business name query
delete data.dba;
// Remove zipcode query string if it's empty
if (data.zipcode === undefined || data.zipcode.length === 0) {
delete data.zipcode;
}
// Merge query strings. Exclude undefined query strings.
var urlQuery = querystring.stringify(_.merge(data));
axios(`${socrataUrl}?${socrataQuery + "&" + urlQuery}`)
.then(response => {
let searchResults = response.data;
if (searchResults.length === 0) {
return res.render("results.hbs", {
pageTitle: "Search Results",
numberResults: "Your search returned no results."
});
} else {
res.render("results.hbs", {
pageTitle: "Search Results",
body: searchResults,
numberResults: `Your search returned ${searchResults.length} results.`
});
}
})
.catch(err => {
// res.status(err.response.status);
res.render("error.hbs", {
pageTitle: "Something went wrong!",
errorMessage:
"There seems to be an error. Let's go home and try something else."
});
});
};