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.

211 lines
6.9 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=\"w3-teal w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align\"/>\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='w3-row'>");
  44. result.write("<h1 class=\"w3-text-teal w3-center\">Posts</h1>");
  45. result.write("<div class=\"w3-responsive w3-card-4\"><table class=\"w3-table w3-striped w3-bordered\"><thead>");
  46. result.write("<tr class=\"w3-teal\">");
  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>");
  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='w3-row'>"+
  97. "<h1 class=\"w3-text-teal w3-center\">Edit Post</h1>"+
  98. "<form action=\"/admin/\" method =\"post\" class=\"w3-container w3-card-4\">"+
  99. " <div class=\"w3-group w3-padding-16\">\n" +
  100. " <input class=\"w3-input\" 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=\"w3-group w3-padding-16\">\n" +
  104. " <input class=\"w3-input\" 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=\"w3-group w3-padding-16\">\n" +
  108. " <input class=\"w3-input\" 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=\"w3-group w3-padding-16\">\n" +
  112. " <input class=\"w3-input\" 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. " <p><input type=\"submit\" name=\"submit\" value=\"Edit\"\n" +
  116. " class=\"w3-teal w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align\"/></p>"+
  117. "<input type='hidden' name='edit_post_2' value='" + post_id + "'/>"+
  118. "</form>"+
  119. "</div>"
  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. processPost(result, postData).then(function()
  183. {
  184. return postsTable(result);
  185. }).then(function()
  186. {
  187. resolve();
  188. }).catch(function(error)
  189. {
  190. console.log("Error in edit post module");
  191. reject(error);
  192. });
  193. });
  194. }
  195. };