Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 06 #9

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Dependency directory
node_modules
node_modules

.env
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: npm start
11 changes: 7 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
require('./app_api/models/db');

var routes = require('./routes/index');
var users = require('./routes/users');
var routes = require('./app_server/routes/index');
var routesApi = require('./app_api/routes/index');
// var users = require('./app_server/routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('views', path.join(__dirname, 'app_server', 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
Expand All @@ -23,7 +25,8 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.use('/api', routesApi);
// app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
205 changes: 205 additions & 0 deletions app_api/controllers/locations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
var mongoose = require('mongoose');
var Loc = mongoose.model('Location');

var sendJSONresponse = function(res, status, content) {
res.status(status);
res.json(content);
};

var theEarth = (function() {
var earthRadius = 6371; // km, miles is 3959

var getDistanceFromRads = function(rads) {
return parseFloat(rads * earthRadius);
};

var getRadsFromDistance = function(distance) {
return parseFloat(distance / earthRadius);
};

return {
getDistanceFromRads: getDistanceFromRads,
getRadsFromDistance: getRadsFromDistance
};
})();

/* GET list of locations */
module.exports.locationsListByDistance = function(req, res) {
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
var maxDistance = parseFloat(req.query.maxDistance);
var point = {
type: "Point",
coordinates: [lng, lat]
};
var geoOptions = {
spherical: true,
maxDistance: theEarth.getRadsFromDistance(maxDistance),
num: 10
};
if (!lng || !lat || !maxDistance) {
console.log('locationsListByDistance missing params');
sendJSONresponse(res, 404, {
"message": "lng, lat and maxDistance query parameters are all required"
});
return;
}
Loc.geoNear(point, geoOptions, function(err, results, stats) {
var locations;
console.log('Geo Results', results);
console.log('Geo stats', stats);
if (err) {
console.log('geoNear error:', err);
sendJSONresponse(res, 404, err);
} else {
locations = buildLocationList(req, res, results, stats);
sendJSONresponse(res, 200, locations);
}
});
};

var buildLocationList = function(req, res, results, stats) {
var locations = [];
results.forEach(function(doc) {
locations.push({
distance: theEarth.getDistanceFromRads(doc.dis),
name: doc.obj.name,
address: doc.obj.address,
rating: doc.obj.rating,
facilities: doc.obj.facilities,
_id: doc.obj._id
});
});
return locations;
};

/* GET a location by the id */
module.exports.locationsReadOne = function(req, res) {
console.log('Finding location details', req.params);
if (req.params && req.params.locationid) {
Loc
.findById(req.params.locationid)
.exec(function(err, location) {
if (!location) {
sendJSONresponse(res, 404, {
"message": "locationid not found"
});
return;
} else if (err) {
console.log(err);
sendJSONresponse(res, 404, err);
return;
}
console.log(location);
sendJSONresponse(res, 200, location);
});
} else {
console.log('No locationid specified');
sendJSONresponse(res, 404, {
"message": "No locationid in request"
});
}
};

/* POST a new location */
/* /api/locations */
module.exports.locationsCreate = function(req, res) {
console.log(req.body);
Loc.create({
name: req.body.name,
address: req.body.address,
facilities: req.body.facilities.split(","),
coords: [parseFloat(req.body.lng), parseFloat(req.body.lat)],
openingTimes: [{
days: req.body.days1,
opening: req.body.opening1,
closing: req.body.closing1,
closed: req.body.closed1,
}, {
days: req.body.days2,
opening: req.body.opening2,
closing: req.body.closing2,
closed: req.body.closed2,
}]
}, function(err, location) {
if (err) {
console.log(err);
sendJSONresponse(res, 400, err);
} else {
console.log(location);
sendJSONresponse(res, 201, location);
}
});
};

/* PUT /api/locations/:locationid */
module.exports.locationsUpdateOne = function(req, res) {
if (!req.params.locationid) {
sendJSONresponse(res, 404, {
"message": "Not found, locationid is required"
});
return;
}
Loc
.findById(req.params.locationid)
.select('-reviews -rating')
.exec(
function(err, location) {
if (!location) {
sendJSONresponse(res, 404, {
"message": "locationid not found"
});
return;
} else if (err) {
sendJSONresponse(res, 400, err);
return;
}
location.name = req.body.name;
location.address = req.body.address;
location.facilities = req.body.facilities.split(",");
location.coords = [parseFloat(req.body.lng), parseFloat(req.body.lat)];
location.openingTimes = [{
days: req.body.days1,
opening: req.body.opening1,
closing: req.body.closing1,
closed: req.body.closed1,
}, {
days: req.body.days2,
opening: req.body.opening2,
closing: req.body.closing2,
closed: req.body.closed2,
}];
location.save(function(err, location) {
if (err) {
sendJSONresponse(res, 404, err);
} else {
sendJSONresponse(res, 200, location);
}
});
}
);
};

/* DELETE /api/locations/:locationid */
module.exports.locationsDeleteOne = function(req, res) {
var locationid = req.params.locationid;
if (locationid) {
Loc
.findByIdAndRemove(locationid)
.exec(
function(err, location) {
if (err) {
console.log(err);
sendJSONresponse(res, 404, err);
return;
}
console.log("Location id " + locationid + " deleted");
sendJSONresponse(res, 204, null);
}
);
} else {
sendJSONresponse(res, 404, {
"message": "No locationid"
});
}
};
Loading