diff --git a/app.js b/app.js index f1d1272..aea2847 100644 --- a/app.js +++ b/app.js @@ -7,15 +7,17 @@ var async = require('async'); var multer = require('multer'); var fs = require('fs'); var markdown = require( "markdown" ).markdown; +var moment = require("moment"); + // Get connected to our database -var MongoClient = require('mongodb').MongoClient +var MongoClient = require('mongodb').MongoClient; Post = require('./post.js'); User = require('./user.js'); Category = require('./category.js'); +Static = require('./static.js'); var database = require('./db.js'); -var genStatic = require('./genStatic.js'); var genPhotos = require('./genPhotos.js'); var app = express(); @@ -92,6 +94,7 @@ 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; @@ -103,9 +106,10 @@ app.get('/admin/post/list/:start?', 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) { @@ -125,6 +129,7 @@ app.get('/admin/post/view/:uuid?', } ); +// GET /admin/post/new app.get('/admin/post/new', function(req, res, next) { Category.getCategories(null, null, function (err, categories) { @@ -133,6 +138,7 @@ app.get('/admin/post/new', } ); +// POST /admin/post/new app.post('/admin/post/new', function(req, res, next) { var post = new Post(); @@ -151,7 +157,7 @@ app.post('/admin/post/new', Category.getByName(req.body.category, function (err, category) { post.set("category", category.get("uuid")); - console.log(post); + //console.log(post); post.save(function (err) { if (!err) { req.flash('successNotice', 'Post created.'); @@ -166,6 +172,7 @@ app.post('/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) { @@ -190,6 +197,7 @@ app.get('/admin/post/edit/:uuid', } ); +// POST /admin/post/edit app.post('/admin/post/edit/:uuid', function(req, res, next) { Post.getByUUID(req.params.uuid, function(err, post) { @@ -215,26 +223,31 @@ app.post('/admin/post/edit/:uuid', } ); -app.get('/admin/post/regenerate/:id?', +// GET /admin/post/regenerate +app.get('/admin/post/regenerate/:uuid?', function(req, res, next) { - if (req.params.id) { - genStatic.generateStatic(function(err){ if (err) console.log(err) }); - genPosts.generatePost(req.params.id, function(err) { - if (!err) { - req.flash('successNotice', 'Post regenerated successfully.'); - res.redirect('/admin/post/view/'+req.params.id); - } - else { - req.flash('failureNotice', 'Post regeneration failed, check logs.'); - res.redirect('/admin/post/view/'+req.params.id); - } + 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 @@ -402,60 +415,26 @@ app.get('/admin/photo/regenerate/:id', // Admin dashboard page. app.get('/admin', function(req, res, next) { - async.parallel({ - categories: function(callback) { - database.countCategories(function(count) { - callback(null, count); - }); - }, - posts: function(callback) { - Post.countPosts(function(err, count) { - callback(null, count); - }); - }, - galleries: function(callback) { - database.countGalleries(function(count) { - callback(null, count); - }); - }, - photos: function(callback) { - database.countPhotos(function(count) { - callback(null, count); - }); - }, - tags: function(callback) { - database.countTags(function(count) { - callback(null, count); - }); - }, - regenerateDate: function(callback) { - database.getLastRegenerateDate(function(date) { - var dateString = helper.epochToDateString(date.date).slice(0,-12); - callback(null, dateString); - }); - }, - uploadDate: function(callback) { - database.getLastUploadDate(function(date) { - var dateString = helper.epochToDateString(date.date).slice(0,-12); - callback(null, dateString); - }); - } - }, - function(err, results) { - res.render('admin-dashboard', { - successNotice: req.flash('successNotice'), - failureNotice: req.flash('failureNotice'), - categories: results.categories, - posts: results.posts, - galleries: results.galleries, - photos: results.photos, - tags: results.tags, - regenerateDate: results.regenerateDate, - uploadDate: results.uploadDate, - user: req.user}); - } - ); -} + Post.countPosts(function (err, postCount) { + Category.countCategories(function (err, categoryCount) { + Post.getLastGenerateDate(function (err, lastGenerateDate) { + Post.getLastUploadDate( function (err, lastUploadDate) { + res.render('admin-dashboard', { + successNotice: req.flash('successNotice'), + failureNotice: req.flash('failureNotice'), + categories: categoryCount, + posts: postCount, + galleries: "###", + photos: "###", + tags: "###", + regenerateDate: moment(lastGenerateDate).format("YYYY-MM-DD HH:mm"), + uploadDate: moment(lastUploadDate).format("YYYY-MM-DD HH:mm"), + user: req.user}); + }) + }) + }) + }) + } ); // Routes for previewing sent versions of the built items. diff --git a/category.js b/category.js new file mode 100644 index 0000000..79e866e --- /dev/null +++ b/category.js @@ -0,0 +1,96 @@ +var moment = require("moment"); +var _ = require("lodash"); +var schemas = require('./schemas.js'); + + +var Category = function(data) { + this.data = data; +} + +Category.prototype.data = {} + +Category.prototype.get = function (name) { + return this.data[name]; +} + +Category.prototype.set = function (name, value) { + this.data[name] = value; +} + +Category.prototype.sanitize = function (data) { + data = data || {}; + schema = schemas.category; + return _.pick(_.defaults(data, schema), _.keys(schema)); +} + +Category.prototype.save = function (callback) { + var self = this; + this.data = this.sanitize(this.data); + db.collection("categories").save(this.data, callback(err)); +} + +// Function to get a YYYY-MM-DD format of the current CategoryDate +// Requires no arguments. +Category.prototype.getShortDate = function () { + return moment(this.get("createdDate")).format("YYYY-MM-DD"); +} + +// Get a specific category by its name +// Returns a Category object +Category.getByName = function (name, callback) { + db.collection("categories").findOne({name: name}, function(err, doc) { + callback(err, new Category(doc)); + }); +} + +// Get a specific category by its UUID +// Returns a Category object +Category.getByUUID = function (uuid, callback) { + db.collection("categories").findOne({uuid: uuid}, function(err, doc) { + callback(err, new Category(doc)); + }); +} + +// Function to get a count of current Categories +// Returns count of Categories +Category.countCategories = function(callback) { + db.collection("categories").find({}).count(function (err, count) { + if (err) console.log(err); + callback(err, count); + }); +} + +// Function to get a list of all existing categories +// Start and count are optional, but if start is used, count must be used as well. +// Returns an array of Category objects +Category.getCategories = function(count, start, callback) { + if (typeof callback === undefined) { + if (typeof start === undefined) { + callback = count; + count = undefined; + start = undefined; + } + else { + callback = start; + start = undefined; + } + } + + var options = {} + if (start) options.skip=start; + if (count) options.limit=count; + options.sort = {name: 1}; + + db.collection("categories").find({}, options).toArray(function(err, docs) { + if (err) console.log(err); + var rows = []; + for (i=0; i