Created
December 17, 2013 16:47
-
-
Save matiasa18/8008133 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var passport = require('passport'), | |
Category = require('../../../models/category'), | |
messages = require('../../../modules/messages'); | |
module.exports = function(app) { | |
app.get('/admin/categories/add', function(req, res) { | |
Category.find(function(err, categories) { | |
if (err) throw err; | |
res.render('admin/categories/form', {title: 'Add Category', category: new Category(), path: '/admin/categories/add', categories: categories, edit: false}); | |
}); | |
}); | |
app.post('/admin/categories/add', function(req, res) { | |
var category = new Category(), | |
parent_category = null, | |
parent_id = req.body.parent_id, | |
name = req.body.name; | |
category.name = name; | |
category.parent_id = parent_id; | |
if (parent_id != 0 && parent_id != null) { | |
Category.findOne({_id: parent_id}, function(err, parent_category) { | |
if (err) throw err; | |
console.log(parent_category); | |
parent_category.categories.push(category); | |
parent_category.save(function (err) { | |
if (err != null) { | |
err = messages.get_from_model(err); | |
return res.render('admin/categories/form', {category: category, error_messages: [err], path: '/admin/categories/add'}); | |
} else { | |
res.redirect('/admin/categories'); | |
res.end(); | |
return; | |
} | |
}); | |
}); | |
} else { | |
category.save(function (err) { | |
if (err != null) { | |
err = messages.get_from_model(err); | |
return res.render('admin/categories/form', {category: category, error_messages: [err], path: '/admin/categories/add'}); | |
} else { | |
res.redirect('/admin/categories'); | |
res.end(); | |
return; | |
} | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment