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.

181 lines
5.5 KiB

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