Skip to content

Commit

Permalink
added edit profile
Browse files Browse the repository at this point in the history
  • Loading branch information
marksupalla committed Aug 30, 2014
1 parent 4022adf commit 1ef3605
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 9 deletions.
9 changes: 9 additions & 0 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ exports.profile = function(req, res){
res.render('users/profile', {items:items});
});
};
exports.edit = function(req, res){
res.render('users/edit');
};

exports.update = function(req, res){
res.locals.user.save(req.body, function(){
console.log(req.body);
res.redirect('/profile');
});
};
16 changes: 14 additions & 2 deletions app/models/user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var bcrypt = require('bcrypt'),
Mongo = require('mongodb');
Mongo = require('mongodb'),
_ = require('underscore-contrib');

function User(){
}
Expand All @@ -12,7 +13,9 @@ Object.defineProperty(User, 'collection', {

User.findById = function(id, cb){
var _id = Mongo.ObjectID(id);
User.collection.findOne({_id:_id}, cb);
User.collection.findOne({_id:_id}, function(err, obj){
cb(err, _.merge(User.prototype, obj));
});
};

User.register = function(o, cb){
Expand All @@ -32,5 +35,14 @@ User.authenticate = function(o, cb){
});
};

User.prototype.save = function(o, cb){
var properties = Object.keys(o),
self = this;
properties.forEach(function(property){
self[property] = o[property];
});
User.collection.save(self, cb);
};

module.exports = User;

2 changes: 2 additions & 0 deletions app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = function(app, express){

app.use(security.bounce);
app.delete('/logout', users.logout);
app.get('/profile/edit', users.edit);
app.put('/profile', users.update);
app.get('/profile', users.profile);
app.post('/items', items.create);

Expand Down
30 changes: 30 additions & 0 deletions app/views/users/edit.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
extends ../shared/template
block content
h2 Edit Profile
.row
.col-xs-6
form(role='form', method='post', action='/profile')
input(type='hidden', name='_method', value='put')
.form-group
label(for='name') Name
input.form-control#name(type='text', name='name', value=user.name, autofocus=true)
.form-group
label(for='email') Email
input.form-control#email(type='email', name='email', value=user.email)
.form-group
label(for='phone') Phone
input.form-control#phone(type='tel', name='phone', value=user.phone)
.form-group
label(for='photo') Photo
input.form-control#photo(type='text', name='photo', value=user.photo)
.form-group
label(for='bio') Bio
input.form-control#bio(type='text', name='bio', value=user.bio)
.form-group
label(for='favorite') Favorite Wine
input.form-control#favorite(type='text', name='favorite Wine', value=user.favorite)

button.btn.btn-primary(type='submit') Update Profile
.col-xs-6

block scripts
4 changes: 2 additions & 2 deletions app/views/users/profile.jade
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
extends ../shared/template
block content
h2 Profile
h5: a(href='/profile/edit') Update Profile
.panel.panel-default
.panel-body
.row
.col-xs-6
h3= user.name
.col-xs-6
h5: a(href='/profile/edit') Update Profile
.row
.col-xs-4
table.table.table-bordered
tbody
each prop in Object.keys(user)
unless prop === '_id' || prop === 'password' || prop === 'photo' || prop === 'isPublic' || prop === 'tagline' || prop === 'name'
unless prop === 'save' || prop === '_id' || prop === 'password' || prop === 'photo' || prop === 'isPublic' || prop === 'tagline' || prop === 'name'
tr
td= prop.charAt(0).toUpperCase() + prop.substring(1)
td= user[prop]
Expand Down
24 changes: 19 additions & 5 deletions test/acceptance/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,33 @@ describe('users', function(){
});
});

describe('post /items', function(){
it('should redirect to the profile page', function(done){
describe('get /profile/edit', function(){
it('should show the update/edit profile page', function(done){
request(app)
.post('/items')
.get('/profile/edit')
.set('cookie', cookie)
.end(function(err, res){
expect(res.status).to.equal(200);
expect(res.text).to.include('name');
expect(res.text).to.include('email');
done();
});
});
});

describe('put /profile', function(){
it('should show updated profile page', function(done){
request(app)
.put('/profile')
.set('cookie', cookie)
.send('name=Red+Wine&photo=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fc%2Fcc%2FFrench_beaujolais_red_wine_bottle.jpg&tags=red%2C+wine&location=Nashville%2C+TN%2C+USA&description=Red+Wine')
.end(function(err, res){
expect(res.status).to.equal(302);
expect(res.headers.location).to.equal('/profile');
done();
});
});
});

});



16 changes: 16 additions & 0 deletions test/unit/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,21 @@ describe('User', function(){
expect(u).to.be.instanceof(User);
});
});

describe('#save', function(){
it('should save a user to the database', function(done){
console.log('*****Made it to line 34');
var mark = {name:'Mark', email:'mark@gmail.com', photo:'http://mark.img', phone:'615-555-5555', password:'5555'};
User.findById('000000000000000000000001', function(err, user){
console.log(user);
user.save(mark, function(){
expect(user.name).to.equal('Mark');
done();
});
});
});
});

});


0 comments on commit 1ef3605

Please sign in to comment.