171 lines
6.4 KiB
JavaScript
171 lines
6.4 KiB
JavaScript
// 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', {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,
|
|
content: markdown.toHTML(post.get("markdown")),
|
|
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();
|
|
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"));
|
|
//console.log(post);
|
|
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/regenerate
|
|
app.get('/admin/post/regenerate/: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) {
|
|
console.log("Got post: "+post.get("title"));
|
|
if (err) console.log(err);
|
|
post.generatePost(function (err) {
|
|
if (!err) {
|
|
req.flash('successNotice', 'Post regenerated successfully.');
|
|
res.redirect('/admin/post/view/'+req.params.uuid);
|
|
}
|
|
else {
|
|
console.log(err);
|
|
req.flash('failureNotice', 'Post regeneration failed, check logs.');
|
|
res.redirect('/admin/post/view/'+req.params.uuid);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
else {
|
|
res.redirect('/admin/post/list');
|
|
}
|
|
}
|
|
);
|
|
}
|