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.

174 lines
4.7 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. /**
  108. * Function which currently returns all posts of a particular category from the database
  109. * @param requestURL
  110. * @return {*|Promise}
  111. */
  112. getPostsFromCategory: function(requestURL)
  113. {
  114. return new Promise(function(resolve, reject)
  115. {
  116. var q = "select * from categories where name ='" + requestURL + "' limit 1";
  117. fetch(q).then(function(categories)
  118. {
  119. if(categories.length != 0)
  120. {
  121. var qPosts = "select * from posts where category_id='" + categories[0].category_id + "'";
  122. resolve(fetch(qPosts));
  123. }
  124. else
  125. {
  126. resolve(0);
  127. }
  128. });
  129. });
  130. },
  131. /**
  132. * Helper method which returns a list of objects which contains the url and name of thee ten most recent posts
  133. *
  134. * {[name: , url: ],[name: , url: ],[name: , url: ],...}
  135. *
  136. * @return {*|Promise}
  137. */
  138. getRecentPosts: function()
  139. {
  140. return new Promise(function(resolve, reject)
  141. {
  142. var q = "select name, category_id from posts order by post_id desc limit 10";
  143. fetch(q).then(function(sqlPosts)
  144. {
  145. var posts = [];
  146. sqlPosts.forEach(function(p)
  147. {
  148. var getCategory = "select url from categories where category_id='" + p.category_id + "'";
  149. fetch(getCategory).then(function(url)
  150. {
  151. var obj = new Object();
  152. obj.name = p.name;
  153. obj.category = url.url;
  154. posts.push(obj);
  155. })
  156. });
  157. resolve(posts);
  158. });
  159. });
  160. }
  161. };