diff --git a/admin/admin.js b/admin/admin.js index 5e4b7c7..4bf6292 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -27,27 +27,27 @@ module.exports= */ main: function(request, clientAddress, templateContext, filename) { - console.log("admin main called"); return new Promise(function(resolve, reject) { //if logged in if(request.session && request.session.user) { - console.log(filename); templateContext.loggedIn = true; utils.getPostData(request).then(function (postData) { - console.log("temp 1"); var page = "./adminHome.js"; if(filename.includes('/downloads')) { page = "./adminDownloads.js"; - console.log("downloads time") } else if(filename.includes("/posts")) { page = "./posts.js"; } + else if(filename.includes("/users")) + { + page = "./users.js"; + } require(page).main(postData, templateContext).then(function(template) { @@ -61,7 +61,8 @@ module.exports= } else { - require("./login/login.js").main(request, clientAddress, templateContext).then(function() + require("./login/login.js").main(request, clientAddress, templateContext) + .then(function() { resolve(); }).catch(function(err) diff --git a/admin/login/login.js b/admin/login/login.js index 663a3c2..220f369 100644 --- a/admin/login/login.js +++ b/admin/login/login.js @@ -23,7 +23,7 @@ const processLogin = function(request, clientAddress, templateContext) { if(DEBUG) { - //what actually logs in the user + //logs in as first user in DB request.session.user = 1; console.log("user has logged in"); templateContext.goodLoginAttempt = true; diff --git a/admin/users.js b/admin/users.js new file mode 100644 index 0000000..b0d1c4c --- /dev/null +++ b/admin/users.js @@ -0,0 +1,175 @@ +/** + * File which deals with adding and removing downloads from + * the admin section of the website. + * + * @author Jeffery Russell 6-30-18 + */ + +/** Whiskers template file */ +const TEMPLATE_FILE = "admin/adminUsers.html"; + + +const includes = require('../includes/includes.js'); + +//updates db +const sql = require('../utils/sql'); + +//parses post data +const qs = require('querystring'); + + +/** + * Processes post requests from the addDownload form + * + * @param postData + * @returns {*|Promise} + */ +const addUserPostData = function(postData) +{ + return new Promise(function(resolve, reject) + { + const post = qs.parse(postData); + if(post.add_user) + { + sql.addUser(post.add_user_name, post.add_user_password) + .then(function() + { + resolve(); + }).catch(function(error) + { + reject(error); + }) + } + else + { + resolve(); + } + }); +}; + + +/** + * Removes a download if requested by the + * post data from an admin. + */ +const removeUserPost = function(postData) +{ + return new Promise(function(resolve, reject) + { + const post = qs.parse(postData); + console.log(post); + if(post.delete_user) + { + console.log("Removing user: " + post.delete_user); + sql.removeUser(post.delete_user).then(function() + { + resolve(); + }).catch(function(err) + { + reject(err); + }); + } + else + { + resolve(); + } + }); +}; + + +/** + * Processes post data to determine if the user requested that + * a user be updated in the database. + */ +const editUserPost = function(postData, templateContext) +{ + return new Promise(function(resolve, reject) + { + const post = qs.parse(postData); + if(post.edit_user) + { + sql.getUserByID(post.edit_user).then(function(user) + { + if(user.length == 1) + { + templateContext.edit_user = post.edit_user; + templateContext.user_name = user[0].user_name; + resolve(); + } + else + { + resolve(); + } + }).catch(function(err) + { + reject(err); + }); + } + else if(post.edit_user_2) + { + sql.updateUser(post.edit_user_2, post.edit_user_name, post.edit_user_password) + .then(function() + { + resolve(); + }) + } + else + { + resolve(); + } + }); +}; + + +/** + * Fetches the download items in the database so that + * the template engine can use it to display them in + * a table. + * + * @param templateContext-- context item used by whiskers + * @returns {Promise} + */ +const getUserInformation = function(templateContext) +{ + return new Promise(function(resolve, reject) + { + sql.getAllUsers().then(function(users) + { + templateContext.users = users; + resolve(); + }).catch(function(error) + { + reject(error); + }); + }); +}; + + +module.exports= + { + /** Fetches context information for the template and handles + * post data for the downloads. + * + * @param postData posted by user + * @param templateContext json object used as the template context + * @returns {Promise} renders the template used for this page + */ + main: 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 add downloads.js"); + reject(error); + }); + }); + } + }; \ No newline at end of file diff --git a/includes/css/bootstrap.css b/includes/css/bootstrap.css index e871786..af31572 100644 --- a/includes/css/bootstrap.css +++ b/includes/css/bootstrap.css @@ -9706,5 +9706,5 @@ footer .footer-col footer .footer-below { padding: 25px 0; - background-color: #2C3E50; + background-color: #1a252f; } diff --git a/includes/html/adminHeader.html b/includes/html/adminHeader.html index a47e45b..6076443 100644 --- a/includes/html/adminHeader.html +++ b/includes/html/adminHeader.html @@ -58,7 +58,7 @@