Updating post model for more completeness.
This commit is contained in:
parent
cf36d983a0
commit
1a3da60b24
8 changed files with 252 additions and 513 deletions
176
app.js
176
app.js
|
@ -9,18 +9,21 @@ var fs = require('fs');
|
||||||
var markdown = require( "markdown" ).markdown;
|
var markdown = require( "markdown" ).markdown;
|
||||||
var moment = require("moment");
|
var moment = require("moment");
|
||||||
|
|
||||||
|
// Make ourselves a nice little express app.
|
||||||
|
var app = express();
|
||||||
|
|
||||||
// Get connected to our database
|
// Get connected to our database
|
||||||
var MongoClient = require('mongodb').MongoClient;
|
var MongoClient = require('mongodb').MongoClient;
|
||||||
|
|
||||||
|
// Include some other JS
|
||||||
Post = require('./post.js');
|
Post = require('./post.js');
|
||||||
User = require('./user.js');
|
User = require('./user.js');
|
||||||
Category = require('./category.js');
|
Category = require('./category.js');
|
||||||
Static = require('./static.js');
|
Static = require('./static.js');
|
||||||
var database = require('./db.js');
|
var database = require('./db.js');
|
||||||
var genPhotos = require('./genPhotos.js');
|
var genPhotos = require('./genPhotos.js');
|
||||||
|
require('./post-routes.js')(app);
|
||||||
var app = express();
|
require('./photo-routes.js')(app);
|
||||||
|
|
||||||
// Get config variables
|
// Get config variables
|
||||||
var config = require('./config.js').config;
|
var config = require('./config.js').config;
|
||||||
|
@ -29,9 +32,6 @@ var config = require('./config.js').config;
|
||||||
app.set('views', path.join(__dirname, 'views'));
|
app.set('views', path.join(__dirname, 'views'));
|
||||||
app.set('view engine', 'jade');
|
app.set('view engine', 'jade');
|
||||||
|
|
||||||
// Make HTML pretty while we're setting up our jade templates
|
|
||||||
// but it doesn't need to be pretty in production.
|
|
||||||
|
|
||||||
// uncomment after placing your favicon in /public
|
// uncomment after placing your favicon in /public
|
||||||
//app.use(require('serve-favicon')(path.join(__dirname, 'public',
|
//app.use(require('serve-favicon')(path.join(__dirname, 'public',
|
||||||
// 'favicon.ico')));
|
// 'favicon.ico')));
|
||||||
|
@ -60,12 +60,12 @@ passport.use(new Strategy(
|
||||||
));
|
));
|
||||||
|
|
||||||
passport.serializeUser(function(user, done) {
|
passport.serializeUser(function(user, done) {
|
||||||
console.log("Serializing user: "+user.username);
|
// console.log("Serializing user: "+user.username);
|
||||||
done(null, user.username);
|
done(null, user.username);
|
||||||
});
|
});
|
||||||
|
|
||||||
passport.deserializeUser(function(username, done) {
|
passport.deserializeUser(function(username, done) {
|
||||||
console.log("Deserializing user: "+username);
|
// console.log("Deserializing user: "+username);
|
||||||
User.getByUsername(username, function (err, user) {
|
User.getByUsername(username, function (err, user) {
|
||||||
if (!user) { return done(false); }
|
if (!user) { return done(false); }
|
||||||
done(null, user);
|
done(null, user);
|
||||||
|
@ -93,163 +93,6 @@ app.get('/logout', function(req, res) {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Post management routing
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
Category.getByName(req.body.category, function (err, category) {
|
|
||||||
post.set("category", category.get("uuid"));
|
|
||||||
//console.log(post);
|
|
||||||
post.save(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);
|
|
||||||
Category.getByName(req.body.category, function(err, category) {
|
|
||||||
post.set("category", category.get("uuid"));
|
|
||||||
post.save(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');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Photo management Routing
|
// Photo management Routing
|
||||||
app.get('/admin/photo/list/:start?',
|
app.get('/admin/photo/list/:start?',
|
||||||
function(req, res, next) {
|
function(req, res, next) {
|
||||||
|
@ -621,6 +464,11 @@ app.use(function(err, req, res, next) {
|
||||||
app.set('port', process.env.PORT || 3000)
|
app.set('port', process.env.PORT || 3000)
|
||||||
var server = require('http').createServer(app)
|
var server = require('http').createServer(app)
|
||||||
|
|
||||||
|
process.on('uncaughtException', function (exception) {
|
||||||
|
console.log(exception);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
MongoClient.connect('mongodb://localhost/crunch', function(err, database) {
|
MongoClient.connect('mongodb://localhost/crunch', function(err, database) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
global.db = database;
|
global.db = database;
|
||||||
|
|
338
db.js
338
db.js
|
@ -5,34 +5,6 @@ var config = require('./config.js').config;
|
||||||
|
|
||||||
var db = new sqlite.Database(config.dbPath);
|
var db = new sqlite.Database(config.dbPath);
|
||||||
|
|
||||||
// Function to get the latest regenerate date
|
|
||||||
// Returns a epoch time in seconds
|
|
||||||
exports.getLastRegenerateDate = function(cb) {
|
|
||||||
db.get('SELECT MAX(lastGenerateDate) as date FROM ( \
|
|
||||||
SELECT lastGenerateDate FROM posts \
|
|
||||||
UNION \
|
|
||||||
SELECT lastGenerateDate FROM photos \
|
|
||||||
UNION \
|
|
||||||
SELECT lastGenerateDate FROM galleries);',
|
|
||||||
function(err, row) {
|
|
||||||
cb(row);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get the latest upload date
|
|
||||||
// Returns a epoch time in seconds
|
|
||||||
exports.getLastUploadDate = function(cb) {
|
|
||||||
db.get('SELECT MAX(lastUpload) as date FROM ( \
|
|
||||||
SELECT lastUpload FROM posts \
|
|
||||||
UNION \
|
|
||||||
SELECT lastUpload FROM photos \
|
|
||||||
UNION \
|
|
||||||
SELECT lastUpload FROM galleries);',
|
|
||||||
function(err, row) {
|
|
||||||
cb(row);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get a count of current galleries
|
// Function to get a count of current galleries
|
||||||
// Returns count of galleries
|
// Returns count of galleries
|
||||||
exports.countGalleries = function(cb) {
|
exports.countGalleries = function(cb) {
|
||||||
|
@ -41,14 +13,6 @@ exports.countGalleries = function(cb) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to get a count of current categories
|
|
||||||
// Returns count of categories
|
|
||||||
exports.countCategories = function(cb) {
|
|
||||||
db.get('SELECT count(id) as count FROM categories;', function(err, count) {
|
|
||||||
cb(count.count);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get a count of current photos
|
// Function to get a count of current photos
|
||||||
// Returns count of tags
|
// Returns count of tags
|
||||||
exports.countPhotos = function(cb) {
|
exports.countPhotos = function(cb) {
|
||||||
|
@ -57,308 +21,6 @@ exports.countPhotos = function(cb) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to get a count of current tags
|
|
||||||
// Returns count of tags
|
|
||||||
exports.countTags = function(cb) {
|
|
||||||
db.get('SELECT count(id) as count FROM tags;', function(err, count) {
|
|
||||||
cb(count.count);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get all categories including their ID and descriptions
|
|
||||||
// Returns array of rows of categories
|
|
||||||
exports.listCategories = function(cb) {
|
|
||||||
db.all('SELECT * FROM CATEGORIES', function(err, rows) {
|
|
||||||
cb(rows);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get a list of photos of a certain count and starting at an offset
|
|
||||||
// Returns a list of photo objects
|
|
||||||
exports.listPhotos = function(count, start, cb) {
|
|
||||||
db.all('SELECT * FROM photos WHERE deleted = 0 \
|
|
||||||
ORDER BY photoDate DESC, title ASC \
|
|
||||||
LIMIT '+count+' OFFSET '+start+';',
|
|
||||||
function(err, photos) {
|
|
||||||
console.log(err);
|
|
||||||
cb(photos);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// NON-EXPORTED function to create a category if it does not exist
|
|
||||||
// Returns a row of category information
|
|
||||||
var getOrCreateCategory = function(name, cb) {
|
|
||||||
db.get('SELECT * FROM categories WHERE name = "'+name+'";', function(err, row) {
|
|
||||||
if (!err && row) {
|
|
||||||
console.log('Category '+name+' exists.');
|
|
||||||
cb(row);
|
|
||||||
} else {
|
|
||||||
console.log('Category '+name+' does not exist. Creating...');
|
|
||||||
epoch = helper.dateToEpoch(new Date());
|
|
||||||
db.run('INSERT INTO categories (\
|
|
||||||
name, slug, createDate \
|
|
||||||
) VALUES (\
|
|
||||||
"'+name+'", \
|
|
||||||
"'+helper.makeSlug(name)+'", \
|
|
||||||
'+epoch+');',
|
|
||||||
function(err, row) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log('Category '+name+' created.');
|
|
||||||
db.get('SELECT * FROM categories WHERE name = "'+name+'";',
|
|
||||||
function(err, row){
|
|
||||||
cb(row);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to set a category for a post given a post id and a category name
|
|
||||||
// Returns error code
|
|
||||||
exports.setPostCategory = function(postId, categoryName, cb) {
|
|
||||||
console.log('Setting post '+postId+' category to '+categoryName);
|
|
||||||
getOrCreateCategory(categoryName, function(category) {
|
|
||||||
db.get('SELECT categoryId FROM categoryPosts \
|
|
||||||
WHERE postId = '+postId+';',
|
|
||||||
function(err, row) {
|
|
||||||
if (row) {
|
|
||||||
console.log('Post '+postId+' has assigned category of '+row.categoryId);
|
|
||||||
if (row.categoryId == category.id) {
|
|
||||||
console.log('Current post category is the same as new post category, no update necessary.');
|
|
||||||
cb(null);
|
|
||||||
} else {
|
|
||||||
console.log("Current post category ("+row.categoryId+") doesn't match new category ("+category.id+"), updating record.");
|
|
||||||
db.run('UPDATE categoryPosts SET \
|
|
||||||
categoryId = '+category.id+' \
|
|
||||||
WHERE postId = '+postId+';',
|
|
||||||
function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log("Category updated.");
|
|
||||||
cb(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('Post '+postId+' has no category assigned. Inserting record.');
|
|
||||||
db.run('INSERT INTO categoryPosts (postId, categoryId) VALUES ('+postId+', '+category.id+');',
|
|
||||||
function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log('Post '+postId+' assigned to category '+category.id);
|
|
||||||
cb(null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to create a new post
|
|
||||||
// Returns the post id
|
|
||||||
exports.createPost = function(title, slug, markdown, postDate, updatedDate,
|
|
||||||
createDate, cb) {
|
|
||||||
db.run('INSERT INTO posts (\
|
|
||||||
title, \
|
|
||||||
slug, \
|
|
||||||
markdown, \
|
|
||||||
postDate, \
|
|
||||||
createDate, \
|
|
||||||
updatedDate, \
|
|
||||||
lastGenerateDate, \
|
|
||||||
lastUpload) \
|
|
||||||
VALUES (\
|
|
||||||
"'+title+'", \
|
|
||||||
"'+slug+'", \
|
|
||||||
"'+markdown+'", \
|
|
||||||
"'+postDate+'", \
|
|
||||||
"'+createDate+'", \
|
|
||||||
"'+updatedDate+'", \
|
|
||||||
(strftime("%s","1900-01-01 00:00")*1000), \
|
|
||||||
(strftime("%s","1900-01-01 00:00")*1000));',
|
|
||||||
function(err, row) {
|
|
||||||
if (err) console.log(err);
|
|
||||||
db.get('SELECT * FROM posts WHERE title = "'+title+'" AND createDate = '+createDate+';', function(err, row){
|
|
||||||
cb(err, row);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to update an existing post record
|
|
||||||
// Returns the post object
|
|
||||||
exports.updatePost = function(id, title, slug, markdown, postDate, cb) {
|
|
||||||
console.log("updatePost called.");
|
|
||||||
db.run('UPDATE posts SET \
|
|
||||||
title = "'+title+'", \
|
|
||||||
slug = "'+slug+'", \
|
|
||||||
markdown = "'+markdown+'", \
|
|
||||||
postDate = '+helper.dateToEpoch(postDate)+', \
|
|
||||||
updatedDate = '+helper.dateToEpoch(new Date())+' \
|
|
||||||
WHERE id = '+id+';',
|
|
||||||
function(err) {
|
|
||||||
console.log('updatePost UPDATE result: '+err);
|
|
||||||
db.get('SELECT * FROM posts WHERE id = '+id+';', function(err, row) {
|
|
||||||
console.log('updatePost SELECT result: ' + err);
|
|
||||||
cb(row);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get a post record by the id of the post
|
|
||||||
// Returns the post record
|
|
||||||
exports.getPostById = function(id, cb) {
|
|
||||||
db.get('SELECT * FROM posts WHERE id = ?', id, function(err, row) {
|
|
||||||
cb(row);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get a category record given a post id
|
|
||||||
// Returns the first category record associated with the post.
|
|
||||||
exports.getPostCategory = function(id, cb) {
|
|
||||||
db.get('SELECT * FROM categories WHERE id = (\
|
|
||||||
SELECT categoryId FROM categoryPosts WHERE postId = '+id+');',
|
|
||||||
function(err, row) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
if (row) {
|
|
||||||
console.log('Post '+id+' category is '+row.name);
|
|
||||||
cb(row);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cb(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to get record of a tag, inserting it if required.
|
|
||||||
// Inputs are a tag name and a callback.
|
|
||||||
// Returns the created tag record.
|
|
||||||
var getOrCreateTag = function(tag, cb) {
|
|
||||||
var slug = helper.makeSlug(tag);
|
|
||||||
db.get('SELECT * from tags WHERE slug = "'+slug+'" LIMIT 1;', function(err, row) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
if (row) {
|
|
||||||
console.log(slug+' tag exists');
|
|
||||||
cb(row);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
db.run('INSERT INTO tags (name, slug, createDate) \
|
|
||||||
VALUES ("'+tag+'", "'+slug+'", '+ helper.dateToEpoch(new Date()) +');',
|
|
||||||
function(err, id) {
|
|
||||||
if (!err) {
|
|
||||||
console.log(slug+' tag created');
|
|
||||||
db.get('SELECT * FROM tags WHERE slug = "'+slug+'" LIMIT 1;',
|
|
||||||
function (err, row) {
|
|
||||||
if (err) console.log(err);
|
|
||||||
if (row) cb(row);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getOrCreateTag = getOrCreateTag;
|
|
||||||
|
|
||||||
|
|
||||||
// Function to tag a post with a list of tag names
|
|
||||||
// Inputs: Post ID, List of Tag names
|
|
||||||
// Does not return.
|
|
||||||
exports.tagPost = function (postId, tags) {
|
|
||||||
|
|
||||||
console.log('Deleting old tags');
|
|
||||||
db.run('DELETE FROM postTags WHERE postId = '+postId+';',
|
|
||||||
function(err) {
|
|
||||||
console.log('Old tags deleted');
|
|
||||||
for (var i = 0, size = tags.length; i < size; i++) {
|
|
||||||
getOrCreateTag(tags[i], function(row) {
|
|
||||||
db.run('INSERT INTO postTags (postId, tagId) \
|
|
||||||
VALUES ('+postId+', '+row.id+');',
|
|
||||||
function(err) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
console.log('Post '+postId+' tagged as '+row.name);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Function to get tag ids associated with a particular post id
|
|
||||||
// Inputs: Post ID
|
|
||||||
// Returns: Records associated with tags of that post id
|
|
||||||
var getPostTags = function (postId, cb) {
|
|
||||||
db.all('SELECT tagId FROM postTags WHERE postId = '+postId+';',
|
|
||||||
function(err, rows) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var tagList = [];
|
|
||||||
for (row in rows) {
|
|
||||||
tagList.push(rows[row].tagId);
|
|
||||||
}
|
|
||||||
tagList = tagList.join(', ');
|
|
||||||
console.log('Tag ids for '+postId+': '+tagList);
|
|
||||||
db.all('SELECT * FROM tags WHERE id IN ('+tagList+');',
|
|
||||||
function(err, rows) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cb(rows);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getPostTags = getPostTags;
|
|
||||||
|
|
||||||
|
|
||||||
// Function that returns all the post tags as a space separated string.
|
|
||||||
// Inputs: Post ID, callback function.
|
|
||||||
// Returns: callback of string with all tags separated by spaces.
|
|
||||||
var getPostTagsAsString = function(postId, cb) {
|
|
||||||
getPostTags(postId, function(tags) {
|
|
||||||
var str = false;
|
|
||||||
for (tag in tags) {
|
|
||||||
if (!str) {
|
|
||||||
str = tags[tag].name;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
str = str + ' ' + tags[tag].name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log("Tags for "+postId+": "+str);
|
|
||||||
cb(str);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.getPostTagsAsString = getPostTagsAsString;
|
|
||||||
|
|
||||||
|
|
||||||
// Function to tag a photo with a list of tag names
|
// Function to tag a photo with a list of tag names
|
||||||
// Inputs: Photo ID, List of Tag names
|
// Inputs: Photo ID, List of Tag names
|
||||||
// Does not return.
|
// Does not return.
|
||||||
|
|
171
post-routes.js
Normal file
171
post-routes.js
Normal file
|
@ -0,0 +1,171 @@
|
||||||
|
// 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');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
59
post.js
59
post.js
|
@ -32,9 +32,13 @@ Post.prototype.sanitize = function (data) {
|
||||||
return _.pick(_.defaults(data, schema), _.keys(schema));
|
return _.pick(_.defaults(data, schema), _.keys(schema));
|
||||||
}
|
}
|
||||||
|
|
||||||
Post.prototype.save = function (callback) {
|
Post.prototype.save = function (date, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
this.set("updatedDate", new Date());
|
if (date) {
|
||||||
|
this.set("updatedDate", date);
|
||||||
|
} else {
|
||||||
|
this.set("updatedDate", new Date());
|
||||||
|
}
|
||||||
this.data = this.sanitize(this.data);
|
this.data = this.sanitize(this.data);
|
||||||
db.collection("posts").update({uuid: this.get("uuid")}, this.data, {upsert: true}, function(err) {
|
db.collection("posts").update({uuid: this.get("uuid")}, this.data, {upsert: true}, function(err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
|
@ -156,10 +160,15 @@ Post.prototype.generatePost = function (callback) {
|
||||||
console.log('Rendering post: '+this.get("title"));
|
console.log('Rendering post: '+this.get("title"));
|
||||||
var jadeOut = jade.renderFile('views/render-post.jade', options);
|
var jadeOut = jade.renderFile('views/render-post.jade', options);
|
||||||
|
|
||||||
console.log('Creating directory: '+ this.getDirectory());
|
self = this;
|
||||||
mkdirp.sync(this.getDirectory());
|
|
||||||
fs.writeFile(this.getFilePath(), jadeOut, 'utf-8', function (err) {
|
mkdirp(self.getDirectory(), function (err) {
|
||||||
callback(err);
|
fs.writeFile(self.getFilePath(), jadeOut, 'utf-8', function (err) {
|
||||||
|
self.set('lastGenerateDate', new Date());
|
||||||
|
self.save(self.get('lastGenerateDate'), function (err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,6 +248,44 @@ Post.getPosts = function(count, start, callback) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to find posts that need to be generated
|
||||||
|
// Inputs: Callback function
|
||||||
|
// Returns: list of Post objects
|
||||||
|
Post.getNeedsGeneration = function (callback) {
|
||||||
|
db.collection("posts").find({$where: "this.lastGenerateDate < this.updatedDate"}).toArray(
|
||||||
|
function (err, docs) {
|
||||||
|
if (err) console.log(err);
|
||||||
|
posts = [];
|
||||||
|
for (i=0; i<docs.length; i++) {
|
||||||
|
posts.push(new Post(docs[i]));
|
||||||
|
}
|
||||||
|
callback(null, posts);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to find posts that need to be uploaded
|
||||||
|
// Inputs: Callback function
|
||||||
|
// Returns: List of Post objects
|
||||||
|
Post.getNeedsUpload = function (callback) {
|
||||||
|
db.collection("posts").find({
|
||||||
|
$where: 'this.lastUploadDate < this.updatedDate',
|
||||||
|
$where: 'this.lastGenerateDate <= this.updatedDate',
|
||||||
|
$where: 'this.published === "True"'
|
||||||
|
}).toArray( function (err, docs) {
|
||||||
|
if (err) console.log(err);
|
||||||
|
posts = [];
|
||||||
|
if (docs) {
|
||||||
|
for (i=0; i<docs.length; i++) {
|
||||||
|
posts.push(new Post(docs[i]));
|
||||||
|
}
|
||||||
|
callback(null, posts);
|
||||||
|
} else {
|
||||||
|
callback("No posts require upload.", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Export the Post object for external use.
|
// Export the Post object for external use.
|
||||||
module.exports = Post;
|
module.exports = Post;
|
||||||
|
|
|
@ -20,6 +20,7 @@ schemas = {
|
||||||
published: false,
|
published: false,
|
||||||
category: null,
|
category: null,
|
||||||
tags: [],
|
tags: [],
|
||||||
|
updateIndexesNeeded: false,
|
||||||
lastGenerateDate: new Date("Mon Jan 1 1900 00:00:00 GMT-0500"),
|
lastGenerateDate: new Date("Mon Jan 1 1900 00:00:00 GMT-0500"),
|
||||||
lastUploadDate: new Date("Mon Jan 1 1900 00:00:00 GMT-0500")
|
lastUploadDate: new Date("Mon Jan 1 1900 00:00:00 GMT-0500")
|
||||||
},
|
},
|
||||||
|
|
8
user.js
8
user.js
|
@ -30,7 +30,7 @@ User.prototype.save = function (callback) {
|
||||||
|
|
||||||
// Function to look up a user document by the users username
|
// Function to look up a user document by the users username
|
||||||
User.getByUsername = function (username, callback) {
|
User.getByUsername = function (username, callback) {
|
||||||
console.log("Getting user document for: "+username);
|
// console.log("Getting user document for: "+username);
|
||||||
db.collection("users").findOne({username:username}, function(err, doc) {
|
db.collection("users").findOne({username:username}, function(err, doc) {
|
||||||
callback(null, new User(doc));
|
callback(null, new User(doc));
|
||||||
});
|
});
|
||||||
|
@ -47,10 +47,10 @@ User.verify = function (username, password, callback) {
|
||||||
callback(err, null);
|
callback(err, null);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log("Username "+username+" exists");
|
// console.log("Username "+username+" exists");
|
||||||
console.log(username + "'s salt is " + doc.salt);
|
// console.log(username + "'s salt is " + doc.salt);
|
||||||
var hash = hashPassword(password, doc.salt);
|
var hash = hashPassword(password, doc.salt);
|
||||||
console.log(username+" is loging in with a hashed password of "+hash);
|
console.log(username+" is logging in with a hashed password of "+hash);
|
||||||
db.collection("users").findOne({username: username, password: hash},
|
db.collection("users").findOne({username: username, password: hash},
|
||||||
function(err, doc) {
|
function(err, doc) {
|
||||||
callback(err, doc);
|
callback(err, doc);
|
||||||
|
|
|
@ -26,7 +26,12 @@ block content
|
||||||
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
||||||
input(type="text", class="form-control", placeholder="Publish Date", name="postDate", value="#{post.get('postDate')}")
|
input(type="text", class="form-control", placeholder="Publish Date", name="postDate", value="#{post.get('postDate')}")
|
||||||
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
||||||
input(type="text", class="form-control", placeholder="Tags", name="tags" value="#{postTags}")
|
input(type="text", class="form-control", placeholder="Tags", name="tags", value="#{postTags}")
|
||||||
|
div(class="row page-header")
|
||||||
|
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
||||||
|
span(class="input-group-addon")
|
||||||
|
input(type="checkbox", name="published", checked=(post.get('published')))
|
||||||
|
input(type="text", class="form-control" placeholder="Published")
|
||||||
div(class="row page-header")
|
div(class="row page-header")
|
||||||
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
||||||
textarea(class="form-control", rows="12", name="markdown")
|
textarea(class="form-control", rows="12", name="markdown")
|
||||||
|
|
|
@ -27,6 +27,11 @@ block content
|
||||||
input(type="text", class="form-control", placeholder="Publish Date", name="postDate")
|
input(type="text", class="form-control", placeholder="Publish Date", name="postDate")
|
||||||
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
||||||
input(type="text", class="form-control", placeholder="Tags", name="tags")
|
input(type="text", class="form-control", placeholder="Tags", name="tags")
|
||||||
|
div(class="row page-header")
|
||||||
|
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
||||||
|
span(class="input-group-addon")
|
||||||
|
input(type="checkbox", name="published", checked=false, value='true')
|
||||||
|
input(type="text", class="form-control" placeholder="Published")
|
||||||
div(class="row page-header")
|
div(class="row page-header")
|
||||||
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
div(class="input-group col-xs-10 col-xs-offset-1 col-md-10 col-md-offset-1")
|
||||||
textarea(class="form-control", rows="12", name="markdown", placeholder="Markdown formatted content")
|
textarea(class="form-control", rows="12", name="markdown", placeholder="Markdown formatted content")
|
||||||
|
|
Loading…
Reference in a new issue