Skip to content
Browse files

完成菜谱添加、删除、修改功能

  • Loading branch information...
1 parent 07fc99f commit f9b636b8bac7e80fe9970e5b83eadfae8e16a688 @dgunzi committed Oct 24, 2012
Showing with 74 additions and 4 deletions.
  1. +66 −0 controllers/cookbook.js
  2. +7 −3 routes.js
  3. +1 −1 views/cookbookwrite.html
View
66 controllers/cookbook.js
@@ -4,6 +4,7 @@
var models = require('../models');
var cookbook = models.cookbook;
var EventProxy = require('eventproxy').EventProxy;
+var crypto = require('crypto');
exports.cookbook_write = function(req, res, next){
res.render('cookbookwrite', {edit: '0', post: ""});
@@ -79,6 +80,71 @@ exports.cookbook_view = function(req, res, next) {
});
}
+
+exports.cookbook_add = function(req, res){
+ var d = new Date();
+ var newCookbook = new cookbook();
+
+ newCookbook.title = req.body.title;
+ newCookbook.url = req.body.url;
+ newCookbook.level = req.body.level;
+ newCookbook.major = req.body.major;
+ newCookbook.minor = req.body.minor;
+ newCookbook.seasoning = req.body.seasoning;
+ newCookbook.steps = req.body.steps;
+ newCookbook.user = 'admin';
+ newCookbook.datetime = d;
+ newCookbook.id = crypto.createHash('md5').update(newCookbook.url + newCookbook.datetime).digest("hex");
+
+ newCookbook.save(function(err){
+ if (err)
+ res.render('error', {error: err});
+ else
+ res.redirect('/cookbook');
+ });
+}
+
+exports.cookbook_update = function(req, res){
+ if (req.params.id){
+ cookbook.findOne({id: req.params.id}, function(err, doc){
+ if (!doc)
+ res.render('error', {error: '无此信息或已被删除'});
+ else {
+ var d = new Date();
+ doc.title = req.body.title;
+ doc.url = req.body.url;
+ doc.level = req.body.level;
+ doc.major = req.body.major;
+ doc.minor = req.body.minor;
+ doc.seasoning = req.body.seasoning;
+ doc.steps = req.body.steps;
+ doc.user = 'admin';
+ doc.datetime = d;
+ doc.save(function(err){
+ if (err)
+ res.render('error', {error: err});
+ else
+ res.redirect('/cookbook');
+ });
+ }
+ });
+ }
+}
+
+exports.cookbook_del = function(req, res){
+ if ( req.params.type == 'a')
+ {
+ if ( req.params.id != "" ){
+ cookbook.remove({id: req.params.id}, function(err){
+ if (err)
+ res.render('error', {error: err});
+ else
+ res.redirect('/cookbook');
+ });
+ }
+ }
+};
+
//指明查询条件查询多条记录
function get_cookbook_by_query(where, opt, cb){
var query = cookbook.find(where).limit(opt.limit).skip(opt.skip).sort({ datetime: 'desc'});;
View
10 routes.js
@@ -15,10 +15,14 @@ module.exports = function (app) {
app.get('/cookbook', cookbook.cookbook_admin);
//edit cookbook
app.get('/edit/:type/:id', cookbook.cookbook_edit);
- // write
+ // write cookbook
app.get('/write', cookbook.cookbook_write);
-
-
+ // new cookbook
+ app.post('/newcookbook', cookbook.cookbook_add);
+ // update cookbook
+ app.post('/update/:id', cookbook.cookbook_update);
+ // delete cookbook
+ app.get('/delete/:type/:id', cookbook.cookbook_del);
// Route that takes the post upload request and sends the server response
app.post('/upload', function(req, res) {
var uploadPath = __dirname + config.upload_dir;
View
2 views/cookbookwrite.html
@@ -50,7 +50,7 @@
,minor = ""
,seasoning = ""
,steps = ""
- ,action="/newpost";
+ ,action="/newcookbook";
if ( edit == '1')
{

0 comments on commit f9b636b

Please sign in to comment.
Something went wrong with that request. Please try again.