crunch-node/routes-post.js

182 lines
6.8 KiB
JavaScript
Raw Normal View History

var config = require('./config.js').config;
var uuid = require('node-uuid');
// Post management routing
module.exports = function(app) {
// GET /admin/post/list
app.get('/admin/post/list/:start?',
function(req, res, next) {
var count = 25;
if (req.params.start) {
var start = req.params.start;
} else {
var start = 0;
}
Post.getPosts(count, start, function(err, posts){
res.render('admin-post-list', {
successNotice: req.flash('successNotice'),
failureNotice: req.flash('failureNotice'),
posts: posts,
user: req.user
});
});
}
);
// GET /admin/post/view
app.get('/admin/post/view/:uuid?',
function (req, res, next) {
if (req.params.uuid) {
Post.getByUUID(req.params.uuid, function(err, post) {
res.render('admin-post-view', {
successNotice: req.flash('successNotice'),
failureNotice: req.flash('failureNotice'),
post: post,
user: req.user
});
});
}
else {
res.redirect('/admin/post/list');
}
}
);
// GET /admin/post/new
app.get('/admin/post/new',
function(req, res, next) {
Category.getCategories(null, null, function (err, categories) {
res.render('admin-post-new', { categories: categories, user: req.user });
});
}
);
// POST /admin/post/new
app.post('/admin/post/new',
function(req, res, next) {
var post = new Post({'uuid': uuid.v1()});
post.set("title", req.body.title);
if (req.body.slug != "") {
post.set("slug", req.body.slug);
} else {
post.set("slug", post.makeSlug());
}
if (req.body.postDate != "") {
post.set("postDate", new Date(req.body.postDate));
post.set("updatedDate", new Date(req.body.postDate));
}
post.tagPost(req.body.tags);
post.set("markdown", req.body.markdown);
if (req.body.published) {
post.set("published", true);
} else {
post.set("published", false);
}
Category.getByName(req.body.category, function (err, category) {
post.set("category", category.get("uuid"));
post.save(null, function (err) {
if (!err) {
req.flash('successNotice', 'Post created.');
res.redirect('/admin/post/edit/'+post.get("uuid"));
}
else {
req.flash('failureNotice', 'Post creation failed');
res.redirect('/admin/post/new/');
}
});
});
}
);
// GET /admin/post/edit
app.get('/admin/post/edit/:uuid',
function(req, res, next) {
Post.getByUUID(req.params.uuid, function(err, post) {
Category.getByUUID(post.get("category"), function(err, category) {
Category.getCategories(null, null, function(err, categories) {
var tags = ""
for (i=0; i<post.get("tags").length; i++) {
tags += post.get("tags")[i].name + " ";
}
res.render('admin-post-edit', {
successNotice: req.flash('successNotice'),
failureNotice: req.flash('failureNotice'),
categories: categories,
category: category,
post: post,
postTags: tags,
user: req.user
});
});
});
});
}
);
// POST /admin/post/edit
app.post('/admin/post/edit/:uuid',
function(req, res, next) {
Post.getByUUID(req.params.uuid, function(err, post) {
post.set("title", req.body.title);
post.set("slug", req.body.slug);
post.set("markdown", req.body.markdown);
post.set("postDate", new Date(req.body.postDate));
post.tagPost(req.body.tags);
if (req.body.published) {
post.set("published", true);
} else {
post.set("published", false);
}
Category.getByName(req.body.category, function(err, category) {
post.set("category", category.get("uuid"));
post.save(null, function(err) {
if (!err) {
req.flash("successNotice", "Post updated.");
res.redirect("/admin/post/edit/"+post.get("uuid"));
}
else {
req.flash("failureNotice", "Post failed to update.");
res.redirect("/admin/post/edit/"+post.get("uuid"));
}
});
});
});
}
);
// GET /admin/post/build
app.get('/admin/post/build/:uuid?',
function(req, res, next) {
if (req.params.uuid) {
Static.updateBuildFolder(function (err) {
if (err) console.log(err);
});
post = Post.getByUUID(req.params.uuid, function (err, post) {
if (!err && post) {
post.build(function (err) {
if (!err) {
req.flash('successNotice', 'Post rebuilt successfully.');
res.redirect(req.header('Referer') || '/admin');
}
else {
console.log(err);
req.flash('failureNotice', 'Post regeneration failed, check logs.');
res.redirect(req.header('Referer') || '/admin');
}
});
} else {
console.log(err);
req.flash('failureNotice', 'Post regeneration failed, check logs.');
res.redirect(req.header('Referer') || '/admin');
}
});
}
else {
res.redirect('/admin/post/list');
}
}
);
}