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.

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