Skip to content

Commit

Permalink
added .findById
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 23, 2014
1 parent b9f8638 commit 720b805
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/controllers/stops.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ exports.create = function(req, res){
};

exports.show = function(req, res){
Stop.findById(req.params.stopId, function(err, s){
res.render('stops/show', {stop:s});
});
};

exports.addEvent = function(req, res){
Expand Down
44 changes: 43 additions & 1 deletion app/models/stop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';

var Mongo = require('mongodb'),
async = require('async');
async = require('async'),
_ = require('lodash'),
fs = require('fs'),
path = require('path');

function Stop(o){
this.loc = {name:o.name, lat:parseFloat(o.lat), lng:parseFloat(o.lng)};
Expand All @@ -22,6 +25,14 @@ Stop.find = function(id, cb){
Stop.collection.find({tripId:id}).toArray(cb);
};

Stop.findById = function(id, cb){
id = Mongo.ObjectID(id);
Stop.collection.findOne({_id:id}, function(err, o){
var s = _.create(Stop.prototype, o);
cb(err, s);
});
};

Stop.create = function(obj, cb){
var stops = Object.keys(obj).map(function(k){
return obj[k];
Expand All @@ -36,6 +47,37 @@ Stop.create = function(obj, cb){
});
};

Stop.prototype.moveFiles = function(files){
var baseDir = __dirname + '/../static',
relDir = '/img/' + this._id,
absDir = baseDir + relDir,
exists = fs.existsSync(absDir);

if(!exists){
fs.mkdirSync(absDir);
}

var photos = files.photos.map(function(photo, index){
if(!photo.size){return;}

var ext = path.extname(photo.path),
name = index + ext,
absPath = absDir + '/' + name,
relPath = relDir + '/' + name;

fs.renameSync(photo.path, absPath);
return relPath;
});

photos = _.compact(photos);
var self = this;
if(photos.length){
photos.forEach(function(photo){
self.photos.push(photo);
});
}
};

Stop.prototype.save = function(cb){
Stop.collection.save(this, cb);
};
Expand Down
6 changes: 6 additions & 0 deletions app/views/stops/show.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extends ../shared/template
block content

block scripts
script(src='/js/user/stops-show.js')

0 comments on commit 720b805

Please sign in to comment.