”2016-04-12”

This commit is contained in:
Martin Donnelly 2016-04-12 16:45:03 +01:00
parent 4567ad83fd
commit 9e3e21addf
3 changed files with 102 additions and 25 deletions

View File

@ -0,0 +1,11 @@
{
"apps" : [{
"name" : "OBrand-Server",
"watch" : true,
"script" : "obrand-server.js",
"instances" : 1,
"exec_mode" : "cluster",
"combine_logs" : true,
"ignore_watch" : ["[\\/\\\\]\\./", "node_modules", "server/static"]
}]
}

View File

@ -6,6 +6,7 @@ var $U = require('md-utils');
var Events = require('events');
var jade = require('pug');
var fs = require('fs');
var logger = require('log4js').getLogger();
var eventHandler = new Events();
@ -17,7 +18,6 @@ var templates = {
var exportPath = __dirname + '/../static/';
var doRenderPage = (page) => {
var compiledhtml;
@ -46,33 +46,31 @@ module.exports = function(app) {
pagesRouter.get('/', function(req, res) {
dbPages.getFullPageList()
.then(function(data) {
const response = {
data: data
};
.then(function(data) {
const response = {
data: data
};
res.status(200).send(response);
res.status(200).send(response);
}).catch((err)=> {
console.error(err);
res.status(401).end();
});
}).catch((err)=> {
console.error(err);
res.status(401).end();
});
});
pagesRouter.get('/list', function(req, res) {
dbPages.getPageList()
.then(function(data) {
res.render('pages', {data: data});
.then(function(data) {
res.render('pages', {data: data});
}).catch((err)=> {
console.error(err);
res.status(401).end();
});
}).catch((err)=> {
console.error(err);
res.status(401).end();
});
});
pagesRouter.post('/', function(req, res) {
dbPages.addNewPage(req.body.data)
.then(function(data) {
@ -91,8 +89,7 @@ module.exports = function(app) {
});
});
pagesRouter.patch('/', function(req, res) {
pagesRouter.patch('/:id', function(req, res) {
dbPages.addNewPage(req.body.data)
.then(function(data) {
@ -109,12 +106,41 @@ module.exports = function(app) {
});
});
pagesRouter.get('/:id', function(req, res) {
res.send({
pages: {
id: req.params.id
}
pagesRouter.delete('/:id', function(req, res) {
logger.debug(req.params.id);
dbPages.deletePage(req.params.id)
.then(function(data) {
let response = {
data: data
};
res.status(200).send(response);
eventHandler.emit('renderPage', data.attributes);
})
.catch((err)=> {
console.error(err);
res.status(401).end();
});
});
pagesRouter.get('/:id', function(req, res) {
logger.debug('Page to edit:', req.params.id);
dbPages.getPage(req.params.id)
.then(function(data) {
let response = {
data: data
};
logger.debug(response);
res.status(200).send(response);
})
.catch((err)=> {
console.error(err);
res.status(401).end();
});
});
app.use('/api/pages',

View File

@ -16,6 +16,22 @@ module.exports = function(db) {
var module = {};
module.deletePage = function(id) {
return new Promise(function(resolve, reject) {
db.oneOrNone(
'delete from pages where id=${id}',{id:id})
.then((d) => {
return resolve(d);
})
.catch((err)=> {
return reject(err);
});
});
};
module.getLatestAddedPage = function() {
return new Promise(function(resolve, reject) {
@ -45,6 +61,30 @@ module.exports = function(db) {
});
};
module.getPage = function(pid) {
return new Promise(function(resolve, reject) {
db.oneOrNone(
'select * from pages where pid=${pid}',{pid:pid})
.then((d) => {
let attributeObj = $U.newObjectFrom(d,
['id', 'cid', 'vid', 'pid', 'content', 'title']);
attributeObj = $U.populateObject(d.data, attributeObj);
let newObj = {
"id": attributeObj.id,
"type": "page",
"attributes": attributeObj
};
return resolve(newObj);
})
.catch((err)=> {
return reject(err);
});
});
};
module.getFullPageList = function() {
return new Promise(function(resolve, reject) {