fipamo/brain/routes/back/index.js

339 lines
9.6 KiB
JavaScript
Raw Normal View History

2018-10-31 17:00:31 +01:00
var express = require('express');
var router = express.Router();
var Models = require('../../models');
var DateUtils = require('../../utils/DateUtils');
var hljs = require('highlight.js/lib/highlight');
var hljs_md = require('highlight.js/lib/languages/markdown');
hljs.registerLanguage('markdown', hljs_md);
//--------------------------
// Index
//--------------------------
router.get('/', function (req, res) {
var loggedIn = false
if (req.session.user)
loggedIn = true;
Models.Post.findAll({
order: [
['id', 'DESC']
],
limit: 10
}).then(function (posts) {
res.render('dash/index', {
title: 'Dashboard',
user_status: loggedIn,
items: posts
});
}).then(function (value) {
//console.log(value);
}).catch(function (err) {
//next(err);
})
});
//--------------------------
// SETTINGS
//--------------------------
router.get('/settings/', function (req, res) {
if (req.session.user) {
res.render('dash/settings', {
title: 'Dashboard | Settings',
mode: 'admin'
});
} else {
res.redirect('/@/dashboard');
}
});
//--------------------------
// ENTRIES
//--------------------------
router.get('/entries/:page?', function (req, res) {
var pageNum = req.params.page;
if(pageNum == "" || pageNum == null) pageNum = 1;
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/entries-index', {
title: 'Dashbord Entries',
mode: 'admin',
items: pageItems,
page_index: pageNum,
page_count: count
});
}).then(function (value) {
//console.log(value);
}).catch(function (err) {
//next(err);
})
} else {
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST ADD DISPLAY
//--------------------------
router.get('/entries/add/', function (req, res) {
if (req.session.user) {
res.render('dash/entry-edit', {
title: 'Add Entry',
mode: 'admin',
edit: false
});
} else {
res.redirect('/@/dashboard');
}
});
//--------------------------
// BLOG POST EDIT DISPLAY
//--------------------------
router.get('/entries/edit/:id', function (req, res) {
if (req.session.user) {
Models.Post.findOne({
where: {
slug: req.params.id
}
}).then(entry => {
let featured_img = JSON.parse(entry.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', entry.entry_plaintext).value;
2018-11-02 21:25:47 +01:00
//let newdate = new Date(entry.created_at);
//let formattedDate = newdate.getFullYear()+"-"+newdate.getMonth()+"-"+newdate.getDate();
let sexydate = DateUtils.getDate('stamp', entry.origin_date)
2018-10-31 17:00:31 +01:00
res.render('dash/entry-edit', {
title: 'Edit Entry',
mode: 'admin',
post: entry,
date:entry.origin_date,
2018-10-31 17:00:31 +01:00
colored: pretty,
html: entry.entry_plaintext,
feature: featured,
edit: true
});
}).then(function (value) {
console.log("VALUE: " + value);
}).catch(function (err) {
console.log(err);
})
} else {
res.redirect('/@/dashboard');
}
});
/**
//--------------------------
// MAIN FIPAMO DISPLAY
//--------------------------
router.get('/fipamo/', function (req, res) {
if (req.session.user) {
Models.Bookmark.findAll({
order: [['id', 'DESC']]
}).then(function (saved) {
res.render('admin/admin-fipamo-index', {
title: 'Manage Saved',
mode: 'admin',
saved: saved
});
}).then(function (value) {
//console.log(value);
}).catch(function (err) {
//next(err);
})
} else {
res.redirect('/admin');
}
});
router.get('/fipamo/edit/:id', function (req, res) {
if (req.session.user) {
Models.Bookmark.findOne({
where: {
id: req.params.id
}
}).then(saved => {
res.render('admin/admin-fipamo-edit', {
title: 'FIPAMO | EDIT ' + saved.title,
mode: 'admin',
bookmark: saved,
edit: true
});
}).then(function (value) {
console.log("VALUE: " + value);
}).catch(function (err) {
console.log(err);
})
} else {
res.redirect('/admin');
}
});
//--------------------------
// MAIN FOLIO DISPLAY
//--------------------------
router.get('/folio/', function (req, res) {
if (req.session.user) {
Models.FolioProject.findAll().then(function (projects) {
res.render('folio-hub', {
title: 'manage folio',
mode: 'admin',
projects: projects
});
}).then(function (value) {
//console.log(value);
}).catch(function (err) {
//next(err);
})
} else {
res.redirect('/admin');
}
});
//--------------------------
// PROJECT DISPLAY
//--------------------------
router.get('/folio/:id', function (req, res) {
if (req.session.user) {
console.log(req.params.id)
Models.FolioProject.findOne({
where: {
slug: req.params.id
}
}).then(function (project) {
//var item = project[0]
res.render('folio-project-display', {
title: project.title,
project: project,
edit: true,
mode: 'admin'
});
}).then(function (value) {
//console.log(value);
}).catch(function (err) {
//next(err);
});
} else {
res.redirect('/admin');
}
});
router.get('/folio/task/add', function (req, res) {
if (req.user) {
res.render('folio-project-display', {
title: 'Add New Project',
edit: false,
mode: 'admin'
});
} else {
res.redirect('/admin');
}
});
//--------------------------
// ADMIN PAGE
//--------------------------
router.get('/admin/:include/:id?', function (req, res) {
if (req.user) {
if (req.user.role == 2) {
switch (req.params.include) {
case "edit-project":
FolioProject.findById(req.params.id).exec().then(function (project) {
res.render('includes/folio-project', {
formTitle: "EDIT " + project.title,
project: project,
mode: req.params.include
});
}).catch(function (err) {
//console.log(err)
});
break
case "add-project":
res.render('includes/folio-project', {
formTitle: 'Fo r mle ss ADMIN | Add New Project',
mode: req.params.include
});
break
case "folio-hub":
FolioProject.find().exec().then(function (entries) {
//res.json(entries);
res.render('content/folio-hub', {
title: 'Fo r mle ss ADMIN | Folio Manager',
entries: entries
});
}).then(function (value) {
//console.log(value);
}).catch(function (err) {
next(err);
});
break
}
}
} else {
res.json({
message: 'NOT AUTHORIZED'
});
}
});
router.get('/includes/admin-menu/', function (req, res) {
if (req.user) {
if (req.user == 1) {
res.render('client-panel')
} else {
res.render('includes/admin-menu', {
title: 'Fo r mle ss | Admin',
user_status: "What up, random entity",
name: "What up, " + req.user.firstname
})
}
} else {
res.render('index', {
title: 'Fo r mle ss',
user_status: "What up, random entity"
});
}
});
router.get('/content/admin/', function (req, res) {
if (req.user) {
if (req.user == 1) {
res.render('client-panel')
} else {
res.render('content/admin', {
title: 'Fo r mle ss | Admin'
})
}
} else {
res.render('content/index', {
title: 'Fo r mle ss'
});
}
});
*/
module.exports = router;