From cc78c105459d50eba3dc3bcb45d3d61f265bbc54 Mon Sep 17 00:00:00 2001 From: Marcial Date: Sat, 9 Sep 2017 00:06:54 -0400 Subject: [PATCH] Improvements --- .gitignore | 2 +- server/config/express.js | 6 ++++-- .../controllers/coordinates.server.controller.js | 15 ++++++++++++++- server/controllers/listings.server.controller.js | 13 +++++-------- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 65b0c42..bb41064 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ .DS_Store # configuration files -server/config/config.js +config.js # node node_modules \ No newline at end of file diff --git a/server/config/express.js b/server/config/express.js index 27145f0..10863a9 100644 --- a/server/config/express.js +++ b/server/config/express.js @@ -1,5 +1,4 @@ -var path = require('path'), - express = require('express'), +var express = require('express'), mongoose = require('mongoose'), morgan = require('morgan'), bodyParser = require('body-parser'), @@ -14,6 +13,9 @@ module.exports.init = function() { //initialize app var app = express(); + // Readable JSON output + app.set('json spaces', 4); + //enable request logging for development debugging app.use(morgan('dev')); diff --git a/server/controllers/coordinates.server.controller.js b/server/controllers/coordinates.server.controller.js index f180055..41323fb 100644 --- a/server/controllers/coordinates.server.controller.js +++ b/server/controllers/coordinates.server.controller.js @@ -16,7 +16,20 @@ module.exports = function(req, res, next) { } var data = JSON.parse(body); - req.results = data.results[0].geometry.location; + if (data.results.length > 0) { + var coordinates = data.results[0].geometry.location; + req.results = { latitude: coordinates.lat, longitude: coordinates.lng}; + } else if (data.status == 'REQUEST_DENIED') { + var debug_message = [ + "You need a Google Geocoding API key for this Assignment", + "Get a key at: https://developers.google.com/maps/documentation/geocoding/get-api-key", + "You'd also need a Google Cloud Platform project, with the 'Google Geocoding API' enabled" + ]; + console.log(debug_message.join("\n")); + } else { + req.results = undefined + console.log(data); + } next(); }); } else { diff --git a/server/controllers/listings.server.controller.js b/server/controllers/listings.server.controller.js index 7fc96ac..fc12586 100644 --- a/server/controllers/listings.server.controller.js +++ b/server/controllers/listings.server.controller.js @@ -19,11 +19,8 @@ exports.create = function(req, res) { var listing = new Listing(req.body); /* save the coordinates (located in req.results if there is an address property) */ - if(req.results) { - listing.coordinates = { - latitude: req.results.lat, - longitude: req.results.lng - }; + if (req.results) { + listing.coordinates = req.results; } /* Then save the listing */ @@ -47,16 +44,16 @@ exports.read = function(req, res) { exports.update = function(req, res) { var listing = req.listing; - /* Replace the article's properties with the new properties found in req.body */ + /* Replace the listing's properties with the new properties found in req.body */ /* save the coordinates (located in req.results if there is an address property) */ - /* Save the article */ + /* Save the listing */ }; /* Delete a listing */ exports.delete = function(req, res) { var listing = req.listing; - /* Remove the article */ + /* Remove the listing */ }; /* Retreive all the directory listings, sorted alphabetically by listing code */