Skip to content

Commit

Permalink
use es6 yields
Browse files Browse the repository at this point in the history
  • Loading branch information
madhums committed Nov 23, 2015
1 parent 14c0db6 commit e57c2ef
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 186 deletions.
99 changes: 36 additions & 63 deletions app/controllers/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,43 @@
*/

const mongoose = require('mongoose');
const Article = mongoose.model('Article');
const utils = require('../../lib/utils');
const assign = require('object-assign');

const wrap = require('co-express');
const only = require('only');
const Article = mongoose.model('Article');

/**
* Load
*/

exports.load = function (req, res, next, id){
Article.load(id, function (err, article) {
if (err) return next(err);
if (!article) return next(new Error('not found'));
req.article = article;
next();
});
};
exports.load = wrap(function* (req, res, next, id) {
req.article = yield Article.load(id);
if (!req.article) return next(new Error('Article not found'));
next();
});

/**
* List
*/

exports.index = function (req, res) {
exports.index = wrap(function* (req, res) {
const page = (req.query.page > 0 ? req.query.page : 1) - 1;
const perPage = 30;
const limit = 30;
const options = {
perPage: perPage,
limit: limit,
page: page
};

Article.list(options, function (err, articles) {
if (err) return res.render('500');
Article.count().exec(function (err, count) {
res.render('articles/index', {
title: 'Articles',
articles: articles,
page: page + 1,
pages: Math.ceil(count / perPage)
});
});
const articles = yield Article.list(options);
const count = yield Article.count();

res.render('articles/index', {
title: 'Articles',
articles: articles,
page: page + 1,
pages: Math.ceil(count / limit)
});
};
});

/**
* New article
Expand All @@ -64,25 +59,17 @@ exports.new = function (req, res){
* Upload an image
*/

exports.create = function (req, res) {
const article = new Article(req.body);
exports.create = wrap(function* (req, res) {
const article = new Article(only(req.body, 'title body tags'));
const images = req.files.image
? [req.files.image]
: undefined;

article.user = req.user;
article.uploadAndSave(images, function (err) {
if (!err) {
req.flash('success', 'Successfully created article!');
return res.redirect('/articles/'+article._id);
}
res.render('articles/new', {
title: 'New Article',
article: article,
errors: utils.errors(err.errors || err)
});
});
};
yield article.uploadAndSave(images);
req.flash('success', 'Successfully created article!');
res.redirect('/articles/' + article._id);
});

/**
* Edit an article
Expand All @@ -99,28 +86,16 @@ exports.edit = function (req, res) {
* Update article
*/

exports.update = function (req, res){
exports.update = wrap(function* (req, res){
const article = req.article;
const images = req.files.image
? [req.files.image]
: undefined;

// make sure no one changes the user
delete req.body.user;
assign(article, req.body);

article.uploadAndSave(images, function (err) {
if (!err) {
return res.redirect('/articles/' + article._id);
}

res.render('articles/edit', {
title: 'Edit Article',
article: article,
errors: utils.errors(err.errors || err)
});
});
};
assign(article, only(req.body, 'title body tags'));
yield article.uploadAndSave(images);
res.redirect('/articles/' + article._id);
});

/**
* Show
Expand All @@ -137,10 +112,8 @@ exports.show = function (req, res){
* Delete an article
*/

exports.destroy = function (req, res){
const article = req.article;
article.remove(function () {
req.flash('info', 'Deleted successfully');
res.redirect('/articles');
});
};
exports.destroy = wrap(function* (req, res) {
yield req.article.remove();
req.flash('success', 'Deleted successfully');
res.redirect('/articles');
});
43 changes: 16 additions & 27 deletions app/controllers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,37 @@
* Module dependencies.
*/

const utils = require('../../lib/utils');
const wrap = require('co-express');

/**
* Load comment
*/

exports.load = function (req, res, next, id) {
const article = req.article;
utils.findByParam(article.comments, { id: id }, function (err, comment) {
if (err) return next(err);
req.comment = comment;
next();
});
req.comment = req.article.comments
.filter(comment => comment.id === id)
.reduce((p, n) => n, null);

if (!req.comment) return next(new Error('Comment not found'));
next();
};

/**
* Create comment
*/

exports.create = function (req, res) {
exports.create = wrap(function* (req, res) {
const article = req.article;
const user = req.user;
if (!req.body.body) return res.redirect('/articles/'+ article.id);

article.addComment(user, req.body, function (err) {
if (err) return res.render('500');
res.redirect('/articles/'+ article.id);
});
};
yield article.addComment(req.user, req.body);
res.redirect('/articles/' + article.id);
});

/**
* Delete comment
*/

exports.destroy = function (req, res) {
const article = req.article;
article.removeComment(req.params.commentId, function (err) {
if (err) {
req.flash('error', 'Oops! The comment was not found');
} else {
req.flash('info', 'Removed comment');
}
res.redirect('/articles/' + article.id);
});
};
exports.destroy = wrap(function* (req, res) {
yield req.article.removeComment(req.params.commentId);
req.flash('info', 'Removed comment');
res.redirect('/articles/' + req.article.id);
});
27 changes: 13 additions & 14 deletions app/controllers/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@
*/

const mongoose = require('mongoose');
const wrap = require('co-express');
const Article = mongoose.model('Article');

/**
* List items tagged with a tag
*/

exports.index = function (req, res) {
exports.index = wrap(function* (req, res) {
const criteria = { tags: req.params.tag };
const perPage = 5;
const page = (req.params.page > 0 ? req.params.page : 1) - 1;
const limit = 30;
const options = {
perPage: perPage,
limit: limit,
page: page,
criteria: criteria
};

Article.list(options, function (err, articles) {
if (err) return res.render('500');
Article.count(criteria).exec(function (err, count) {
res.render('articles/index', {
title: 'Articles tagged ' + req.params.tag,
articles: articles,
page: page + 1,
pages: Math.ceil(count / perPage)
});
});
const articles = yield Article.list(options);
const count = yield Article.count(criteria);

res.render('articles/index', {
title: 'Articles tagged ' + req.params.tag,
articles: articles,
page: page + 1,
pages: Math.ceil(count / limit)
});
};
});
2 changes: 1 addition & 1 deletion app/mailer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

const Notifier = require('notifier');
const swig = require('swig');
const config = require('config');
const config = require('../../config/config');

/**
* Process the templates using swig - refer to notifier#processTemplate method
Expand Down
Loading

0 comments on commit e57c2ef

Please sign in to comment.