fipamo/brain/routes/back/dash_posts.js

116 lines
3.8 KiB
JavaScript
Raw Normal View History

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;
console.log(pageNum);
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 {
console.log("REDIRECT")
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;