mirror of
https://gitlab.silvrtree.co.uk/martind2000/project.git
synced 2025-01-10 23:35:08 +00:00
73 lines
1.7 KiB
JavaScript
73 lines
1.7 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.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;
|
|
|
|
};
|