Skip to content

Commit

Permalink
added #addPhoto
Browse files Browse the repository at this point in the history
  • Loading branch information
abarnhard committed Aug 19, 2014
1 parent 99baad8 commit 0683d1a
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 4 deletions.
8 changes: 8 additions & 0 deletions app/controllers/vacations.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ exports.show = function(req, res){
res.render('vacations/show', {vacation:v, moment:moment});
});
};

exports.addPhoto = function(req, res){
Vacation.findById(req.params.id, function(err, v){
v.addPhoto(req.body.url, function(){
res.redirect('/vacations/' + req.params.id);
});
});
};
25 changes: 23 additions & 2 deletions app/models/vacation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var Mongo = require('mongodb');
var Mongo = require('mongodb'),
_ = require('lodash'),
cp = require('child_process');

function Vacation(o){
this.name = o.name.trim();
Expand Down Expand Up @@ -28,7 +30,26 @@ Vacation.save = function(o, cb){

Vacation.findById = function(id, cb){
id = Mongo.ObjectID(id);
Vacation.collection.findOne({_id:id}, cb);
Vacation.collection.findOne({_id:id}, function(err, v){
v = _.create(Vacation.prototype, v);
cb(err, v);
});
};

Vacation.prototype.addPhoto = function(url, cb){
var exts = url.split('.'),
ext = exts[exts.length - 1],
dir = this._id.toString(),
file = this.photos.length + '.' + ext,
self = this;

// console.log(url, dir, file);
cp.execFile(__dirname + '/../scripts/download.sh', [url, file, dir], {cwd:__dirname + '/../scripts'}, function(err, stdout, stderr){
// console.log(err, stdout, stderr);
var photo = '/img/' + dir + '/' + file;
self.photos.push(photo);
Vacation.collection.save(self, cb);
});
};

module.exports = Vacation;
Expand Down
1 change: 1 addition & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function(app, express){
app.post('/vacations', vacations.create);
app.get('/vacations', vacations.index);
app.get('/vacations/:id', vacations.show);
app.post('/vacations/:id/photos/new', vacations.addPhoto);

console.log('Routes Loaded');
};
Expand Down
2 changes: 1 addition & 1 deletion app/static/css/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions app/static/css/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ html, body {
width: 60%;
margin: 0 auto;
}

.photo {
display: inline-block;
width: 200px;
height: 200px;
background-size: cover;
margin: 5px;
}
Binary file added app/static/img/000000000000000000000001/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000001/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/img/000000000000000000000001/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion app/views/vacations/show.jade
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ block content
button.btn.btn-default(type='submit') Add Image
.row
.col-xs-12
h3 Photos Go Here
each photo in vacation.photos
.photo(style='background-image:url(#{photo});')

block scripts
script(src='/js/user/vacation-show.js')
Expand Down

0 comments on commit 0683d1a

Please sign in to comment.