2016-03-31 13:24:21 +00:00
'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 = { } ;
2016-04-05 13:53:16 +00:00
module . getLatestAddedPage = function ( ) {
console . log ( '+ getLatestAddedPage' ) ;
//select '/export/' || pages.pid::text || '.html' as fullpath from pages order by id desc limit 1;
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 ) => {
console . log ( d ) ;
console . log ( '- getlastaddedpage' ) ;
return resolve ( d ) ;
} )
. catch ( ( err ) => {
console . log ( '+getLatestAddedPage failed' ) ;
console . log ( err ) ;
return reject ( err ) ;
} ) ;
} ) ;
} ;
2016-03-31 13:24:21 +00:00
module . getPageList = function ( ) {
return new Promise ( function ( resolve , reject ) {
2016-04-05 13:53:16 +00:00
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;" )
2016-03-31 13:24:21 +00:00
. then ( ( d ) => {
return resolve ( d ) ;
} )
. catch ( ( err ) => {
2016-04-05 13:53:16 +00:00
console . log ( '+getPageList failed' ) ;
console . log ( err ) ;
return reject ( err ) ;
} ) ;
2016-03-31 13:24:21 +00:00
} ) ;
} ;
2016-04-04 08:59:37 +00:00
module . getFullPageList = function ( ) {
2016-04-05 13:53:16 +00:00
return new Promise ( function ( resolve , reject ) {
2016-04-04 08:59:37 +00:00
2016-04-05 13:53:16 +00:00
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 ) ;
2016-04-04 08:59:37 +00:00
2016-04-05 13:53:16 +00:00
let newObj = {
"id" : attributeObj . id ,
"type" : "page" ,
"attributes" : attributeObj
} ;
2016-04-04 08:59:37 +00:00
2016-04-05 13:53:16 +00:00
// { data: [ { "id": "56b7ecbc1ef21172377d6159", "type": "guest", "attributes": { "name":"Ray","message":"First ever guest post entry" } }, ... ] }
2016-04-04 08:59:37 +00:00
2016-04-05 13:53:16 +00:00
rArray . push ( newObj ) ;
}
return resolve ( rArray ) ;
2016-04-04 08:59:37 +00:00
2016-04-05 13:53:16 +00:00
} )
. catch ( ( err ) => {
console . log ( '+getPageList failed' ) ;
console . log ( err ) ;
return reject ( err ) ;
} ) ;
} ) ;
} ;
2016-04-04 08:59:37 +00:00
2016-03-31 13:24:21 +00:00
module . sqlInsertPage = function ( data ) {
return new Promise ( function ( resolve , reject ) {
2016-04-05 13:53:16 +00:00
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 ) ;
} ) ;
} ) ;
2016-03-31 13:24:21 +00:00
} ;
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 ( ) ;
}
2016-04-05 13:53:16 +00:00
_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 )
] ;
2016-03-31 13:24:21 +00:00
console . log ( sqlData ) ;
this . sqlInsertPage ( sqlData )
2016-04-05 13:53:16 +00:00
. 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 ) ;
} ) ;
2016-03-31 13:24:21 +00:00
} ) ;
} ;
return module ;
} ;