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
|
|
|
|
//--------------------------
|
2019-02-27 17:17:51 +01:00
|
|
|
router.get('/:page?', function(req, res) {
|
|
|
|
var pageNum = req.params.page;
|
|
|
|
if (pageNum == '' || pageNum == null) pageNum = 1;
|
|
|
|
if (req.session.user) {
|
|
|
|
Models.FreshPost.findAll({
|
|
|
|
order: [['id', 'DESC']]
|
|
|
|
})
|
|
|
|
.then(function(posts) {
|
|
|
|
let all = [];
|
|
|
|
let deleted = [];
|
|
|
|
let published = [];
|
|
|
|
let pages = [];
|
|
|
|
let featured = [];
|
|
|
|
for (let index = 0; index < posts.length; index++) {
|
|
|
|
let item = posts[index].post;
|
|
|
|
if (typeof item.deleted == 'undefined' || item.deleted == false) {
|
|
|
|
all.push(posts[index]);
|
|
|
|
if (item.published == 'true') published.push(posts[index]);
|
|
|
|
if (item.page == 'true') pages.push(posts[index]);
|
|
|
|
if (item.featured == 'true') featured.push(posts[index]);
|
|
|
|
} else {
|
|
|
|
deleted.push(posts[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var count = Math.round(all.length / 6);
|
|
|
|
var pageItems = [];
|
|
|
|
var itemLimit = 6;
|
|
|
|
var rangeStart = pageNum * itemLimit - itemLimit;
|
|
|
|
for (var i = 0; i < itemLimit; i++) {
|
|
|
|
try {
|
|
|
|
if (all[i + rangeStart].id != null) {
|
|
|
|
pageItems.push(all[i + rangeStart]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
//console.log("NO POST", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.render('dash/posts-index', {
|
|
|
|
title: 'Dashbord | Posts',
|
|
|
|
mode: 'admin',
|
|
|
|
welcome: 'Your Posts',
|
|
|
|
items: pageItems,
|
|
|
|
page_info: {
|
|
|
|
all: all.length,
|
|
|
|
deleted: deleted.length,
|
|
|
|
published: published.length,
|
|
|
|
pages: pages.length,
|
|
|
|
featured: featured.length
|
|
|
|
},
|
|
|
|
page_index: pageNum,
|
|
|
|
page_count: count,
|
|
|
|
user_status: true
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
//console.log(value);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//next(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
|
|
|
//--------------------------
|
|
|
|
// BLOG POST ADD DISPLAY
|
|
|
|
//--------------------------
|
2019-02-27 17:17:51 +01:00
|
|
|
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');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
|
|
|
//--------------------------
|
|
|
|
// BLOG POST EDIT DISPLAY
|
|
|
|
//--------------------------
|
2019-02-27 17:17:51 +01:00
|
|
|
router.get('/edit/:id', function(req, res) {
|
|
|
|
if (req.session.user) {
|
|
|
|
Models.FreshPost.findOne({
|
|
|
|
where: {
|
|
|
|
post: {
|
|
|
|
[Op.contains]: {
|
|
|
|
uuid: req.params.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(item => {
|
|
|
|
let featured = 'null';
|
|
|
|
if (item.post.feature != null || item.post.feature != '')
|
|
|
|
featured = item.post.feature.substr(8, item.post.feature.length);
|
|
|
|
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,
|
|
|
|
title: 'Edit Post',
|
|
|
|
mode: 'admin',
|
|
|
|
post: item.post,
|
|
|
|
date: sexydate,
|
|
|
|
colored: pretty,
|
|
|
|
html: item.post.plaintext,
|
|
|
|
feature: featured,
|
|
|
|
status: [
|
|
|
|
String(item.post.page),
|
|
|
|
String(item.post.featured),
|
|
|
|
String(item.post.published)
|
|
|
|
],
|
|
|
|
edit: true
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
//console.log("VALUE: " + value);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.log(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
});
|
2019-02-27 17:17:51 +01:00
|
|
|
module.exports = router;
|