diff --git a/.gitignore b/.gitignore
index e8e6ce6..468d7fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
# Dependency directory
-node_modules
\ No newline at end of file
+node_modules
+
+.env
\ No newline at end of file
diff --git a/app_api/controllers/locations.js b/app_api/controllers/locations.js
index 80e9066..b2c9ebd 100644
--- a/app_api/controllers/locations.js
+++ b/app_api/controllers/locations.js
@@ -37,7 +37,7 @@ module.exports.locationsListByDistance = function(req, res) {
maxDistance: theEarth.getRadsFromDistance(maxDistance),
num: 10
};
- if (!lng || !lat || !maxDistance) {
+ if ((!lng && lng!==0) || (!lat && lat!==0) || ! maxDistance) {
console.log('locationsListByDistance missing params');
sendJSONresponse(res, 404, {
"message": "lng, lat and maxDistance query parameters are all required"
diff --git a/app_api/models/locations.js b/app_api/models/locations.js
index 9338db2..cfe270e 100644
--- a/app_api/models/locations.js
+++ b/app_api/models/locations.js
@@ -1,14 +1,14 @@
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
- author: String,
+ author: {type: String, required: true},
rating: {
type: Number,
required: true,
min: 0,
max: 5
},
- reviewText: String,
+ reviewText: {type: String, required: true},
createdOn: {
type: Date,
"default": Date.now
diff --git a/app_server/controllers/locations.js b/app_server/controllers/locations.js
index db8d394..1eafd87 100644
--- a/app_server/controllers/locations.js
+++ b/app_server/controllers/locations.js
@@ -1,92 +1,190 @@
+var request = require('request');
+var apiOptions = {
+ server : "http://localhost:3000"
+};
+if (process.env.NODE_ENV === 'production') {
+ apiOptions.server = "https://getting-mean-loc8r.herokuapp.com";
+}
+
+var _isNumeric = function (n) {
+ return !isNaN(parseFloat(n)) && isFinite(n);
+};
+
+var _formatDistance = function (distance) {
+ var numDistance, unit;
+ if (distance && _isNumeric(distance)) {
+ if (distance > 1) {
+ numDistance = parseFloat(distance).toFixed(1);
+ unit = 'km';
+ } else {
+ numDistance = parseInt(distance * 1000,10);
+ unit = 'm';
+ }
+ return numDistance + unit;
+ } else {
+ return "?";
+ }
+};
+
+var _showError = function (req, res, status) {
+ var title, content;
+ if (status === 404) {
+ title = "404, page not found";
+ content = "Oh dear. Looks like we can't find this page. Sorry.";
+ } else if (status === 500) {
+ title = "500, internal server error";
+ content = "How embarrassing. There's a problem with our server.";
+ } else {
+ title = status + ", something's gone wrong";
+ content = "Something, somewhere, has gone just a little bit wrong.";
+ }
+ res.status(status);
+ res.render('generic-text', {
+ title : title,
+ content : content
+ });
+};
+
+var renderHomepage = function(req, res, responseBody){
+ var message;
+ if (!(responseBody instanceof Array)) {
+ message = "API lookup error";
+ responseBody = [];
+ } else {
+ if (!responseBody.length) {
+ message = "No places found nearby";
+ }
+ }
+ res.render('locations-list', {
+ title: 'Loc8r - find a place to work with wifi',
+ pageHeader: {
+ title: 'Loc8r',
+ strapline: 'Find places to work with wifi near you!'
+ },
+ sidebar: "Looking for wifi and a seat? Loc8r helps you find places to work when out and about. Perhaps with coffee, cake or a pint? Let Loc8r help you find the place you're looking for.",
+ locations: responseBody,
+ message: message
+ });
+};
+
/* GET 'home' page */
-module.exports.homelist = function(req, res) {
- res.render('locations-list', {
- title: 'Loc8r - find a place to work with wifi',
- pageHeader: {
- title: 'Loc8r',
- strapline: 'Find places to work with wifi near you!'
- },
- sidebar: "Looking for wifi and a seat? Loc8r helps you find places to work when out and about. Perhaps with coffee, cake or a pint? Let Loc8r help you find the place you're looking for.",
- locations: [{
- name: 'Starcups',
- address: '125 High Street, Reading, RG6 1PS',
- rating: 3,
- facilities: ['Hot drinks', 'Food', 'Premium wifi'],
- distance: '100m'
- }, {
- name: 'Cafe Hero',
- address: '125 High Street, Reading, RG6 1PS',
- rating: 4,
- facilities: ['Hot drinks', 'Food', 'Premium wifi'],
- distance: '200m'
- }, {
- name: 'Burger Queen',
- address: '125 High Street, Reading, RG6 1PS',
- rating: 2,
- facilities: ['Food', 'Premium wifi'],
- distance: '250m'
- }]
- });
+module.exports.homelist = function(req, res){
+ var requestOptions, path;
+ path = '/api/locations';
+ requestOptions = {
+ url : apiOptions.server + path,
+ method : "GET",
+ json : {},
+ qs : {
+ lng : -0.7992599,
+ lat : 51.378091,
+ maxDistance : 20
+ }
+ };
+ request(
+ requestOptions,
+ function(err, response, body) {
+ var i, data;
+ data = body;
+ if (response.statusCode === 200 && data.length) {
+ for (i=0; i