Skip to content

Commit

Permalink
Update POST /search route
Browse files Browse the repository at this point in the history
Route can filter search results by business name. Issues #2 and #8
  • Loading branch information
CodeWritingCow committed Dec 21, 2017
1 parent 239bf52 commit 3ea1ca2
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,16 @@ app.post('/search', (req, res) => {
var borough = req.body.borough;

var data = req.body;
var businessName = data.dba.toLowerCase();

// Remove business name query
delete data.dba;

// Remove zipcode query string if it's empty
if (data.zipcode.length === 0) {
delete data.zipcode;
}

// Remove business name query string if it's empty
if (data.dba.length === 0) {
delete data.dba;
}

// Merge query strings. Exclude undefined query strings.
var urlQuery = querystring.stringify(_.merge(data));

Expand All @@ -123,14 +122,32 @@ app.post('/search', (req, res) => {

// if zipcode contains letters, return errorMessage
// ADD CODE HERE

request(`${url}?${urlQuery}`, (error, response, body) => {
if (JSON.parse(body).length === 0) {
return res.render("search.hbs", {errorMessage: 'Your search turned up no records.'});
return res.render("search.hbs", {errorMessage: 'No results found.'});
}

if (!error && response.statusCode === 200) {
console.log(`${url}?${urlQuery}`);
res.render("search.hbs", {body: JSON.parse(body)});

// If user search includes restaurant name
if (businessName.length > 0) {
var searchResults = JSON.parse(body).filter((business) => {
if (business.dba) {
return business.dba.toLowerCase().includes(businessName);
}
});
console.log(`${searchResults.length} search results`);

if (searchResults.length === 0) {
return res.render("search.hbs", {errorMessage: 'No results found.'});
} else {
res.render("search.hbs", {body: searchResults});
}
} else {
console.log(`${url}?${urlQuery}`);
res.render("search.hbs", {body: JSON.parse(body)});
}
} else {
return error;
}
Expand Down

0 comments on commit 3ea1ca2

Please sign in to comment.