Browse Source

Added Promise implementation on post pages

pull/4/head
jrtechs 7 years ago
parent
commit
e4a2872834
7 changed files with 148 additions and 69 deletions
  1. +2
    -2
      admin/admin.js
  2. +1
    -1
      includes/sidebar.js
  3. +54
    -17
      posts/posts.js
  4. +20
    -13
      posts/singlePost.js
  5. +2
    -3
      server.js
  6. +56
    -25
      utils/sql.js
  7. +13
    -8
      utils/utils.js

+ 2
- 2
admin/admin.js View File

@ -42,11 +42,11 @@ module.exports=
} }
}); });
}
};
var promiseToDisplayContents = function(postData) var promiseToDisplayContents = function(postData)
{ {
return require("../admin/addCategory.js").main(result, postData); return require("../admin/addCategory.js").main(result, postData);
}
};
promiseToGetPost(request).then(function (postData) promiseToGetPost(request).then(function (postData)
{ {

+ 1
- 1
includes/sidebar.js View File

@ -4,6 +4,6 @@ module.exports=
{ {
main: function(res, fileName) main: function(res, fileName)
{ {
utils.include(res,"includes/sidebar.html");
return utils.include(res,"includes/sidebar.html");
} }
}; };

+ 54
- 17
posts/posts.js View File

@ -1,34 +1,71 @@
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
const sql = require('../utils/sql'); 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("<div class=\"w3-col l8 s12\">");
require("../includes/sidebar.js").main(res, requestURL).then(function()
{
resolve();
})
});
};
var renderPost = function(res, requestURL)
{
return new Promise(function(resolve, reject)
{
res.write("<div class=\"w3-col l8 s12\">");
var splitURL = requestURL.split("/"); var splitURL = requestURL.split("/");
//user entered /category/name/ or /category/name //user entered /category/name/ or /category/name
if(splitURL.length == 3 || splitURL.length == 4) 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("</div>");
resolve();
})
} }
else else
{ {
console.log("error in parameters");
} }
res.write("</div>");
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();
});
});
} }
}; };

+ 20
- 13
posts/singlePost.js View File

@ -1,22 +1,29 @@
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
var Promise = require('promise');
module.exports= module.exports=
{ {
renderPost: function(res, post) renderPost: function(res, post)
{ {
res.write("<div class=\"w3-card-4 w3-margin w3-white\">");
//image
res.write("<div class=\"w3-container\">");
//title
res.write("<h3><b>" + post.name + "</b></h3>");
//date
res.write("<h5><span class=\"w3-opacity\">" + post.date + "</span></h5>");
res.write("</div>");
res.write("<div class=\"w3-container\">");
//include page content
utils.include(res, "../entries/" + post.url);
res.write("</div></div>")
return new Promise(function (resolve, reject)
{
res.write("<div class=\"w3-card-4 w3-margin w3-white\">");
//image
res.write("<div class=\"w3-container\">");
//title
res.write("<h3><b>" + post.name + "</b></h3>");
//date
res.write("<h5><span class=\"w3-opacity\">" + post.date + "</span></h5>");
res.write("</div>");
res.write("<div class=\"w3-container\">");
//include page content
utils.include(res, "../entries/" + post.url).then(function (value) {
res.write("</div></div>");
resolve();
});
});
} }
}; };

+ 2
- 3
server.js View File

@ -59,7 +59,8 @@ http.createServer(function (request, res)
{ {
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
require(file).main(res, filename, request).then(function(){
require(file).main(res, filename, request).then(function()
{
resolve(); resolve();
}); });
}); });
@ -79,9 +80,7 @@ http.createServer(function (request, res)
displayHeader().then(function() displayHeader().then(function()
{ {
return displayContent(); return displayContent();
}).then(function(){ }).then(function(){
return displayFooter() return displayFooter()
}).then(function(){ }).then(function(){

+ 56
- 25
utils/sql.js View File

@ -2,6 +2,8 @@ const mysql = require('mysql');
const sanitizer = require('sanitizer'); const sanitizer = require('sanitizer');
var Promise = require('promise');
const con = mysql.createConnection({ const con = mysql.createConnection({
host: "localhost", host: "localhost",
user: "blog_user", user: "blog_user",
@ -14,24 +16,34 @@ con.connect(function(err) {
console.log("Connected!"); 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) 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 * Function used to use insert statements into the database
* *
@ -42,7 +54,7 @@ module.exports=
*/ */
insert : function(sqlStatement) insert : function(sqlStatement)
{ {
con.query(sanitizer.sanitize(sqlStatement), function (err, result)
con.query((sqlStatement), function (err, result)
{ {
if (err) if (err)
{ {
@ -62,19 +74,38 @@ module.exports=
*/ */
getPost : function(requestURL) 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);
}
});
});
} }
}; };

+ 13
- 8
utils/utils.js View File

@ -5,6 +5,8 @@
const fs = require('fs'); const fs = require('fs');
var Promise = require('promise');
module.exports= module.exports=
{ {
/** /**
@ -16,15 +18,18 @@ module.exports=
*/ */
include: function(result, fileName) 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();
});
}, },
/** /**

Loading…
Cancel
Save