You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Fipamo/brain/routes/dash/pages.js

152 lines
4.0 KiB
JavaScript

import Book from '../../data/Book';
const express = require('express');
const router = express.Router();
const moment = require('moment');
const book = new Book();
const uuidv4 = require('uuid/v4');
const fs = require('fs-extra');
//--------------------------
// POSTS
//--------------------------
router.get('/list/:filter?/:page?', function (req, res) {
var pageNum = req.params.page;
var filter = req.params.filter;
if (pageNum == '' || pageNum == null) pageNum = 1;
if (filter == '' || filter == null) filter = 'all';
if (req.session.user) {
book.getPage()
.then(pages => {
pages.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id));
let all = [];
let deleted = [];
let published = [];
let menu = [];
let featured = [];
for (let index = 0; index < pages.length; index++) {
let item = pages[index].metadata;
if (typeof item.deleted === 'undefined' || item.deleted === false) {
all.push(pages[index].metadata);
if (item.published == true) published.push(pages[index].metadata);
if (item.menu == true) menu.push(pages[index].metadata);
if (item.featured == true) featured.push(pages[index].metadata);
} else {
deleted.push(pages[index].metadata);
}
}
var filtered;
switch (filter) {
case 'published':
filtered = published;
break;
case 'deleted':
filtered = deleted;
break;
default:
filtered = all;
break;
}
var count = Math.round(filtered.length / 6);
var pageItems = [];
var itemLimit = 6;
var rangeStart = pageNum * itemLimit - itemLimit;
for (var i = 0; i < itemLimit; i++) {
try {
if (filtered[i + rangeStart].id != null) {
pageItems.push(filtered[i + rangeStart]);
}
} catch (e) {
//console.log("NO POST", e)
}
}
res.render('book-index', {
title: 'Dashbord | Book',
welcome: 'Your pages',
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,
postFilter: filter,
status: true
});
})
.then(() => {
//console.log(value);
})
.catch(err => {
res.render('error', { error: err });
});
} else {
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST ADD DISPLAY
//--------------------------
router.get('/add/new', function (req, res) {
if (req.session.user) {
//need to grab a few copy of settings for the lastest index
fs.readJSON('site/settings.json')
.then(config => {
res.render('page-edit', {
id: config.library_stats.current_index,
uuid: uuidv4(),
title: 'Add New Page',
user_status: true,
welcome: 'Add New Page',
date: moment(Date.now()).format('YYYY MMM DD'),
page: [],
rawDate: moment(Date.now()).format(),
status: ['false', 'false', 'false'],
edit: false
});
})
.catch(err => {
res.render('error', { error: err });
});
} else {
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST EDIT DISPLAY
//--------------------------
router.get('/edit/:id', function (req, res) {
var id = req.params.id;
if (req.session.user) {
book.getPage(id)
.then(page => {
res.render('page-edit', {
id: page.metadata.id,
uuid: page.metadata.uuid,
title: 'Edit Page',
welcome: 'Edit Page',
page: page.metadata,
date: moment(page.metadata.created).format('YYYY MMM DD HH:mm'),
layout: page.metadata.layout,
rawDate: page.metadata.created,
content: page.content,
feature: page.metadata.feature,
status: [
String(page.metadata.menu),
String(page.metadata.featured),
String(page.metadata.published)
],
edit: true
});
})
.catch(err => {
res.render('error', { error: err });
});
} else {
res.redirect('/@/dashboard');
}
});
module.exports = router;