diff --git a/admin/addCategory.js b/admin/addCategory.js index fea0bec..8408d9e 100644 --- a/admin/addCategory.js +++ b/admin/addCategory.js @@ -7,50 +7,48 @@ const Promise = require('promise'); /** * Displays all the categories in the database - * @param res * @return {*|Promise} */ -var printCategories = function(res) +const printCategories = function() { - res.write("
"); - res.write("

Categories

"); - res.write("
"); - res.write(""); - res.write(""); - res.write(""); - res.write(""); + var html = "
" + + "

Categories

" + + "
NameURLEdit
" + + "" + + "" + + "" + + ""; + return new Promise(function(resolve, reject) { sql.getCategories().then(function(categories) { categories.forEach(function(c) { - res.write(""); - - res.write(""); - - res.write(""); - - res.write(""); - - res.write(""); + html +="" + + "" + + "" + + "" + + ""; }); - res.write("
NameURLEdit
" + c.name + "" + c.url + "" + c.category_id + "
" + c.name + "" + c.url + "" + c.category_id + "
"); - resolve(); + resolve(html + ""); + }).catch(function(error) + { + reject(error); }) }); }; + /** * Checks for post data regarding adding a new category. * If a post is made with add_category, it parses the url-- replaces spaces * with dashes -- and calls a insert method on the database * - * @param res * @param postData * @return {*|Promise} */ -var processPost = function(res, postData) +const processPost = function(postData) { return new Promise(function(resolve, reject) { @@ -69,30 +67,24 @@ var processPost = function(res, postData) console.log("error adding category"); } } - resolve(postData); + resolve(""); }); }; module.exports= { - main: function(res, postData) + main: function(postData) { - res.write("
"); return new Promise(function(resolve, reject) { - utils.include(res, "./admin/addCategory.html"); - printCategories(res).then(function() - { - //console.write("categories finished"); - return processPost(res, postData); - }).then(function() + Promise.all([utils.include("./admin/addCategory.html"), printCategories(), processPost(postData)]).then(function(html) { - res.write("
"); - resolve(postData); - }).catch(function(err) + resolve("
" + html.join('') + "
"); + }).catch(function(error) { - console.log(err); + console.log("error in cat.js"); + reject(error); }) }); } diff --git a/admin/addDownload.js b/admin/addDownload.js index a5bbdab..f9e2d81 100644 --- a/admin/addDownload.js +++ b/admin/addDownload.js @@ -11,11 +11,10 @@ const Promise = require('promise'); /** * Processes post requests from the addDownload form - * @param res * @param postData * @returns {*|Promise} */ -var addDownloadPostData = function(res, postData) +var addDownloadPostData = function(postData) { return new Promise(function(resolve, reject) { @@ -26,12 +25,15 @@ var addDownloadPostData = function(res, postData) sql.addDownload(post.add_download_name, post.add_download_file).then(function() { - resolve(postData); - }); + resolve(""); + }).catch(function(error) + { + reject(error); + }) } else { - resolve(postData); + resolve(""); } }); }; @@ -40,27 +42,23 @@ var addDownloadPostData = function(res, postData) /** * Displays the addDownload form the the user * - * @param res * @param postData * @returns {*|Promise} */ -var addDownload = function(res, postData) +const addDownload = function(postData) { - res.write("
"); + //res.write("
"); return new Promise(function(resolve, reject) { - addDownloadPostData(res, postData).then(function() - { - return utils.include(res, "./admin/addDownload.html"); - }).then(function() + Promise.all([addDownloadPostData(postData), utils.include("./admin/addDownload.html")]).then(function(html) { - res.write("
"); - resolve(postData); - }).catch(function(err) + console.log("add download is good"); + resolve("
" + html.join('') + "
"); + }).catch(function(error) { - console.log(err); - reject(err); - }); + console.log(error); + reject(error); + }) }); }; @@ -68,11 +66,10 @@ var addDownload = function(res, postData) /** * Handel form requests from the downloads table * - * @param res * @param postData * @returns {*|Promise} */ -var displayDownloadsPostData = function(res, postData) +const displayDownloadsPostData = function(postData) { return new Promise(function(resolve, reject) { @@ -90,58 +87,42 @@ var displayDownloadsPostData = function(res, postData) /** * Renders a single download row in the downloads table * - * @param result * @param download * @returns {*|Promise} */ -var renderDownloadRow = function(result, download) +const renderDownloadRow = function(download) { - return new Promise(function(resolve, reject) - { - result.write(""); - - //download name - result.write("" + download.name + ""); - - //file name - result.write("" + download.file + ""); - - //download count - result.write("" + download.download_count + ""); - - //edit - result.write("
\n" + + return "" + + "" + download.name + "" + + "" + download.file + "" + + "" + download.download_count + "" + + "\n" + " \n" + ""+ - "
"); - - result.write(""); - - resolve(); - }); + "" + + ""; }; /** * Displays all the download information in a table - * @param res * @param postData * @returns {*|Promise} */ -var displayDownloads = function(res, postData) +const displayDownloads = function(postData) { - res.write("
"); + var html = "
"; return new Promise(function(resolve, reject) { - displayDownloadsPostData(res, postData).then(function() + displayDownloadsPostData(postData).then(function() { - res.write("
"); - res.write("

Downloads

"); - res.write("
"); - res.write(""); - res.write(""); - res.write(""); + html += "
" + + "

Downloads

" + + "
Download NameFileDownload CountDelete
" + + "" + + "" + + ""; sql.getAllDownloads().then(function(downloads) @@ -150,26 +131,20 @@ var displayDownloads = function(res, postData) downloads.forEach(function(download) { - downloadPromises.push(new Promise(function(resolveDownload, reject) - { - renderDownloadRow(res, download).then(function() - { - resolveDownload(); - }).catch(function(error) - { - reject(error); - }) - })); + downloadPromises.push(renderDownloadRow(download)); }); - Promise.all(downloadPromises).then(function() + Promise.all(downloadPromises).then(function(htmls) { - res.write("
Download NameFileDownload CountDelete

"); - res.write("
"); - resolve(postData); + var htmlafter = "

" + + ""; + + console.log("display download is good"); + resolve(html + htmls.join('') + htmlafter); }); }).catch(function(error) { + console.log(error); reject(error); }); }); @@ -180,23 +155,18 @@ var displayDownloads = function(res, postData) module.exports= { - main: function(res, postData) + main: function(postData) { - res.write("
"); return new Promise(function(resolve, reject) { - addDownload(res, postData).then(function() + Promise.all([addDownload(postData), displayDownloads(postData)]).then(function(html) { - return displayDownloads(res, postData); - }).then(function() - { - res.write("
"); - resolve(postData); - }).catch(function(err) + resolve("
" + html.join('') + "
"); + }).catch(function(error) { - console.log(err); - reject(err); - }) + console.log("error in add downloads.js"); + reject(error); + }); }); } }; \ No newline at end of file diff --git a/admin/admin.js b/admin/admin.js index 0b8207b..8017eaa 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -1,17 +1,16 @@ const utils = require('../utils/utils.js'); -var Promise = require('promise'); +const Promise = require('promise'); module.exports= { /** * Method calls the admin widgets it correct order * - * @param result * @param fileName * @param request * @return {*|Promise} */ - main: function(result, fileName, request) + main: function(fileName, request) { return new Promise(function(resolve, reject) { @@ -19,31 +18,25 @@ module.exports= { utils.getPostData(request).then(function (postData) { - return require("../admin/newPost.js").main(result, postData); - }).then(function(postData) - { - return require("../admin/addCategory.js").main(result, postData); - }).then(function(postData) - { - result.write(""); //ends main row - return require("../admin/editPost.js").main(result, postData); - }).then(function(postData) - { - return require("../admin/addDownload.js").main(result, postData); - }).then(function() - { - resolve(); - }).catch(function(error) - { - reject(error); + Promise.all([require("../admin/newPost.js").main(postData), + require("../admin/addCategory.js").main(postData), + require("../admin/editPost.js").main(postData), + require("../admin/addDownload.js").main(postData)]) + .then(function(content) + { + resolve(content.join('')); + }).catch(function(error) + { + reject(error); + }); }); } else { //login page - require("../admin/login.js").main(result, request).then(function() + require("../admin/login.js").main(request).then(function(html) { - resolve(); + resolve(html); }).catch(function(err) { console.log(err); diff --git a/admin/editPost.js b/admin/editPost.js index 7d48c5f..491cfac 100644 --- a/admin/editPost.js +++ b/admin/editPost.js @@ -8,54 +8,39 @@ const Promise = require('promise'); const qs = require('querystring'); const sql = require('../utils/sql'); + /** * Displays a single row in the posts view - * @param result + * * @param post */ -var renderPostRow = function(result, post) +const renderPostRow = function(post) { - return new Promise(function(resolve, reject) - { - result.write(""); - - //category - result.write("" + post.category_id + ""); - - //name - result.write("" + post.name + ""); - - //picture - result.write("" + post.picture_url + ""); - - //date - result.write("" + post.published + ""); - - //edit - result.write("
\n" + - " " + + "" + post.category_id + "" + + "" + post.name + "" + + "" + post.picture_url + "" + + "" + post.published + "" + + "\n" + + "\n" + ""+ - "
"); - - result.write(""); - - resolve(); - }); + "" + + ""; }; + /** * Displays all the posts in a table - * @param result */ -var postsTable = function(result) +const postsTable = function() { - result.write("
"); - result.write("

Posts

"); - result.write("
"); - result.write(""); - result.write(""); - result.write(""); + var html = "
" + + "

Posts

" + + "
Category #NameHeader PictureDateEdit
" + + "" + + "" + + ""; return new Promise(function(resolve, reject) { sql.getAllPosts().then(function(posts) @@ -63,48 +48,35 @@ var postsTable = function(result) var postPromises = []; posts.forEach(function(post) { - postPromises.push(new Promise(function(res, rej) - { - renderPostRow(result, post).then(function() - { - res(); - }).catch(function(error) - { - console.log("error rendering " + post); - rej(error); - }) - })); + postPromises.push(renderPostRow(post)); }); - Promise.all(postPromises).then(function() + + Promise.all(postPromises).then(function(htmls) { - result.write("
Category #NameHeader PictureDateEdit

"); - resolve(); + resolve(html + htmls.join('') + "
"); }).catch(function(error) { - console.log(error); - console.log("error rendering posts"); reject(error); }); }).catch(function(error) { - console.log("error with sql query"); reject(error); }) }); }; + /** * Displays the edit form for edit posts - * @param result * @param post_id */ -var displayRenderForm = function(result, post_id) +const displayRenderForm = function(post_id) { return new Promise(function(resolve, reject) { sql.getPostById(post_id).then(function(post) { - result.write("
"+ + var html = "
"+ "

Edit Post

"+ "
"+ "
\n" + @@ -127,26 +99,24 @@ var displayRenderForm = function(result, post_id) " class=\"btn btn-lg btn-secondary\"/>
"+ ""+ "
"+ - "

" - ); - resolve(); + "

"; + + resolve(html); }).catch(function(error) { - console.log(error); - console.log("error getting post from sql in display Reender Form"); reject(error); }); }); }; + /** * Detects if the post data came from the edit form in posts table or edit post * in the edit post form. Based on this, this function will call one of two functions - * @param result * @param postData */ -var processPost = function(result, postData) +const processPost = function(postData) { return new Promise(function(resolve, reject) { @@ -155,30 +125,27 @@ var processPost = function(result, postData) if(postParsed.edit_post) { //display edit form - displayRenderForm(result, postParsed.edit_post).then(function() + displayRenderForm(postParsed.edit_post).then(function(html) { - resolve(); + resolve(html); }).catch(function(error) { - console.log(error); - console.log("error processing the edit post data"); + reject(error); }); } else if(postParsed.edit_post_2) { - //insert edit into sql - - sql.editPost(postParsed).then(function() + sql.editPost(postParsed).then(function(html) { - resolve(); + resolve(html); }).catch(function(error) { - console.log("error inserting edit post data into sql"); + reject(error); }); } else { - resolve(); + resolve(""); } }); }; @@ -189,25 +156,21 @@ module.exports= /** * Method which calls helper functions which processes post data for editing posts * and calls a function which displays all the posts in a table - * @param result * @param postData */ - main: function(result, postData) + main: function(postData) { return new Promise(function(resolve, reject) { - result.write("
"); - processPost(result, postData).then(function() + Promise.all([processPost(postData), + postsTable()]).then(function(html) { - return postsTable(result); - }).then(function() - { - resolve(postData); + resolve("
" + html.join('')); }).catch(function(error) { - console.log("Error in edit post module"); + console.log("error in edit post.js"); reject(error); - }); + }) }); } }; \ No newline at end of file diff --git a/admin/login.js b/admin/login.js index 4900613..5a14c18 100644 --- a/admin/login.js +++ b/admin/login.js @@ -2,7 +2,7 @@ const utils = require('../utils/utils.js'); const Promise = require('promise'); const sql = require('../utils/sql'); -var processLogin = function(result, request) +const processLogin = function(request) { return new Promise(function(resolve, reject) { @@ -14,13 +14,16 @@ var processLogin = function(result, request) if(loginResult.pass) { request.session.user = loginResult.user; - result.write(""); + console.log("user has logged in"); + resolve(""); + } + else + { + resolve(""); } - resolve(); }).catch(function(err) { - console.log(err); - resolve(); + reject(err); }) }); }; @@ -28,22 +31,18 @@ var processLogin = function(result, request) module.exports= { - main: function(result, request) + main: function(request) { - console.log("main of login.js"); return new Promise(function(resolve, reject) { - utils.include(result, './admin/login.html').then(function() - { - console.log("got login html"); - return require("../sidebar/sidebar.js").main(result); - }).then(function() + Promise.all([utils.include('./admin/login.html'), + require("../sidebar/sidebar.js").main(), + processLogin(request)]).then(function(html) { - return processLogin(result, request); - }).then(function() + resolve(html.join('') + ""); + }).catch(function(err) { - result.write(""); - resolve(); + reject(err); }) }); }, diff --git a/admin/newPost.js b/admin/newPost.js index a1c14b8..e627e13 100644 --- a/admin/newPost.js +++ b/admin/newPost.js @@ -2,55 +2,67 @@ const utils = require('../utils/utils.js'); const sql = require('../utils/sql'); const qs = require('querystring'); -var Promise = require('promise'); +const Promise = require('promise'); + + +/** + * + * @param postData + * @return {*|Promise} + */ +const processPost = function(postData) +{ + return new Promise(function(resolve, reject) + { + var post = qs.parse(postData); + if(post.add_post_name) + { + var urls = post.add_post_name; + 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 + + "', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')"; + sql.insert(q).then(function() + { + var map = require('../utils/generateSiteMap'); + map.main(); + resolve(""); + }).catch(function(error) + { + reject(error); + }) + } + else + { + resolve(""); + } + }); +}; module.exports= { /** * - * @param res * @param postData * @return {*} */ - main: function(res, postData) - { - utils.include(res, "./admin/newPost.html"); - return this.processPost(res, postData); - }, - /** - * - * @param res - * @param postData - * @return {*|Promise} - */ - processPost: function(res, postData) + main: function(postData) { return new Promise(function(resolve, reject) { - var post = qs.parse(postData); - if(post.add_post_name) + Promise.all([utils.include("./admin/newPost.html"), processPost(postData)]).then(function(html) { - var urls = post.add_post_name; - 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 + - "', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')"; - sql.insert(q).then(function() - { - var map = require('../utils/generateSiteMap'); - map.main(); - resolve(); - }) - } - else + resolve(html.join('')); + }).catch(function(error) { - resolve(postData); - } + console.log(error); + reject(error); + }) }); } }; \ No newline at end of file diff --git a/downloads/content/Cube-Field-youtube-domo.zip b/downloads/content/Cube-Field-youtube-demo.zip similarity index 100% rename from downloads/content/Cube-Field-youtube-domo.zip rename to downloads/content/Cube-Field-youtube-demo.zip diff --git a/downloads/downloads.js b/downloads/downloads.js index 2691475..461994f 100644 --- a/downloads/downloads.js +++ b/downloads/downloads.js @@ -3,9 +3,7 @@ including html files easier for me programming. */ -const fs = require('fs'); - -var Promise = require('promise'); +const Promise = require('promise'); const utils = require('../utils/utils.js'); @@ -34,7 +32,7 @@ module.exports= console.log(result); if(result.length == 1) { - var file = './downloads/content/' + result[0].file; + const file = './downloads/content/' + result[0].file; res.download(file); } else @@ -48,9 +46,9 @@ module.exports= } else { - utils.print404(res).then(function() + utils.print404().then(function(content) { - resolve(); + resolve(content); }) } }); diff --git a/server.js b/server.js index ed84e53..2530b92 100644 --- a/server.js +++ b/server.js @@ -26,6 +26,7 @@ app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxA const port = 8000; +app.use(express.static(__dirname + './', { maxAge: 86400000 })); /** * Parses the request url and calls correct JS files @@ -83,6 +84,7 @@ app.use(function(request, res) }).catch(function(err) { console.log(err); + throw err; }); } } diff --git a/utils/sql.js b/utils/sql.js index cdb5495..b1d096f 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -10,7 +10,7 @@ const qs = require('querystring'); const utils = require('../utils/utils.js'); -var con = mysql.createConnection({ +const con = mysql.createConnection({ host: "localhost", user: "blog_user", password: utils.getFileLine('../sql_secret'), @@ -22,13 +22,14 @@ con.connect(function(err) { if (err) throw err; }); + /** * Function used to query the database for records * * @param sqlStatement * @returns {Array} */ -var fetch = function(sqlStatement) +const fetch = function(sqlStatement) { return new Promise(function(resolve, reject) { @@ -44,6 +45,7 @@ var fetch = function(sqlStatement) }); }; + module.exports= { /** @@ -251,7 +253,7 @@ module.exports= */ checkLogin: function(postData) { - var post = qs.parse(postData); + const post = qs.parse(postData); return new Promise(function(resolve, reject) { var result = Object(); diff --git a/utils/utils.js b/utils/utils.js index 291fa2b..79e87ea 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -5,7 +5,7 @@ const fs = require('fs'); -var Promise = require('promise'); +const Promise = require('promise'); module.exports= {