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.

97 lines
2.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //file io
  2. const utils = require('../../utils/utils.js');
  3. //update db
  4. const sql = require('../../utils/sql');
  5. //parse post data
  6. const qs = require('querystring');
  7. /**
  8. * Displays all the categories in the database
  9. * @return {*|Promise}
  10. */
  11. const printCategories = function()
  12. {
  13. var html = "<div class=\"blogPost\">" +
  14. "<h1 class=\"text-center\">Categories</h1>" +
  15. "<div class=\"\"><table class=\"table table-striped\">" +
  16. "<thead class=\"thead-dark\">" +
  17. "<tr>" +
  18. "<td>Name</td><td>URL</td><td>Edit</td>" +
  19. "</tr></thead><tbody>";
  20. return new Promise(function(resolve, reject)
  21. {
  22. sql.getCategories().then(function(categories)
  23. {
  24. categories.forEach(function(c)
  25. {
  26. html +="<tr>" +
  27. "<td>" + c.name + "</td>" +
  28. "<td>" + c.url + "</td>" +
  29. "<td>" + c.category_id + "</td>" +
  30. "</tr>";
  31. });
  32. resolve(html + "</tbody></table></div></div>");
  33. }).catch(function(error)
  34. {
  35. reject(error);
  36. })
  37. });
  38. };
  39. /**
  40. * Checks for post data regarding adding a new category.
  41. * If a post is made with add_category, it parses the url-- replaces spaces
  42. * with dashes -- and calls a insert method on the database
  43. *
  44. * @param postData
  45. * @return {*|Promise}
  46. */
  47. const processPost = function(postData)
  48. {
  49. return new Promise(function(resolve, reject)
  50. {
  51. const post = qs.parse(postData);
  52. if(post.add_category)
  53. {
  54. const url = post.add_category.split(" ").join("-").toLowerCase();
  55. const q = "insert into categories (name, url) values " +
  56. "('" + post.add_category + "','" + url + "')";
  57. if(sql.insert(q) != 0)
  58. {
  59. console.log("category added");
  60. }
  61. else
  62. {
  63. console.log("error adding category");
  64. }
  65. }
  66. resolve("");
  67. });
  68. };
  69. module.exports=
  70. {
  71. main: function(postData)
  72. {
  73. return new Promise(function(resolve, reject)
  74. {
  75. Promise.all([utils.include("./admin/category/addCategory.html"),
  76. printCategories(),
  77. processPost(postData)]).then(function(html)
  78. {
  79. resolve("<div class=\"col-md-6\">" +
  80. html.join('') +
  81. "</div></div>");
  82. }).catch(function(error)
  83. {
  84. console.log("error in addCategory.js");
  85. reject(error);
  86. })
  87. });
  88. }
  89. };