Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Cleaning Up
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Aug 16, 2013
1 parent e34d2d8 commit 6c3564f
Show file tree
Hide file tree
Showing 20 changed files with 777 additions and 694 deletions.
80 changes: 42 additions & 38 deletions app/controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,72 +10,76 @@ var mongoose = require('mongoose'),
/**
* Find article by id
*/
exports.article = function(req, res, next, id){
var User = mongoose.model('User');
exports.article = function(req, res, next, id) {
var User = mongoose.model('User');

Article.load(id, function (err, article) {
if (err) return next(err);
if (!article) return next(new Error('Failed to load article ' + id));
req.article = article;
next();
});
Article.load(id, function(err, article) {
if (err) return next(err);
if (!article) return next(new Error('Failed to load article ' + id));
req.article = article;
next();
});
};

/**
* Create a article
*/
exports.create = function (req, res) {
var article = new Article(req.body);
exports.create = function(req, res) {
var article = new Article(req.body);

article.user = req.user;
article.save();
res.jsonp(article);
article.user = req.user;
article.save();
res.jsonp(article);
};

/**
* Update a article
*/
exports.update = function(req, res){
var article = req.article;
exports.update = function(req, res) {
var article = req.article;

article = _.extend(article, req.body);
article = _.extend(article, req.body);

article.save(function(err) {
res.jsonp(article);
});
article.save(function(err) {
res.jsonp(article);
});
};

/**
* Delete an article
*/
exports.destroy = function(req, res){
var article = req.article;
exports.destroy = function(req, res) {
var article = req.article;

article.remove(function(err){
if (err) {
res.render('error', {status: 500});
} else {
res.jsonp(article);
}
});
article.remove(function(err) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(article);
}
});
};

/**
* Show an article
*/
exports.show = function(req, res){
res.jsonp(req.article);
exports.show = function(req, res) {
res.jsonp(req.article);
};

/**
* List of Articles
*/
exports.all = function(req, res){
Article.find().sort('-created').populate('user').exec(function(err, articles) {
if (err) {
res.render('error', {status: 500});
} else {
res.jsonp(articles);
}
});
exports.all = function(req, res) {
Article.find().sort('-created').populate('user').exec(function(err, articles) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(articles);
}
});
};
14 changes: 7 additions & 7 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Module dependencies.
*/
var mongoose = require('mongoose'),
async = require('async'),
_ = require('underscore');
async = require('async'),
_ = require('underscore');


exports.render = function(req, res){
res.render('index', {
user: req.user ? JSON.stringify(req.user) : "null"
});
};
exports.render = function(req, res) {
res.render('index', {
user: req.user ? JSON.stringify(req.user) : "null"
});
};
100 changes: 51 additions & 49 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,103 @@

/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
User = mongoose.model('User');

//exports.signin = function (req, res) {}

/**
* Auth callback
*/
exports.authCallback = function (req, res, next) {
res.redirect('/');
exports.authCallback = function(req, res, next) {
res.redirect('/');
};

/**
* Show login form
*/
exports.signin = function (req, res) {
res.render('users/signin', {
title: 'Signin',
message: req.flash('error')
});
exports.signin = function(req, res) {
res.render('users/signin', {
title: 'Signin',
message: req.flash('error')
});
};

/**
* Show sign up form
*/
exports.signup = function (req, res) {
res.render('users/signup', {
title: 'Sign up',
user: new User()
});
exports.signup = function(req, res) {
res.render('users/signup', {
title: 'Sign up',
user: new User()
});
};

/**
* Logout
*/
exports.signout = function (req, res) {
req.logout();
res.redirect('/');
exports.signout = function(req, res) {
req.logout();
res.redirect('/');
};

/**
* Session
*/
exports.session = function (req, res) {
res.redirect('/');
exports.session = function(req, res) {
res.redirect('/');
};

/**
* Create user
*/
exports.create = function (req, res) {
var user = new User(req.body);
exports.create = function(req, res) {
var user = new User(req.body);

user.provider = 'local';
user.save(function (err) {
if (err) {
return res.render('users/signup', { errors: err.errors, user: user });
}
req.logIn(user, function(err) {
if (err) return next(err);
return res.redirect('/');
user.provider = 'local';
user.save(function(err) {
if (err) {
return res.render('users/signup', {
errors: err.errors,
user: user
});
}
req.logIn(user, function(err) {
if (err) return next(err);
return res.redirect('/');
});
});
});
};

/**
* Show profile
*/
exports.show = function (req, res) {
var user = req.profile;
exports.show = function(req, res) {
var user = req.profile;

res.render('users/show', {
title: user.name,
user: user
});
res.render('users/show', {
title: user.name,
user: user
});
};

/**
* Send User
*/
exports.me = function (req, res) {
res.jsonp(req.user || null);
exports.me = function(req, res) {
res.jsonp(req.user || null);
};

/**
* Find user by id
*/
exports.user = function (req, res, next, id) {
User
.findOne({ _id : id })
.exec(function (err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + id));
req.profile = user;
next();
});
};
exports.user = function(req, res, next, id) {
User
.findOne({
_id: id
})
.exec(function(err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + id));
req.profile = user;
next();
});
};
30 changes: 23 additions & 7 deletions app/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,35 @@ var mongoose = require('mongoose'),
* Article Schema
*/
var ArticleSchema = new Schema({
created: {type : Date, default : Date.now},
title: {type: String, default: '', trim : true},
content: {type: String, default: '', trim : true},
user: {type : Schema.ObjectId, ref : 'User'}
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true
},
content: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});

/**
* Statics
*/
ArticleSchema.statics = {
load: function (id, cb) {
this.findOne({ _id : id }).populate('user').exec(cb);
}
load: function(id, cb) {
this.findOne({
_id: id
}).populate('user').exec(cb);
}
};

mongoose.model('Article', ArticleSchema);
Loading

0 comments on commit 6c3564f

Please sign in to comment.