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/index.js

63 lines
1.6 KiB
JavaScript

import Book from '../../data/Book';
import Settings, { SETTINGS_FILE } from '../../data/Settings';
const express = require('express');
const moment = require('moment');
const router = express.Router();
const book = new Book();
const settings = new Settings();
const indexLimit = 5;
//--------------------------
// Index
//--------------------------
router.get('/', function (req, res) {
settings
.load(SETTINGS_FILE)
.then(config => {
book.getPage().then(result => {
result.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id));
let indexPages = [];
let indexCount = 0;
result.forEach(page => {
if (
typeof page.metadata.deleted === 'undefined' ||
page.metadata.deleted === false
) {
if (indexCount === indexLimit) return;
indexPages.push({
page: page,
date: moment(page.metadata.created).fromNow()
});
++indexCount;
}
});
let pageData = [];
if (req.session.user) {
pageData = { title: config.global.title, status: true, pages: indexPages };
} else {
pageData = { title: config.global.title, status: false, pages: indexPages };
}
res.render('index', pageData);
});
})
.catch(err => {
if (err.code === 'ENOENT') {
let setupData = { title: 'Fipamo Set up' };
res.render('init', setupData);
} else {
res.render('error', { error: err });
}
});
});
//--------------------------
// Logout
//--------------------------
router.get('/logout', (req, res) => {
//req.logout();
req.session.destroy();
res.redirect('/@/dashboard');
});
module.exports = router;