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.

212 lines
6.7 KiB

  1. /** File which renders the edit form for the posts and processes
  2. * the post data generated by edit forms.
  3. *
  4. * @type {Promise|*}
  5. */
  6. const Promise = require('promise');
  7. const qs = require('querystring');
  8. const sql = require('../utils/sql');
  9. /**
  10. * Displays a single row in the posts view
  11. * @param result
  12. * @param post
  13. */
  14. var renderPostRow = function(result, post)
  15. {
  16. return new Promise(function(resolve, reject)
  17. {
  18. result.write("<tr>");
  19. //category
  20. result.write("<td>" + post.category_id + "</td>");
  21. //name
  22. result.write("<td>" + post.name + "</td>");
  23. //picture
  24. result.write("<td>" + post.picture_url + "</td>");
  25. //date
  26. result.write("<td>" + post.published + "</td>");
  27. //edit
  28. result.write("<td><form action=\"/admin/\" method =\"post\" >\n" +
  29. " <input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
  30. " class=\"btn btn-secondary\"/>\n" +
  31. "<input type='hidden' name='edit_post' value='" + post.post_id + "'/>"+
  32. "</form></td>");
  33. result.write("</tr>");
  34. resolve();
  35. });
  36. };
  37. /**
  38. * Displays all the posts in a table
  39. * @param result
  40. */
  41. var postsTable = function(result)
  42. {
  43. result.write("<div class='blogPost p-2'>");
  44. result.write("<h1 class=\"text-center\">Posts</h1>");
  45. result.write("<div class=\"\"><table class=\"table table-striped\">");
  46. result.write("<thead class=\"thead-dark\"><tr>");
  47. result.write("<td>Category #</td><td>Name</td><td>Header Picture</td><td>Date</td><td>Edit</td>");
  48. result.write("</tr></thead><tbody>");
  49. return new Promise(function(resolve, reject)
  50. {
  51. sql.getAllPosts().then(function(posts)
  52. {
  53. var postPromises = [];
  54. posts.forEach(function(post)
  55. {
  56. postPromises.push(new Promise(function(res, rej)
  57. {
  58. renderPostRow(result, post).then(function()
  59. {
  60. res();
  61. }).catch(function(error)
  62. {
  63. console.log("error rendering " + post);
  64. rej(error);
  65. })
  66. }));
  67. });
  68. Promise.all(postPromises).then(function()
  69. {
  70. result.write("</tbody></table></div></div><br>");
  71. resolve();
  72. }).catch(function(error)
  73. {
  74. console.log(error);
  75. console.log("error rendering posts");
  76. reject(error);
  77. });
  78. }).catch(function(error)
  79. {
  80. console.log("error with sql query");
  81. reject(error);
  82. })
  83. });
  84. };
  85. /**
  86. * Displays the edit form for edit posts
  87. * @param result
  88. * @param post_id
  89. */
  90. var displayRenderForm = function(result, post_id)
  91. {
  92. return new Promise(function(resolve, reject)
  93. {
  94. sql.getPostById(post_id).then(function(post)
  95. {
  96. result.write("<div class='blogPost p-2'>"+
  97. "<h1 class=\"text-center\">Edit Post</h1>"+
  98. "<form action=\"/admin/\" method =\"post\" >"+
  99. " <div class=\"form-group\">\n" +
  100. " <input class=\"form-control\" type=\"text\" name=\"edit_cat_num\" value='" + post.category_id + "' required>\n" +
  101. " <label class=\"w3-label w3-validate\">Category Number</label>\n" +
  102. " </div>"+
  103. " <div class=\"form-group\">\n" +
  104. " <input class=\"form-control\" type=\"text\" name=\"edit_name_new\" value='" + post.name + "' required>\n" +
  105. " <label class=\"w3-label w3-validate\">Post Title</label>\n" +
  106. " </div>"+
  107. " <div class=\"form-group\">\n" +
  108. " <input class=\"form-control\" type=\"text\" name=\"edit_pic\" value='" + post.picture_url + "' required>\n" +
  109. " <label class=\"w3-label w3-validate\">Picture URL</label>\n" +
  110. " </div>"+
  111. " <div class=\"form-group\">\n" +
  112. " <input class=\"form-control\" type=\"date\" name=\"edit_date\" value='" + post.published.toISOString().split('T')[0] + "' required>\n" +
  113. " <label class=\"w3-label w3-validate\">Published Date</label>\n" +
  114. " </div>"+
  115. " <div><input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
  116. " class=\"btn btn-lg btn-secondary\"/></div>"+
  117. "<input type='hidden' name='edit_post_2' value='" + post_id + "'/>"+
  118. "</form>"+
  119. "</div><br>"
  120. );
  121. resolve();
  122. }).catch(function(error)
  123. {
  124. console.log(error);
  125. console.log("error getting post from sql in display Reender Form");
  126. reject(error);
  127. });
  128. });
  129. };
  130. /**
  131. * Detects if the post data came from the edit form in posts table or edit post
  132. * in the edit post form. Based on this, this function will call one of two functions
  133. * @param result
  134. * @param postData
  135. */
  136. var processPost = function(result, postData)
  137. {
  138. return new Promise(function(resolve, reject)
  139. {
  140. var postParsed = qs.parse(postData);
  141. if(postParsed.edit_post)
  142. {
  143. //display edit form
  144. displayRenderForm(result, postParsed.edit_post).then(function()
  145. {
  146. resolve();
  147. }).catch(function(error)
  148. {
  149. console.log(error);
  150. console.log("error processing the edit post data");
  151. });
  152. }
  153. else if(postParsed.edit_post_2)
  154. {
  155. //insert edit into sql
  156. sql.editPost(postParsed).then(function()
  157. {
  158. resolve();
  159. }).catch(function(error)
  160. {
  161. console.log("error inserting edit post data into sql");
  162. });
  163. }
  164. else
  165. {
  166. resolve();
  167. }
  168. });
  169. };
  170. module.exports=
  171. {
  172. /**
  173. * Method which calls helper functions which processes post data for editing posts
  174. * and calls a function which displays all the posts in a table
  175. * @param result
  176. * @param postData
  177. */
  178. main: function(result, postData)
  179. {
  180. return new Promise(function(resolve, reject)
  181. {
  182. result.write("<br>");
  183. processPost(result, postData).then(function()
  184. {
  185. return postsTable(result);
  186. }).then(function()
  187. {
  188. resolve();
  189. }).catch(function(error)
  190. {
  191. console.log("Error in edit post module");
  192. reject(error);
  193. });
  194. });
  195. }
  196. };