diff --git a/O-BrandServer/process.json b/O-BrandServer/process.json new file mode 100644 index 0000000..3f175a8 --- /dev/null +++ b/O-BrandServer/process.json @@ -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"] + }] +} diff --git a/O-BrandServer/server/app/pages.js b/O-BrandServer/server/app/pages.js index d970cf3..589081b 100644 --- a/O-BrandServer/server/app/pages.js +++ b/O-BrandServer/server/app/pages.js @@ -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', diff --git a/O-BrandServer/server/units/db-pages.js b/O-BrandServer/server/units/db-pages.js index 4e51371..a3c55a1 100644 --- a/O-BrandServer/server/units/db-pages.js +++ b/O-BrandServer/server/units/db-pages.js @@ -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) {