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-11-11 21:22:01 +01:00
|
|
|
var count = Math.round(posts.length / 6);
|
|
|
|
//console.log("COUNT: "+count);
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (posts[i + rangeStart].id != null)
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
pageItems.push(posts[i + rangeStart]);
|
|
|
|
}
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
//console.log(e)
|
|
|
|
}
|
|
|
|
}
|
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-21 01:42:52 +01:00
|
|
|
console.log("REDIRECT")
|
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]:
|
|
|
|
{
|
|
|
|
slug: req.params.id
|
|
|
|
}
|
|
|
|
}
|
2018-11-11 21:22:01 +01:00
|
|
|
}
|
2018-11-22 02:08:53 +01:00
|
|
|
}).then(item =>
|
|
|
|
{
|
|
|
|
let featured_img = JSON.parse(item.post.feature);
|
2018-11-11 21:22:01 +01:00
|
|
|
let featured = 'null';
|
2018-11-22 02:08:53 +01:00
|
|
|
if (featured_img.length != 0) featured = featured_img[0].substr(7, featured_img[0].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,
|
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-11-11 21:22:01 +01:00
|
|
|
console.log("VALUE: " + value);
|
2018-11-22 02:08:53 +01:00
|
|
|
}).catch(function(err)
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
console.log(err);
|
|
|
|
})
|
2018-11-22 02:08:53 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-11 21:22:01 +01:00
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = router;
|