Skip to content

Instantly share code, notes, and snippets.

@jeffdonthemic
Created July 4, 2014 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffdonthemic/3fc55e245fb61f44563e to your computer and use it in GitHub Desktop.
Save jeffdonthemic/3fc55e245fb61f44563e to your computer and use it in GitHub Desktop.
Mongo scripts used for Chowfinder app
if(process.env.VCAP_SERVICES){
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env['mongodb-1.8'][0]['credentials'];
}
else{
var mongo = {
"hostname":"localhost",
"port":27017,
"username":"",
"password":"",
"name":"",
"db":"db"
}
}
var generate_mongo_url = function(obj){
obj.hostname = (obj.hostname || 'localhost');
obj.port = (obj.port || 27017);
obj.db = (obj.db || 'test');
if(obj.username && obj.password){
return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
else{
return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
}
var mongourl = generate_mongo_url(mongo);
// creates a new facility for a location
app.post('/v.1/locations/:location_id/facilities', function(req, res){
// add the location id to the json
var facility = req.body;
facility['location'] = req.params.location_id;
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('facilities', function(err, coll){
coll.insert( facility, {safe:true}, function(err){
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(facility));
});
});
});
});
// creates a location in the 'locations' collection
app.post('/v.1/locations', function(req, res){
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('locations', function(err, coll){
coll.insert( req.body, {safe:true}, function(err){
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(req.body));
});
});
});
});
// returns a specific location by id
app.get('/v.1/locations/:location_id', function(req, res){
var ObjectID = require('mongodb').ObjectID;
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('locations', function(err, coll){
coll.findOne({'_id':new ObjectID(req.params.location_id)}, function(err, document) {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(document));
});
});
});
});
// returns a specific location by id
app.get('/v.1/locations/:location_id', function(req, res){
var ObjectID = require('mongodb').ObjectID;
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('locations', function(err, coll){
coll.findOne({'_id':new ObjectID(req.params.location_id)}, function(err, document) {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(document));
});
});
});
});
// creates a location in the 'locations' collection
app.post('/v.1/locations', function(req, res){
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('locations', function(err, coll){
coll.insert( req.body, {safe:true}, function(err){
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(req.body));
});
});
});
});
// returns list of locations
app.get('/v.1/locations', function(req, res){
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('locations', function(err, coll){
coll.find(function(err, cursor) {
cursor.toArray(function(err, items) {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(items));
});
});
});
});
});
// updates a facility
app.put('/v.1/locations/:location_id/facilities/:facility_id', function(req, res){
var ObjectID = require('mongodb').ObjectID;
require('mongodb').connect(mongourl, function(err, conn){
conn.collection('facilities', function(err, coll){
coll.findAndModify({'_id':new ObjectID(req.params.facility_id)}, [['name','asc']], { $set: req.body }, {}, function(err, document) {
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
res.end(JSON.stringify(document));
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment