Browse Source

Updated route to get admin home and posts pages to work.

pull/61/head
jrtechs 5 years ago
parent
commit
c86145125c
6 changed files with 94 additions and 20 deletions
  1. +22
    -13
      admin/adminHome.js
  2. +10
    -5
      admin/posts.js
  3. +4
    -1
      routes/admin/index.js
  4. +1
    -0
      routes/admin/login.js
  5. +7
    -0
      routes/admin/posts.js
  6. +50
    -1
      utils/utils.js

+ 22
- 13
admin/adminHome.js View File

@ -29,13 +29,19 @@ const processPostAddCategory = function(postData)
if(sql.insert(q) != 0)
{
console.log("category added");
resolve();
}
else
{
reject();
console.log("error adding category");
}
}
resolve("");
else
{
resolve();
}
});
};
@ -74,7 +80,6 @@ const processPost = function(postData)
urls = urls.split(" ").join("-");
urls =urls.toLowerCase();
var q = "insert into posts (category_id, picture_url, published, name, url) values ";
q += "('" + post.add_post_category + "', '" + post.add_post_picture +
@ -83,7 +88,7 @@ const processPost = function(postData)
{
var map = require('../utils/generateSiteMap');
map.main();
resolve("");
resolve();
}).catch(function(error)
{
reject(error);
@ -93,15 +98,17 @@ const processPost = function(postData)
{
require("../sites/blog.js").clearCache();
require("../includes/includes.js").clearCache();
resolve();
}
else if(post.git_pull)
{
const execSync = require('child_process').execSync;
code = execSync('git pull')
code = execSync('git pull');
resolve();
}
else
{
resolve("");
resolve();
}
});
};
@ -124,29 +131,31 @@ module.exports=
appendCategoriesToTemplate(templateContext)])
.then(function(template)
{
resolve(template[0]);
templateContext.adminPage = template[0];
resolve();
}).catch(function(error)
{
console.log("error in add downloads.js");
console.log("error in add adminHome.js");
reject(error);
});
});
},
processPostData: function(postData, templateContexgt)
processPostData: function(postData)
{
return new Promise(function(resolve, reject)
{
Promise.all([processPostAddCategory(postData),
processPost(postData)])
.then(function(data)
.then(function()
{
console.log("all resolved");
resolve();
}).catch(function(error)
{
console.log("error in add downloads.js");
reject(error);
});
{
console.log("error in add downloads.js");
reject(error);
});
});
}
};

+ 10
- 5
admin/posts.js View File

@ -10,6 +10,9 @@ const sql = require('../utils/sql');
const qs = require('querystring');
const utils = require('../utils/utils');
/**
* Detects if the post data came from the edit form in blog table or edit post
* in the edit post form.
@ -91,16 +94,17 @@ module.exports=
Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
fetchPostsInformation(templateContext)]).then(function(template)
{
resolve(template[0]);
templateContext.adminPage = template[0];
resolve();
}).catch(function(error)
{
console.log("error in add admin blog.js");
console.log("error in add admin posts.js");
reject(error);
});
});
},
processPostData(postData, templateContext)
processPostData(templateContext, postData)
{
return new Promise(function(resolve, reject)
{
@ -108,10 +112,11 @@ module.exports=
processPostData(postData, templateContext),
fetchPostsInformation(templateContext)]).then(function(template)
{
resolve(template[0]);
templateContext.adminPage = template[0];
resolve();
}).catch(function(error)
{
console.log("error in add admin blog.js");
console.log("error in add admin posts.js");
reject(error);
});
});

+ 4
- 1
routes/admin/index.js View File

@ -24,7 +24,10 @@ routes.get('/', (request, result) =>
routes.post('/', (request, result) =>
{
utils.constructAdminPage(request, result, require("../../admin/adminHome").processPostData);
utils.adminPostRoute(request, require("../../admin/adminHome").processPostData).then(function()
{
result.redirect("/admin");
});
});
routes.get('*', (request, result) =>

+ 1
- 0
routes/admin/login.js View File

@ -10,6 +10,7 @@ routes.get('/', (request, result) =>
routes.post('/', (request, result) =>
{
console.log("login request");
utils.constructAdminPage(request, result, require("../../admin/login").main)
});

+ 7
- 0
routes/admin/posts.js View File

@ -1,8 +1,15 @@
const routes = require('express').Router();
const utils = require('../../utils/utils.js');
routes.get('/', (request, result) =>
{
utils.constructAdminPage(request, result, require("../../admin/posts").main)
});
routes.post('/', (request, result) =>
{
utils.adminPostPageWithOutput(request, result ,require("../../admin/posts").processPostData)
});
module.exports = routes;

+ 50
- 1
utils/utils.js View File

@ -135,6 +135,7 @@ module.exports=
if(module.exports.loggedIn(request))
{
templateContext.loggedIn = true;
promises.push(templateFiller(templateContext));
}
else
@ -156,8 +157,56 @@ module.exports=
});
},
adminPostRoute: function(request, result, templateFiller)
adminPostPageWithOutput: function(request, result, templateFiller)
{
module.exports.getPostData(request).then(function(postData)
{
var templateContext = Object();
var promises = [];
promises.push(includes.fetchTemplate("admin/adminMain.html"));
promises.push(includes.printAdminHeader(templateContext));
promises.push(includes.printFooter(templateContext));
if (module.exports.loggedIn(request))
{
templateContext.loggedIn = true;
promises.push(templateFiller(templateContext, postData));
}
else
{
//login
const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
|| request.connection.remoteAddress;
promises.push(require("../admin/login").main(request, clientAddress, templateContext));
}
Promise.all(promises).then(function (content)
{
result.write(whiskers.render(content[0], templateContext));
result.end();
}).catch(function (err) {
console.log(err);
throw err;
});
});
},
adminPostRoute: function(request, templateFiller)
{
return new Promise(function(resolve, reject)
{
module.exports.getPostData(request).then(function(postData)
{
templateFiller(postData).then(function()
{
resolve();
})
}).catch(function(e)
{
reject(e);
})
})
},

Loading…
Cancel
Save