obrand-admin-server/server/units/db-pages.js
2016-04-08 16:45:02 +01:00

136 lines
3.0 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.getLatestAddedPage = function() {
return new Promise(function(resolve, reject) {
db.oneOrNone(
'select \'export/\' || pages.pid::text || \'.html\' as fullpath from pages order by id desc limit 1;')
.then((d) => {
return resolve(d);
})
.catch((err)=> {
return reject(err);
});
});
};
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)=> {
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
};
rArray.push(newObj);
}
return resolve(rArray);
})
.catch((err)=> {
return reject(err);
});
});
};
module.sqlInsertPage = function(data) {
return new Promise(function(resolve, reject) {
db.func('upsert_page', data)
.then(()=> {
return resolve('ok');
})
.catch((err)=> {
return reject(err);
});
});
};
module.addNewPage = function(data) {
return new Promise((resolve, reject) => {
let _data, _jsonData;
_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)
];
this.sqlInsertPage(sqlData)
.then(function() {
data.attributes = _data;
return resolve(data);
})
.catch(function(err) {
return reject(err);
});
});
};
return module;
};