Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.5 KiB

  1. const mysql = require('mysql');
  2. const sanitizer = require('sanitizer');
  3. var Promise = require('promise');
  4. const con = mysql.createConnection({
  5. host: "localhost",
  6. user: "blog_user",
  7. password: "password",
  8. database: "blog_name"
  9. });
  10. con.connect(function(err) {
  11. if (err) throw err;
  12. console.log("Connected!");
  13. });
  14. /**
  15. * Function used to query the database for records
  16. *
  17. * @param sqlStatement
  18. * @returns {Array}
  19. */
  20. var fetch = function(sqlStatement)
  21. {
  22. return new Promise(function(resolve, reject)
  23. {
  24. con.query(sqlStatement, function (err, result)
  25. {
  26. if (err)
  27. {
  28. reject();
  29. }
  30. resolve(result);
  31. });
  32. });
  33. };
  34. module.exports=
  35. {
  36. /**
  37. * Function used to use insert statements into the database
  38. *
  39. * Don't worry, the input gets sanitized
  40. *
  41. * @param sqlStatement
  42. * @return the id of the new record - if there is one
  43. */
  44. insert : function(sqlStatement)
  45. {
  46. return new Promise(function(resolve, reject)
  47. {
  48. con.query(sanitizer.sanitize(sqlStatement), function (err, result)
  49. {
  50. if (err)
  51. {
  52. console.log(err);
  53. resolve(0);
  54. }
  55. resolve(result.insertId);
  56. });
  57. })
  58. },
  59. /**
  60. * Not to be mistaken for getPostData() in @file utils/utils.js,
  61. * this function extracts a post entry from the sql server
  62. *
  63. * @param requestURL url user used to request blog post
  64. * @return {*} the entry found in the data base -- if any
  65. */
  66. getPost : function(requestURL)
  67. {
  68. return new Promise(function(resolve, reject)
  69. {
  70. var splitURL = requestURL.split("/")
  71. var q = "select * from categories where url='" + splitURL[1] + "'";
  72. fetch(q).then(function (result_category)
  73. {
  74. if(result_category.length != 0)
  75. {
  76. var q2 = "select * from posts where category_id='" + result_category[0].category_id +
  77. "' and url='" + splitURL[2] + "'";
  78. fetch(q2).then(function (result_posts)
  79. {
  80. if(result_posts != 0)
  81. {
  82. resolve(result_posts[0]);
  83. }
  84. else
  85. {
  86. resolve(0);
  87. }
  88. });
  89. }
  90. else
  91. {
  92. resolve(0);
  93. }
  94. });
  95. });
  96. },
  97. /**
  98. * Function used to retrieve all categories when making the sidebar
  99. *
  100. * @return {Promise<Response> | * | Array}
  101. */
  102. getCategories : function()
  103. {
  104. var q = "select * from categories";
  105. return fetch(q);
  106. },
  107. getPostsFromCategory: function(requestURL)
  108. {
  109. return new Promise(function(resolve, reject)
  110. {
  111. var q = "select * from categories where name ='" + requestURL + "' limit 1";
  112. fetch(q).then(function(categories)
  113. {
  114. if(categories.length != 0)
  115. {
  116. var qPosts = "select * from posts where category_id='" + categories[0].category_id + "'";
  117. resolve(fetch(qPosts));
  118. }
  119. else
  120. {
  121. resolve(0);
  122. }
  123. });
  124. });
  125. }
  126. };