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.

123 lines
3.4 KiB

  1. /** Whiskers template file
  2. * this has stuff for both editing blog and viewing a list of blog*/
  3. const TEMPLATE_FILE = "admin/adminPosts.html";
  4. const includes = require('../includes/includes.js');
  5. const sql = require('../utils/sql');
  6. //parses the post data
  7. const qs = require('querystring');
  8. const utils = require('../utils/utils');
  9. /**
  10. * Detects if the post data came from the edit form in blog table or edit post
  11. * in the edit post form.
  12. *
  13. * @param postData
  14. * @param renderContext
  15. * @returns {Promise}
  16. */
  17. const processPostData = function(postData, renderContext)
  18. {
  19. return new Promise(function(resolve, reject)
  20. {
  21. var postParsed = qs.parse(postData);
  22. if(postParsed.edit_post)
  23. {
  24. renderContext.editPost = true;
  25. sql.getPostById(postParsed.edit_post).then(function(post)
  26. {
  27. post.published = post.published.toISOString().split('T')[0];
  28. renderContext.post = post;
  29. resolve();
  30. });
  31. }
  32. else if(postParsed.edit_post_2)
  33. {
  34. sql.editPost(postParsed).then(function()
  35. {
  36. resolve();
  37. }).catch(function(error)
  38. {
  39. reject(error);
  40. });
  41. }
  42. else
  43. {
  44. resolve();
  45. }
  46. });
  47. };
  48. /**
  49. * Grabs and appends the list of blog from the SQL database to
  50. * the template context for the template renderer.
  51. *
  52. * @param templateContext
  53. * @returns {Promise}
  54. */
  55. const fetchPostsInformation = function(templateContext)
  56. {
  57. return new Promise(function(resolve, reject)
  58. {
  59. sql.getAllPosts().then(function(posts)
  60. {
  61. templateContext.posts = posts;
  62. resolve();
  63. }).catch(function(error)
  64. {
  65. reject(error);
  66. })
  67. });
  68. };
  69. module.exports=
  70. {
  71. /**
  72. * Fetches context information for the admin blog page and handles post
  73. * data sent regarding editing blog.
  74. *
  75. * @param postData posted by user
  76. * @param templateContext json object used as the template context
  77. * @returns {Promise} renders the template used for this page
  78. */
  79. main: function(templateContext)
  80. {
  81. return new Promise(function(resolve, reject)
  82. {
  83. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  84. fetchPostsInformation(templateContext)]).then(function(template)
  85. {
  86. templateContext.adminPage = template[0];
  87. resolve();
  88. }).catch(function(error)
  89. {
  90. console.log("error in add admin posts.js");
  91. reject(error);
  92. });
  93. });
  94. },
  95. processPostData(templateContext, postData)
  96. {
  97. return new Promise(function(resolve, reject)
  98. {
  99. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  100. processPostData(postData, templateContext),
  101. fetchPostsInformation(templateContext)]).then(function(template)
  102. {
  103. templateContext.adminPage = template[0];
  104. resolve();
  105. }).catch(function(error)
  106. {
  107. console.log("error in add admin posts.js");
  108. reject(error);
  109. });
  110. });
  111. }
  112. };