Skip to content

Commit

Permalink
[Structure] Introduce the common module.
Browse files Browse the repository at this point in the history
  • Loading branch information
yfwz100 committed Jan 23, 2013
1 parent 9a3440d commit ce2ba52
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 129 deletions.
File renamed without changes.
57 changes: 13 additions & 44 deletions routes/admin/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var async = require('async')
, Post = require('../../../my/model/post')
, Catalog = require('../../../my/model/catalog');
var common = require('../../../my/view/common');

exports.catalog = require('./catalog');

Expand All @@ -17,7 +18,7 @@ function fetchAllCatalogsTask(callback) {
* @see #create()
*/
exports.createView = function createView(req, res) {
async.series({
async.parallel({
catalogs: fetchAllCatalogsTask
}, function(err, results) {
res.render('admin/article-edit-page', {
Expand Down Expand Up @@ -54,20 +55,13 @@ exports.create = function create(req, res) {
message: '保存错误:' + err,
link: '/admin/article/new'
});
common.error(res, err, '/admin/article/new');
} else {
res.render('done', {
title: '完成',
message: '保存成功!',
link: '/admin/'
});
common.info(res, '保存成功!', '/admin/article/new');
}
});
} else {
res.render('error', {
title: '错误参数',
message: '错误参数!',
link: '/admin/'
});
common.error(res, '错误参数!', '/admin/');
}
};

Expand All @@ -82,17 +76,9 @@ exports.remove = function remove(req, res) {
url: url
}, function(err, post) {
if (err) {
res.render('error', {
title: '出错了~',
message: err,
link: '/admin/'
});
common.error(res, err, '/admin/');
} else {
res.render('done', {
title: '完成',
message: '删除文档。',
link: '/admin/'
});
common.info(res, '删除文档。', '/admn/');
}
});
};
Expand All @@ -105,18 +91,14 @@ exports.updateView = function updateView(req, res) {
var catalog = req.params.catalog;
var url = req.params.url;
var author = req.session.user.name;
async.series({
async.parallel({
post: function(callback) {
Post.getByUrl(catalog, url, callback);
},
catalogs: fetchAllCatalogsTask
}, function(err, results) {
if (err || !results.post || results.post.author != author) {
res.render('error', {
title: '出错了~',
message: '您确认这个URL是有效的?',
link: '/admin/'
});
common.error(res, '您确认这个URL是有效的?');
} else {
var url = results.post.catalog + '/' + results.post.getUrl();
res.render('admin/article-edit-page', {
Expand Down Expand Up @@ -157,17 +139,9 @@ exports.update = function update(req, res) {
author: author
}, doc, {}, function(err) {
if (err) {
res.render('error', {
title: '出错了~',
message: '数据库链接可能出错了~',
link: '/admin/'
});
common.error(res, err, '/admin/');
} else {
res.render('done', {
title: '完成',
message: '完成编辑~',
link: '/admin/articles/'
});
common.info(res, '完成编辑!', '/admin/articles/');
}
});
}
Expand All @@ -188,7 +162,7 @@ exports.browse = function browse(req, res) {
if (catalog) {
option.catalog = catalog;
}
async.series({
async.parallel({
posts: function(callback) {
Post.list(option, function(err, posts) {
callback(err, posts);
Expand All @@ -212,13 +186,8 @@ exports.browse = function browse(req, res) {
posts: results.posts
});
} else {
res.render('error', {
title: '文章',
message: '没有这个栏目或者数据库出错了~',
link: '/admin/'
});
common.error(res, err, '/admin/');
}
});
};


7 changes: 4 additions & 3 deletions routes/admin/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* The home controller for administration.
*/

var async = require('async'),
Message = require('../../my/model/message');
var async = require('async')
var Message = require('../../my/model/message');
var common = require('../../my/view/common');

exports.index = function index(req, res) {
async.series({
async.parallel({
numOfUnread: function(callback) {
var user = req.session.user;
Message.count({
Expand Down
18 changes: 4 additions & 14 deletions routes/admin/message.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@

var Message = require('../../my/model/message');
var Message = require('../../my/model/message')
, common = require('../../my/view/common');

function info(res, message, link) {
res.render('done', {
message: message,
link: link
});
}

function error(res, message, link) {
res.render('error', {
message: message,
link: link
});
}
var info = common.info;
var error = common.error;

module.exports = {
browse: function browse(req, res) {
Expand Down
16 changes: 3 additions & 13 deletions routes/admin/picture.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@

var async = require('async');
var Picture = require('../../my/model/picture');
var common = require('../../my/view/common');

function error(res, message, link) {
res.render('error', {
message: message,
link: link
});
}

function info(res, message, link) {
res.render('done', {
message: message,
link: link
});
}
var error = common.error;
var info = common.info;

module.exports = {
createView: function createView(req, res) {
Expand Down
62 changes: 13 additions & 49 deletions routes/admin/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

var User = require('../../my/model/user');
var common = require('../../my/view/common');

/*
* Check if the user has login or not.
Expand Down Expand Up @@ -53,25 +54,13 @@ exports.register = function register(req, res) {
});
user.save(function(err) {
if (!err) {
res.render('done', {
title: '完成',
link: '/admin/login',
message: '成功注册!请等候审核。'
});
common.info(res, '成功注册!请等候审核。', '/admin/login');
} else {
res.render('error', {
title: '出错了!',
link: '/admin/register',
message: err
});
common.error(res, err, '/admin/register');
}
});
} else {
res.render('error', {
title: '出错了!',
link: '/admin/register',
message: '参数错误。'
});
common.error(res, '参数错误!', '/admin/register');
}
};

Expand All @@ -98,21 +87,13 @@ exports.login = function login(req, res) {
if (user && username == user.name && password == user.password && user.verified) {
req.session.user = user.toJSON();
req.session.user.loginDate = new Date;
res.render('done', {
title: '登陆成功!',
message: '成功登陆~',
link: '/admin/'
});
common.info(res, '成功登陆~', '/admin/');
} else {
var message = '用户名或密码错误!';
if (user && ! user.verified) {
message = '用户尚未激活。';
}
res.render('error', {
title: '错误!',
message: message,
link: '/admin/register'
});
common.error(res, message, '/admin/register');
}
});
};
Expand All @@ -127,6 +108,7 @@ exports.logout = function logout(req, res) {
link: '/admin/login',
message: '已经安全退出本系统。'
});
common.info(res, '已经安全退出本系统。', '/admin/login');
};

exports.browse = function browse(req, res) {
Expand All @@ -137,10 +119,7 @@ exports.browse = function browse(req, res) {
users: users
});
} else {
res.render('error', {
message: err,
link: '/admin/'
});
common.error(res, err, '/admin/');
}
});
};
Expand All @@ -151,10 +130,7 @@ exports.updateView = function updateView(req, res) {
_id: id
}, function(err, user) {
if (err) {
res.render('error', {
message: err,
link: '/admin/users/'
});
common.error(res, err, '/admin/users/');
} else {
res.render('admin/user-edit-page', {
actionUrl: '/admin/users/edit/' + id,
Expand Down Expand Up @@ -184,18 +160,12 @@ exports.update = function update(req, res) {
_id: id
}, updated, function(err, user) {
if (err) {
res.render('error', {
message: err,
link: '/admin/users/'
});
common.error(res, err, '/admin/users');
} else {
if (id == req.session.user._id) {
req.session.user = user;
}
res.render('done', {
message: '完成编辑!',
link: '/admin/users/'
});
common.info(res, '完成编辑!', '/admin/users/');
}
});
};
Expand All @@ -209,15 +179,9 @@ exports.verify = function verify(req, res) {
_id: id
}, updated, function(err, user) {
if (err) {
res.render('error', {
message: err,
link: '/admin/users/'
});
common.error(res, err, '/admin/users/');
} else {
res.render('done', {
message: '完成激活!',
link: '/admin/users/'
});
common.info(res, '完成激活!', '/admin/users/');
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion routes/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The Article module
*/

var common = require('./common');
var common = require('../my/view/common');
var Post = require('../my/model/post')
, Catalog = require('../my/model/catalog');

Expand Down
15 changes: 10 additions & 5 deletions routes/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
* The homepage of the neuol site.
*/

var SlidePic = require('../my/model/picture');
var SlidePic = require('../my/model/picture')
, common = require('../my/view/common');

exports.index = function index(req, res) {
SlidePic.list(3, function(err, slides) {
res.render('index', {
title: 'Neuola',
headPics: slides
});
if (err) {
common.error(res, err, '/');
} else {
res.render('index', {
title: 'Neuola',
headPics: slides
});
}
});
}

Expand Down

0 comments on commit ce2ba52

Please sign in to comment.