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.

160 lines
4.3 KiB

  1. const TEMPLATE_FILE = "admin/adminHome.html";
  2. const includes = require('../includes/includes.js');
  3. const sql = require('../utils/sql');
  4. const qs = require('querystring');
  5. /**
  6. * Checks for post data regarding adding a new category.
  7. * If a post is made with add_category, it parses the url-- replaces spaces
  8. * with dashes -- and calls a insert method on the database
  9. *
  10. * @param postData
  11. * @return {*|Promise}
  12. */
  13. const processPostAddCategory = function(postData)
  14. {
  15. return new Promise(function(resolve, reject)
  16. {
  17. const post = qs.parse(postData);
  18. if(post.add_category)
  19. {
  20. const url = post.add_category.split(" ").join("-").toLowerCase();
  21. const q = "insert into categories (name, url) values " +
  22. "('" + post.add_category + "','" + url + "')";
  23. if(sql.insert(q) != 0)
  24. {
  25. console.log("category added");
  26. resolve();
  27. }
  28. else
  29. {
  30. reject();
  31. console.log("error adding category");
  32. }
  33. }
  34. else
  35. {
  36. resolve();
  37. }
  38. });
  39. };
  40. /**
  41. * Displays all the categories in the database
  42. * @return {*|Promise}
  43. */
  44. const appendCategoriesToTemplate = function(templateContext)
  45. {
  46. return new Promise(function(resolve, reject)
  47. {
  48. sql.getCategories().then(function(categories)
  49. {
  50. templateContext.categories = categories;
  51. resolve();
  52. }).catch(function(error)
  53. {
  54. reject(error);
  55. })
  56. });
  57. };
  58. /**
  59. *
  60. * @param postData
  61. * @return {*|Promise}
  62. */
  63. const processPost = function(postData)
  64. {
  65. return new Promise(function(resolve, reject)
  66. {
  67. var post = qs.parse(postData);
  68. if(post.add_post_name)
  69. {
  70. var urls = post.add_post_name;
  71. urls = urls.split(" ").join("-");
  72. urls =urls.toLowerCase();
  73. var q = "insert into posts (category_id, picture_url, published, name, url) values ";
  74. q += "('" + post.add_post_category + "', '" + post.add_post_picture +
  75. "', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')";
  76. sql.insert(q).then(function()
  77. {
  78. var map = require('../utils/generateSiteMap');
  79. map.main();
  80. resolve();
  81. }).catch(function(error)
  82. {
  83. reject(error);
  84. })
  85. }
  86. else if(post.clear_cache)
  87. {
  88. require("../sites/blog.js").clearCache();
  89. require("../includes/includes.js").clearCache();
  90. resolve();
  91. }
  92. else if(post.git_pull)
  93. {
  94. const execSync = require('child_process').execSync;
  95. code = execSync('git pull');
  96. resolve();
  97. }
  98. else
  99. {
  100. resolve();
  101. }
  102. });
  103. };
  104. module.exports=
  105. {
  106. /**
  107. *
  108. * @param postData posted by user
  109. * @param templateContext json object used as the template context
  110. * @returns {Promise} renders the template used for this page
  111. */
  112. main: function(templateContext)
  113. {
  114. return new Promise(function(resolve, reject)
  115. {
  116. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  117. appendCategoriesToTemplate(templateContext)])
  118. .then(function(template)
  119. {
  120. templateContext.adminPage = template[0];
  121. resolve();
  122. }).catch(function(error)
  123. {
  124. console.log("error in add adminHome.js");
  125. reject(error);
  126. });
  127. });
  128. },
  129. processPostData: function(postData)
  130. {
  131. return new Promise(function(resolve, reject)
  132. {
  133. Promise.all([processPostAddCategory(postData),
  134. processPost(postData)])
  135. .then(function()
  136. {
  137. console.log("all resolved");
  138. resolve();
  139. }).catch(function(error)
  140. {
  141. console.log("error in add downloads.js");
  142. reject(error);
  143. });
  144. });
  145. }
  146. };