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.

137 lines
3.7 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. }
  27. else
  28. {
  29. console.log("error adding category");
  30. }
  31. }
  32. resolve("");
  33. });
  34. };
  35. /**
  36. * Displays all the categories in the database
  37. * @return {*|Promise}
  38. */
  39. const appendCategoriesToTemplate = function(templateContext)
  40. {
  41. return new Promise(function(resolve, reject)
  42. {
  43. sql.getCategories().then(function(categories)
  44. {
  45. templateContext.categories = categories;
  46. resolve();
  47. }).catch(function(error)
  48. {
  49. reject(error);
  50. })
  51. });
  52. };
  53. /**
  54. *
  55. * @param postData
  56. * @return {*|Promise}
  57. */
  58. const processPost = function(postData)
  59. {
  60. return new Promise(function(resolve, reject)
  61. {
  62. var post = qs.parse(postData);
  63. if(post.add_post_name)
  64. {
  65. var urls = post.add_post_name;
  66. urls = urls.split(" ").join("-");
  67. urls =urls.toLowerCase();
  68. var q = "insert into blog (category_id, picture_url, published, name, url) values ";
  69. q += "('" + post.add_post_category + "', '" + post.add_post_picture +
  70. "', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')";
  71. sql.insert(q).then(function()
  72. {
  73. var map = require('../utils/generateSiteMap');
  74. map.main();
  75. resolve("");
  76. }).catch(function(error)
  77. {
  78. reject(error);
  79. })
  80. }
  81. else if(post.clear_cache)
  82. {
  83. require("../sites/blog.js").clearCache();
  84. require("../includes/includes.js").clearCache();
  85. }
  86. else if(post.git_pull)
  87. {
  88. const execSync = require('child_process').execSync;
  89. code = execSync('git pull')
  90. }
  91. else
  92. {
  93. resolve("");
  94. }
  95. });
  96. };
  97. module.exports=
  98. {
  99. /**
  100. *
  101. * @param postData posted by user
  102. * @param templateContext json object used as the template context
  103. * @returns {Promise} renders the template used for this page
  104. */
  105. main: function(postData, templateContext)
  106. {
  107. console.log("called");
  108. return new Promise(function(resolve, reject)
  109. {
  110. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  111. processPostAddCategory(postData),
  112. appendCategoriesToTemplate(templateContext),
  113. processPost(postData)])
  114. .then(function(template)
  115. {
  116. resolve(template[0]);
  117. }).catch(function(error)
  118. {
  119. console.log("error in add downloads.js");
  120. reject(error);
  121. });
  122. });
  123. }
  124. };