obrand-admin-server/server/units/db-pages.js
Martin Donnelly 40904a7f09 added session
2016-04-04 09:59:37 +01:00

117 lines
3.2 KiB
JavaScript

'use strict';
var mdErrors = require('./md-errors');
var newId = require('uuid-pure').newId;
var $U = require('md-utils');
/**
*
* User: Martin Donnelly
* Date: 2016-03-24
* Time: 11:10
*
*/
module.exports = function(db) {
var module = {};
module.getPageList = function() {
return new Promise(function(resolve, reject) {
db.any("select pages.cid, pages.pid, pages.title, company.company_name, '/export/' || pages.pid::text || '.html' as fullpath from pages join company on company.cid = pages.cid;")
.then((d) => {
return resolve(d);
})
.catch((err)=> {
console.log('+getPageList failed');
console.log(err);
return reject(err);
});
});
};
module.getFullPageList = function() {
return new Promise(function(resolve, reject) {
db.any("select * from pages;")
.then((d) => {
let rArray = [];
for (var l = 0;l < d.length;l++) {
let attributeObj = $U.newObjectFrom(d[l],['id','cid','vid','pid','content','title']);
attributeObj = $U.populateObject(d[l].data,attributeObj);
let newObj = {"id":attributeObj.id, "type":"page","attributes":attributeObj};
// { data: [ { "id": "56b7ecbc1ef21172377d6159", "type": "guest", "attributes": { "name":"Ray","message":"First ever guest post entry" } }, ... ] }
rArray.push(newObj);
}
return resolve(rArray);
})
.catch((err)=> {
console.log('+getPageList failed');
console.log(err);
return reject(err);
});
});
};
module.sqlInsertPage = function(data) {
return new Promise(function(resolve, reject) {
db.func('upsert_page',data)
.then((d)=> {
console.log(d);
console.log('+sqlInsertPage OK');
return resolve('ok');
})
.catch((err)=> {
console.log('+sqlInsertPage failed');
console.log(err);
return reject(err);
});
});
};
module.addNewPage = function(data) {
return new Promise((resolve, reject) => {
let _data, _jsonData;
console.log(data);
_data = $U.cloneTrim(data.attributes);
if (typeof _data.pid === 'undefined' || _data.pid === null) {
_data.pid = newId();
}
_jsonData = $U.newObjectFrom(_data, ['title','image-url','link1-text','link1-url','link2-text','link2-url','link3-text','link3-url','link4-text','link4-url']);
const sqlData = [_data.cid, _data.vid, _data.pid, _data.content, _data.title, JSON.stringify(_jsonData)];
console.log(sqlData);
this.sqlInsertPage(sqlData)
.then(function(d) {
console.log('Inserted');
console.log(d);
data.attributes = _data;
return resolve(data);
})
.catch(function(err) {
console.log('Failed to insert');
return reject(err);
});
});
};
return module;
};