From e2b3a6e838ebf4b067bc510ed344995df58c7db2 Mon Sep 17 00:00:00 2001 From: Ro Date: Sun, 11 Nov 2018 15:22:01 -0500 Subject: [PATCH] seperated routes to cut down on bloat and make a clear structure for additional sections --- brain/app.js | 65 ++++++-------- brain/routes/back/dash_posts.js | 114 +++++++++++++++++++++++++ brain/routes/back/index.js | 144 +++++++++++++++++++------------- 3 files changed, 225 insertions(+), 98 deletions(-) create mode 100644 brain/routes/back/dash_posts.js diff --git a/brain/app.js b/brain/app.js index 6a3a640..7538709 100644 --- a/brain/app.js +++ b/brain/app.js @@ -9,99 +9,86 @@ var MemoryStore = require('memorystore')(session) var flash = require('connect-flash'); var theme = "default"; var app = express(); - // view engine setup app.set('views', path.join(__dirname, '../themes')); app.set('view engine', 'pug'); -//app.use(express.static(__dirname+'/content/data')); app.use(express.static(__dirname + '../content/folio-images')); - -// uncomment after placing your favicon in /public -//app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); -app.use(bodyParser.urlencoded({ +app.use(bodyParser.urlencoded( +{ extended: false })); app.use(cookieParser()); -//app.use(require('node-compass')({mode: 'expanded', css:'styles', sass: 'styles', project: path.join(__dirname, 'src')})); app.use(express.static(path.join(__dirname, '../content'))); app.use(express.static(path.join(__dirname, '../themes'))); - -// Why Do we need this key ? -/** -app.use(session({ - secret: '1KqZ18W8KskE1iSw', - saveUninitialized: false, - resave: false, - cookie:{maxAge:608800000} -})); -**/ - -app.use(session({ - store: new MemoryStore({ +app.use(session( +{ + store: new MemoryStore( + { checkPeriod: 86400000 // prune expired entries every 24h }), secret: '1KqZ18W8KskE1iSw', saveUninitialized: false, resave: false, - cookie: { maxAge: 608800000 } + cookie: + { + maxAge: 608800000 + } })) - - app.use(flash()); - //sections var front = require('./routes/front/index')(session); var back = require('./routes/back/index'); - +var postDashboard = require('./routes/back/dash_posts'); //api var folioLibrary = require('./api/content/folio'); var projectLibrary = require('./api/content/project'); var bookmarkLibrary = require('./api/content/bookmarks'); var postLibrary = require('./api/content/posts'); var mailer = require('./api/content/mailer'); - // API PATHS app.use('/api/folio', folioLibrary); app.use('/api/projects', projectLibrary); app.use('/api/bookmarks', bookmarkLibrary); app.use('/api/post', postLibrary); - // PAGES app.use('/', front); app.use('/@/dashboard', back); +app.use('/@/dashboard/posts', postDashboard); //app.use('/mailer', mailer); - // catch 404 and forward to error handler -app.use(function(req, res, next) { +app.use(function(req, res, next) +{ var err = new Error('Not Found'); err.status = 404; next(err); }); - // error handlers - // development error handler // will print stacktrace -if (app.get('env') === 'development') { - app.use(function(err, req, res, next) { +if (app.get('env') === 'development') +{ + app.use(function(err, req, res, next) + { res.status(err.status || 500); - res.render(theme+'/error', { + res.render(theme + '/error', + { message: err.message, error: err }); }); } - // production error handler // no stacktraces leaked to user -app.use(function(err, req, res, next) { +app.use(function(err, req, res, next) +{ res.status(err.status || 500); - res.render(theme+'/error', { + res.render(theme + '/error', + { message: err.message, - error: {} + error: + {} }); }); - module.exports = app; \ No newline at end of file diff --git a/brain/routes/back/dash_posts.js b/brain/routes/back/dash_posts.js new file mode 100644 index 0000000..782ad81 --- /dev/null +++ b/brain/routes/back/dash_posts.js @@ -0,0 +1,114 @@ + +import DateUtils from '../../tools/utilities/DateUtils'; +var express = require('express'); +var router = express.Router(); +var Models = require('../../models'); +var hljs = require('highlight.js/lib/highlight'); +var hljs_md = require('highlight.js/lib/languages/markdown'); +hljs.registerLanguage('markdown', hljs_md); + +const dateUtils = new DateUtils(); + +//-------------------------- +// POSTS +//-------------------------- +router.get('/:page?', function (req, res) { + var pageNum = req.params.page; + if (pageNum == "" || pageNum == null) pageNum = 1; + var offset = ((pageNum * 5) - 5); + if (req.session.user) { + Models.Post.findAll({ + order: [ + ['id', 'DESC'] + ] + }).then(function (posts) { + var count = Math.round(posts.length / 6); + //console.log("COUNT: "+count); + var pageItems = []; + var itemLimit = 6; + var rangeStart = (pageNum * itemLimit) - itemLimit; + for (var i = 0; i < itemLimit; i++) { + try { + if (posts[i + rangeStart].id != null) { + pageItems.push(posts[i + rangeStart]); + } + } catch (e) { + //console.log(e) + } + } + res.render('dash/posts-index', { + title: 'Dashbord | Posts', + mode: 'admin', + items: pageItems, + page_index: pageNum, + page_count: count + }); + }).then(function (value) { + //console.log(value); + }).catch(function (err) { + //next(err); + }) + } else { + res.redirect('/@/dashboard'); + } +}); + + +//-------------------------- +// BLOG POST ADD DISPLAY +//-------------------------- +router.get('/add/new', function (req, res) { + if (req.session.user) { + res.render('dash/post-edit', { + title: 'Make New Post', + mode: 'admin', + date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()), + status:['false', 'false', 'false'], + edit: false + }); + } else { + res.redirect('/@/dashboard'); + } +}); +//-------------------------- +// BLOG POST EDIT DISPLAY +//-------------------------- +router.get('/edit/:id', function (req, res) { + if (req.session.user) { + Models.Post.findOne({ + where: { + slug: req.params.id + } + }).then(post => { + let featured_img = JSON.parse(post.feature_image); + let featured = 'null'; + if (featured_img.length != 0) + featured = featured_img[0].substr(7, featured_img[0].length); + let pretty = hljs.highlight('markdown', post.plaintext).value; + + let sexydate + if (post.origin_date == "" || post.origin_date == null) + sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at) + else + sexydate = post.origin_date + res.render('dash/post-edit', { + title: 'Edit Post', + mode: 'admin', + post: post, + date: sexydate, + colored: pretty, + html: post.plaintext, + feature: featured, + status:[String(post.page), String(post.featured), String(post.published)], + edit: true + }); + }).then(function (value) { + console.log("VALUE: " + value); + }).catch(function (err) { + console.log(err); + }) + } else { + res.redirect('/@/dashboard'); + } +}); +module.exports = router; \ No newline at end of file diff --git a/brain/routes/back/index.js b/brain/routes/back/index.js index 3cef99b..856e6f0 100644 --- a/brain/routes/back/index.js +++ b/brain/routes/back/index.js @@ -1,4 +1,3 @@ - import DateUtils from '../../tools/utilities/DateUtils'; var express = require('express'); var router = express.Router(); @@ -6,139 +5,162 @@ var Models = require('../../models'); var hljs = require('highlight.js/lib/highlight'); var hljs_md = require('highlight.js/lib/languages/markdown'); hljs.registerLanguage('markdown', hljs_md); - const dateUtils = new DateUtils(); - //-------------------------- // Index //-------------------------- -router.get('/', function (req, res) { +router.get('/', function(req, res) +{ var loggedIn = false - if (req.session.user) - loggedIn = true; - - Models.Post.sync().then(f => { - Models.Post.findAll({ + if (req.session.user) loggedIn = true; + Models.Post.sync().then(f => + { + Models.Post.findAll( + { order: [ ['id', 'DESC'] ], limit: 10 - }).then(function (posts) { - res.render('dash/index', { + }).then(function(posts) + { + res.render('dash/index', + { title: 'Dashboard', user_status: loggedIn, items: posts }); - }).then(function (value) { + }).then(function(value) + { //console.log(value); - }).catch(function (err) { + }).catch(function(err) + { //next(err); }) - }).catch(err => { + }).catch(err => + { console.log("ERR: " + err); }) - - }); - //-------------------------- // SETTINGS //-------------------------- -router.get('/settings/', function (req, res) { - if (req.session.user) { - res.render('dash/settings', { +router.get('/settings/', function(req, res) +{ + if (req.session.user) + { + res.render('dash/settings', + { title: 'Dashboard | Settings', mode: 'admin' }); - } else { + } + else + { res.redirect('/@/dashboard'); } }); - - //-------------------------- // POSTS //-------------------------- -router.get('/posts/:page?', function (req, res) { +router.get('/posts/:page?', function(req, res) +{ var pageNum = req.params.page; if (pageNum == "" || pageNum == null) pageNum = 1; var offset = ((pageNum * 5) - 5); - if (req.session.user) { - Models.Post.findAll({ + if (req.session.user) + { + Models.Post.findAll( + { order: [ ['id', 'DESC'] ] - }).then(function (posts) { + }).then(function(posts) + { var count = Math.round(posts.length / 6); //console.log("COUNT: "+count); var pageItems = []; var itemLimit = 6; var rangeStart = (pageNum * itemLimit) - itemLimit; - for (var i = 0; i < itemLimit; i++) { - try { - if (posts[i + rangeStart].id != null) { + for (var i = 0; i < itemLimit; i++) + { + try + { + if (posts[i + rangeStart].id != null) + { pageItems.push(posts[i + rangeStart]); } - } catch (e) { + } + catch (e) + { //console.log(e) } } - res.render('dash/posts-index', { + res.render('dash/posts-index', + { title: 'Dashbord | Posts', mode: 'admin', items: pageItems, page_index: pageNum, page_count: count }); - }).then(function (value) { + }).then(function(value) + { //console.log(value); - }).catch(function (err) { + }).catch(function(err) + { //next(err); }) - } else { + } + else + { res.redirect('/@/dashboard'); } }); - - //-------------------------- // BLOG POST ADD DISPLAY //-------------------------- -router.get('/posts/add/new', function (req, res) { - if (req.session.user) { - res.render('dash/post-edit', { +router.get('/posts/add/new', function(req, res) +{ + if (req.session.user) + { + res.render('dash/post-edit', + { title: 'Make New Post', mode: 'admin', date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()), - status:['false', 'false', 'false'], + status: ['false', 'false', 'false'], edit: false }); - } else { + } + else + { res.redirect('/@/dashboard'); } }); //-------------------------- // BLOG POST EDIT DISPLAY //-------------------------- -router.get('/posts/edit/:id', function (req, res) { - if (req.session.user) { - Models.Post.findOne({ - where: { +router.get('/posts/edit/:id', function(req, res) +{ + if (req.session.user) + { + Models.Post.findOne( + { + where: + { slug: req.params.id } - }).then(post => { + }).then(post => + { let featured_img = JSON.parse(post.feature_image); let featured = 'null'; - if (featured_img.length != 0) - featured = featured_img[0].substr(7, featured_img[0].length); + if (featured_img.length != 0) featured = featured_img[0].substr(7, featured_img[0].length); let pretty = hljs.highlight('markdown', post.plaintext).value; - let sexydate - if (post.origin_date == "" || post.origin_date == null) - sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at) - else - sexydate = post.origin_date - res.render('dash/post-edit', { + if (post.origin_date == "" || post.origin_date == null) sexydate = dateUtils.getDate('year', post.created_at) + "-" + dateUtils.getDate('month', post.created_at) + "-" + dateUtils.getDate('day', post.created_at) + else sexydate = post.origin_date + res.render('dash/post-edit', + { title: 'Edit Post', mode: 'admin', post: post, @@ -146,15 +168,19 @@ router.get('/posts/edit/:id', function (req, res) { colored: pretty, html: post.plaintext, feature: featured, - status:[String(post.page), String(post.featured), String(post.published)], + status: [String(post.page), String(post.featured), String(post.published)], edit: true }); - }).then(function (value) { + }).then(function(value) + { console.log("VALUE: " + value); - }).catch(function (err) { + }).catch(function(err) + { console.log(err); }) - } else { + } + else + { res.redirect('/@/dashboard'); } });