From e4a2872834795f91f55b1b1e355312108d8d3ef7 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 21 Jan 2018 22:10:02 -0500 Subject: [PATCH] Added Promise implementation on post pages --- admin/admin.js | 4 +-- includes/sidebar.js | 2 +- posts/posts.js | 71 +++++++++++++++++++++++++++++---------- posts/singlePost.js | 33 ++++++++++-------- server.js | 5 ++- utils/sql.js | 81 +++++++++++++++++++++++++++++++-------------- utils/utils.js | 21 +++++++----- 7 files changed, 148 insertions(+), 69 deletions(-) diff --git a/admin/admin.js b/admin/admin.js index 5dd0876..9c901b8 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -42,11 +42,11 @@ module.exports= } }); - } + }; var promiseToDisplayContents = function(postData) { return require("../admin/addCategory.js").main(result, postData); - } + }; promiseToGetPost(request).then(function (postData) { diff --git a/includes/sidebar.js b/includes/sidebar.js index 99ded45..6e3ae48 100644 --- a/includes/sidebar.js +++ b/includes/sidebar.js @@ -4,6 +4,6 @@ module.exports= { main: function(res, fileName) { - utils.include(res,"includes/sidebar.html"); + return utils.include(res,"includes/sidebar.html"); } }; \ No newline at end of file diff --git a/posts/posts.js b/posts/posts.js index 236aeea..a2cbf4e 100644 --- a/posts/posts.js +++ b/posts/posts.js @@ -1,34 +1,71 @@ const utils = require('../utils/utils.js'); const sql = require('../utils/sql'); +var Promise = require('promise'); -module.exports= +var renderSideBar = function(res, requestURL) { - /** - * Function which parses a url and displays appropriate post - * - * @param res - * @param fileName request url - */ - main: function(res, requestURL, request) + return new Promise(function(resolve, reject) { - res.write("
"); + require("../includes/sidebar.js").main(res, requestURL).then(function() + { + resolve(); + }) + }); +}; +var renderPost = function(res, requestURL) +{ + return new Promise(function(resolve, reject) + { + res.write("
"); var splitURL = requestURL.split("/"); //user entered /category/name/ or /category/name if(splitURL.length == 3 || splitURL.length == 4) { - var post = sql.getPost(requestURL); - if(post) - require("../posts/singlePost.js").renderPost(res, post); - else - utils.include(res, "includes/404.html"); + sql.getPost(requestURL).then(function(post) + { + console.log("a " + post); + if(post != 0) + { + return require("../posts/singlePost.js").renderPost(res, post); + } + else + { + return utils.include(res, "includes/404.html"); + } + }).then(function() + { + res.write("
"); + resolve(); + }) } else { - + console.log("error in parameters"); } - res.write("
"); - require("../includes/sidebar.js").main(res, requestURL); + }); +}; + +module.exports= +{ + /** + * Function which parses a url and displays appropriate post + * + * @param res + * @param fileName request url + */ + main: function(res, requestURL, request) + { + return new Promise(function(resolve, reject) + { + renderPost(res, requestURL).then(function() + { + return renderSideBar(res, requestURL); + }).then(function () + { + resolve(); + }); + }); } }; \ No newline at end of file diff --git a/posts/singlePost.js b/posts/singlePost.js index bfb792b..96de4f1 100644 --- a/posts/singlePost.js +++ b/posts/singlePost.js @@ -1,22 +1,29 @@ const utils = require('../utils/utils.js'); +var Promise = require('promise'); + module.exports= { renderPost: function(res, post) { - res.write("
"); - //image - res.write("
"); - //title - res.write("

" + post.name + "

"); - //date - res.write("
" + post.date + "
"); - res.write("
"); - - res.write("
"); - //include page content - utils.include(res, "../entries/" + post.url); - res.write("
") + return new Promise(function (resolve, reject) + { + res.write("
"); + //image + res.write("
"); + //title + res.write("

" + post.name + "

"); + //date + res.write("
" + post.date + "
"); + res.write("
"); + + res.write("
"); + //include page content + utils.include(res, "../entries/" + post.url).then(function (value) { + res.write("
"); + resolve(); + }); + }); } }; diff --git a/server.js b/server.js index bb0ae71..8bcf7bc 100644 --- a/server.js +++ b/server.js @@ -59,7 +59,8 @@ http.createServer(function (request, res) { return new Promise(function(resolve, reject) { - require(file).main(res, filename, request).then(function(){ + require(file).main(res, filename, request).then(function() + { resolve(); }); }); @@ -79,9 +80,7 @@ http.createServer(function (request, res) displayHeader().then(function() { - return displayContent(); - }).then(function(){ return displayFooter() }).then(function(){ diff --git a/utils/sql.js b/utils/sql.js index a82fd53..8c849bb 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -2,6 +2,8 @@ const mysql = require('mysql'); const sanitizer = require('sanitizer'); +var Promise = require('promise'); + const con = mysql.createConnection({ host: "localhost", user: "blog_user", @@ -14,24 +16,34 @@ con.connect(function(err) { console.log("Connected!"); }); -module.exports= +/** + * Function used to query the database for records + * + * @param sqlStatement + * @returns {Array} + */ +var fetch = function(sqlStatement) { - /** - * Function used to query the database for records - * - * @param sqlStatement - * @returns {Array} - */ - fetch : function(sqlStatement) + console.log("sql fetch method called"); + return new Promise(function(resolve, reject) { + con.query(sqlStatement, function (err, result) { - if (err) throw err; - return result; + if (err) + { + console.log(err); + reject(); + } + console.log("sql statement method"); + resolve(result); }); + }); - return []; - }, +}; + +module.exports= +{ /** * Function used to use insert statements into the database * @@ -42,7 +54,7 @@ module.exports= */ insert : function(sqlStatement) { - con.query(sanitizer.sanitize(sqlStatement), function (err, result) + con.query((sqlStatement), function (err, result) { if (err) { @@ -62,19 +74,38 @@ module.exports= */ getPost : function(requestURL) { - var splitURL = requestURL.split("/"); - var result_category = this.fetch("select * from categories " + - "where url='" + splitURL[1] + "'"); - if(result_category.length != 0) + return new Promise(function(resolve, reject) { - var result_posts = this.fetch("select * from posts where" + - "category_id='" + result_category[0].category_id + "' " + - "and url='" + splitURL[2] + "'"); - if(result_posts.length != 0) + var splitURL = requestURL.split("/"); + var q = "select * from categories where url='" + splitURL[1] + "'"; + fetch(q).then(function (result_category) { - return result_posts[0]; - } - } - return 0; + console.log(result_category); + if(result_category.length != 0) + { + + q = "select * from posts where category_id='" + result_category[0].category_id + "' and url='" + splitURL[2] + "'"; + console.log(q); + fetch(q).then(function (result_posts) + { + console.log(result_posts); + if(result_posts != 0) + { + resolve(result_posts[0]); + } + else + { + resolve(0); + } + }); + } + else + { + resolve(0); + } + + }); + + }); } }; \ No newline at end of file diff --git a/utils/utils.js b/utils/utils.js index 9874d8b..496e5d9 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -5,6 +5,8 @@ const fs = require('fs'); +var Promise = require('promise'); + module.exports= { /** @@ -16,15 +18,18 @@ module.exports= */ include: function(result, fileName) { - try - { - result.write(fs.readFileSync(fileName)); - } - catch (e) + return new Promise(function(resolve, reject) { - console.log("Could not find " + fileName); - } - + try + { + result.write(fs.readFileSync(fileName)); + } + catch (e) + { + console.log("Could not find " + fileName); + } + resolve(); + }); }, /**