diff --git a/admin/adminDownloads.js b/admin/adminDownloads.js index 08e45d6..717a43d 100644 --- a/admin/adminDownloads.js +++ b/admin/adminDownloads.js @@ -109,13 +109,11 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - addDownloadPostData(postData), - removeDownloads(postData), displayDownloads(templateContext)]).then(function(template) { resolve(template[0]); @@ -125,5 +123,21 @@ module.exports= reject(error); }); }); + }, + + processPostData: function(postData, templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([addDownloadPostData(postData), + removeDownloads(postData)]).then(function() + { + resolve(); + }).catch(function(error) + { + console.log(error); + reject(error); + }) + }) } }; \ No newline at end of file diff --git a/admin/adminHome.js b/admin/adminHome.js index c1dbdd0..0cc2e34 100644 --- a/admin/adminHome.js +++ b/admin/adminHome.js @@ -116,15 +116,12 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) { - console.log("called"); return new Promise(function(resolve, reject) { Promise.all([includes.fetchTemplate(TEMPLATE_FILE), - processPostAddCategory(postData), - appendCategoriesToTemplate(templateContext), - processPost(postData)]) + appendCategoriesToTemplate(templateContext)]) .then(function(template) { resolve(template[0]); @@ -134,5 +131,22 @@ module.exports= reject(error); }); }); + }, + + processPostData: function(postData, templateContexgt) + { + return new Promise(function(resolve, reject) + { + Promise.all([processPostAddCategory(postData), + processPost(postData)]) + .then(function(data) + { + resolve(); + }).catch(function(error) + { + console.log("error in add downloads.js"); + reject(error); + }); + }); } }; \ No newline at end of file diff --git a/admin/posts.js b/admin/posts.js index 019fb14..d5a9f68 100644 --- a/admin/posts.js +++ b/admin/posts.js @@ -84,7 +84,23 @@ module.exports= * @param templateContext json object used as the template context * @returns {Promise} renders the template used for this page */ - main: function(postData, templateContext) + main: function(templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + fetchPostsInformation(templateContext)]).then(function(template) + { + resolve(template[0]); + }).catch(function(error) + { + console.log("error in add admin blog.js"); + reject(error); + }); + }); + }, + + processPostData(postData, templateContext) { return new Promise(function(resolve, reject) { diff --git a/admin/users.js b/admin/users.js index b0d1c4c..74fecde 100644 --- a/admin/users.js +++ b/admin/users.js @@ -167,7 +167,26 @@ module.exports= resolve(template[0]); }).catch(function(error) { - console.log("error in add downloads.js"); + console.log("error in users.js"); + reject(error); + }); + }); + }, + + processPostData: function(postData, templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + addUserPostData(postData), + removeUserPost(postData), + editUserPost(postData, templateContext), + getUserInformation(templateContext)]).then(function(template) + { + resolve(template[0]); + }).catch(function(error) + { + console.log("error in users.js"); reject(error); }); }); diff --git a/includes/includes.js b/includes/includes.js index 4c9d755..d7365de 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -153,7 +153,7 @@ module.exports = */ printAdminHeader(templateContext) { - return utils.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER); + return module.exports.includeInObject(HEADER_KEY, templateContext, ADMIN_HEADER); }, diff --git a/routes/about.js b/routes/about.js index d5ca964..ff2818c 100644 --- a/routes/about.js +++ b/routes/about.js @@ -1,5 +1,7 @@ const routes = require('express').Router(); +const utils = require('../utils/utils.js'); + routes.get('/', (request, result) => { @@ -8,7 +10,7 @@ routes.get('/', (request, result) => routes.get('*', (request, result) => { - //error + utils.print404(result); }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/index.js b/routes/admin/index.js index cdb51cc..fad942d 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -15,21 +15,21 @@ routes.use('/downloads', downloads); const users = require('./users'); routes.use('/users', users); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { - if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER) - { - utils.renderHTML(request, result, "users.html", getUserInformation); - } - else - { - utils.printError(result, "You need to be logged in"); - } + utils.constructAdminPage(request, result, require("../../admin/adminHome").main) +}); + +routes.post('/', (request, result) => +{ + utils.constructAdminPage(request, result, require("../../admin/adminHome").processPostData); }); routes.get('*', (request, result) => { - //error + utils.print404(result); }); module.exports = routes; \ No newline at end of file diff --git a/routes/admin/login.js b/routes/admin/login.js index 5c68232..22c208f 100644 --- a/routes/admin/login.js +++ b/routes/admin/login.js @@ -1,8 +1,16 @@ const routes = require('express').Router(); +const utils = require('../../utils/utils.js'); + routes.get('/', (request, result) => { + utils.constructAdminPage(request, result, require("../../admin/adminHome").main) +}); + +routes.post('/', (request, result) => +{ + utils.constructAdminPage(request, result, require("../../admin/login").main) }); module.exports = routes; \ No newline at end of file diff --git a/routes/contact.js b/routes/contact.js index 4c81fae..bce9656 100644 --- a/routes/contact.js +++ b/routes/contact.js @@ -1,5 +1,7 @@ const routes = require('express').Router(); +const utils = require('../utils/utils.js'); + routes.get('/', (request, result) => { require("../blog/contact.js").main(request, result); @@ -7,7 +9,7 @@ routes.get('/', (request, result) => routes.get('*', (request, result) => { - //error + utils.print404(result); }); module.exports = routes; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index faf2e42..74e0fae 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,14 +1,22 @@ const routes = require('express').Router(); +/** about page */ const about = require('./about'); routes.use('/about', about); +/** admin page and all of its sub pages */ +const admin = require('./admin'); +routes.use('/admin', admin); + +/** Contact page */ const contact = require('./contact'); routes.use('/contact', contact); +/** Downloads path for files */ const downloads = require('./downloads'); routes.use('/downloads', downloads); +/** static content like css, js, and images */ const includes = require('./includes'); routes.use('/includes', includes); diff --git a/sites/admin.js b/sites/admin.js index bbfa674..9b9ea2a 100644 --- a/sites/admin.js +++ b/sites/admin.js @@ -1,9 +1,6 @@ //sending static content const includes = require('../includes/includes.js'); -//used to append static content to result -const contentLoader = require('../includes/staticContentServer.js'); - const whiskers = require('whiskers'); @@ -22,34 +19,26 @@ module.exports= */ main: function(request, result, filename) { - if(contentLoader.serveStaticContent(request, result, filename, "")) - { - //do nothing - } - else - { - const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] - || request.connection.remoteAddress; + const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0] + || request.connection.remoteAddress; - result.writeHead(200, {'Content-Type': 'text/html'}); + result.writeHead(200, {'Content-Type': 'text/html'}); - const file = "../admin/admin.js"; + const file = "../admin/admin.js"; - var templateContext = Object(); - Promise.all([includes.fetchTemplate("admin/adminMain.html"), - includes.printAdminHeader(templateContext), - require(file).main(request, clientAddress, templateContext, filename), - includes.printFooter(templateContext), - ]).then(function(content) - { - result.write(whiskers.render(content[0], templateContext)); - result.end(); - - }).catch(function(err) - { - console.log(err); - throw err; - }); - } + var templateContext = Object(); + Promise.all([includes.fetchTemplate("admin/adminMain.html"), + includes.printAdminHeader(templateContext), + require(file).main(request, clientAddress, templateContext, filename), + includes.printFooter(templateContext), + ]).then(function(content) + { + result.write(whiskers.render(content[0], templateContext)); + result.end(); + }).catch(function(err) + { + console.log(err); + throw err; + }); } }; \ No newline at end of file diff --git a/templates/admin/adminMain.html b/templates/admin/adminMain.html index 94bbd8f..82cf0d6 100644 --- a/templates/admin/adminMain.html +++ b/templates/admin/adminMain.html @@ -26,7 +26,7 @@

Login

-
+
diff --git a/utils/utils.js b/utils/utils.js index 933089a..b43cbbf 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -6,6 +6,9 @@ //used for file io const fs = require('fs'); +const whiskers = require('whiskers'); + + const includes = require("../includes/includes"); @@ -118,6 +121,49 @@ module.exports= result.write(html); result.end(); }) + }, + + + constructAdminPage: function(request, result, templateFiller) + { + 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)) + { + promises.push(templateFiller(templateContext)); + } + 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, result, templateFiller) + { + + }, + + + loggedIn(request) + { + return(request.session && request.session.user); } }; \ No newline at end of file