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.

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