2018-11-11 21:22:01 +01:00
|
|
|
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);
|
2018-11-22 02:08:53 +01:00
|
|
|
var Sequelize = require('sequelize');
|
|
|
|
const Op = Sequelize.Op;
|
2018-11-11 21:22:01 +01:00
|
|
|
const dateUtils = new DateUtils();
|
|
|
|
//--------------------------
|
|
|
|
// POSTS
|
|
|
|
//--------------------------
|
2018-11-22 02:08:53 +01:00
|
|
|
router.get('/:page?', function(req, res)
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
var pageNum = req.params.page;
|
|
|
|
if (pageNum == "" || pageNum == null) pageNum = 1;
|
|
|
|
var offset = ((pageNum * 5) - 5);
|
2018-11-22 02:08:53 +01:00
|
|
|
if (req.session.user)
|
|
|
|
{
|
|
|
|
Models.FreshPost.findAll(
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
order: [
|
|
|
|
['id', 'DESC']
|
|
|
|
]
|
2018-11-22 02:08:53 +01:00
|
|
|
}).then(function(posts)
|
|
|
|
{
|
2018-12-04 03:19:10 +01:00
|
|
|
let filtered = [];
|
|
|
|
for (let index = 0; index < posts.length; index++)
|
|
|
|
{
|
|
|
|
let item = posts[index].post;
|
|
|
|
if (typeof item.deleted == 'undefined' || item.deleted == false)
|
|
|
|
{
|
|
|
|
filtered.push(posts[index])
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var count = Math.round(filtered.length / 6);
|
2018-11-11 21:22:01 +01:00
|
|
|
var pageItems = [];
|
|
|
|
var itemLimit = 6;
|
|
|
|
var rangeStart = (pageNum * itemLimit) - itemLimit;
|
2018-11-22 02:08:53 +01:00
|
|
|
for (var i = 0; i < itemLimit; i++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-12-08 19:21:48 +01:00
|
|
|
if (filtered[i + rangeStart].id != null)
|
2018-11-22 02:08:53 +01:00
|
|
|
{
|
2018-12-04 03:19:10 +01:00
|
|
|
pageItems.push(filtered[i + rangeStart]);
|
2018-11-11 21:22:01 +01:00
|
|
|
}
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2018-12-08 19:21:48 +01:00
|
|
|
//console.log("NO POST", e)
|
2018-11-11 21:22:01 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-22 02:08:53 +01:00
|
|
|
res.render('dash/posts-index',
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
title: 'Dashbord | Posts',
|
|
|
|
mode: 'admin',
|
|
|
|
items: pageItems,
|
|
|
|
page_index: pageNum,
|
|
|
|
page_count: count
|
|
|
|
});
|
2018-11-22 02:08:53 +01:00
|
|
|
}).then(function(value)
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
//console.log(value);
|
2018-11-22 02:08:53 +01:00
|
|
|
}).catch(function(err)
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
//next(err);
|
|
|
|
})
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//--------------------------
|
|
|
|
// BLOG POST ADD DISPLAY
|
|
|
|
//--------------------------
|
2018-11-22 02:08:53 +01:00
|
|
|
router.get('/add/new', function(req, res)
|
|
|
|
{
|
|
|
|
if (req.session.user)
|
|
|
|
{
|
|
|
|
res.render('dash/post-edit',
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
title: 'Make New Post',
|
|
|
|
mode: 'admin',
|
|
|
|
date: dateUtils.getDate('year', new Date()) + "-" + dateUtils.getDate('month', new Date()) + "-" + dateUtils.getDate('day', new Date()),
|
2018-11-22 02:08:53 +01:00
|
|
|
status: ['false', 'false', 'false'],
|
2018-11-11 21:22:01 +01:00
|
|
|
edit: false
|
|
|
|
});
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//--------------------------
|
|
|
|
// BLOG POST EDIT DISPLAY
|
|
|
|
//--------------------------
|
2018-11-22 02:08:53 +01:00
|
|
|
router.get('/edit/:id', function(req, res)
|
|
|
|
{
|
|
|
|
if (req.session.user)
|
|
|
|
{
|
|
|
|
Models.FreshPost.findOne(
|
|
|
|
{
|
|
|
|
where:
|
|
|
|
{
|
|
|
|
"post":
|
|
|
|
{
|
|
|
|
[Op.contains]:
|
|
|
|
{
|
2018-12-06 22:49:49 +01:00
|
|
|
uuid: req.params.id
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
}
|
2018-11-22 02:08:53 +01:00
|
|
|
}).then(item =>
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
let featured = 'null';
|
2018-12-04 03:19:10 +01:00
|
|
|
if (item.post.feature != null || item.post.feature != '') featured = item.post.feature.substr(8, item.post.feature.length);
|
2018-11-22 02:08:53 +01:00
|
|
|
let pretty = hljs.highlight('markdown', item.post.plaintext).value;
|
|
|
|
let sexydate = dateUtils.getDate('year', item.post.created) + "-" + dateUtils.getDate('month', item.post.created) + "-" + dateUtils.getDate('day', item.post.created)
|
|
|
|
res.render('dash/post-edit',
|
|
|
|
{
|
|
|
|
id: item.id,
|
2018-11-11 21:22:01 +01:00
|
|
|
title: 'Edit Post',
|
|
|
|
mode: 'admin',
|
2018-11-22 02:08:53 +01:00
|
|
|
post: item.post,
|
2018-11-11 21:22:01 +01:00
|
|
|
date: sexydate,
|
|
|
|
colored: pretty,
|
2018-11-22 02:08:53 +01:00
|
|
|
html: item.post.plaintext,
|
2018-11-11 21:22:01 +01:00
|
|
|
feature: featured,
|
2018-11-22 02:08:53 +01:00
|
|
|
status: [String(item.post.page), String(item.post.featured), String(item.post.published)],
|
2018-11-11 21:22:01 +01:00
|
|
|
edit: true
|
|
|
|
});
|
2018-11-22 02:08:53 +01:00
|
|
|
}).then(function(value)
|
|
|
|
{
|
2018-12-08 19:21:48 +01:00
|
|
|
//console.log("VALUE: " + value);
|
2018-11-22 02:08:53 +01:00
|
|
|
}).catch(function(err)
|
|
|
|
{
|
2018-12-08 19:21:48 +01:00
|
|
|
//console.log(err);
|
2018-11-11 21:22:01 +01:00
|
|
|
})
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = router;
|