|
|
@ -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); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
}; |