diff --git a/app/controllers/stops.js b/app/controllers/stops.js index 501ff4a..a9dc677 100644 --- a/app/controllers/stops.js +++ b/app/controllers/stops.js @@ -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){ diff --git a/app/models/stop.js b/app/models/stop.js index 1cf6bfa..a0210b1 100644 --- a/app/models/stop.js +++ b/app/models/stop.js @@ -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)}; @@ -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]; @@ -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); }; diff --git a/app/views/stops/show.jade b/app/views/stops/show.jade index e69de29..c367382 100644 --- a/app/views/stops/show.jade +++ b/app/views/stops/show.jade @@ -0,0 +1,6 @@ +extends ../shared/template +block content + +block scripts + script(src='/js/user/stops-show.js') +