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.

87 lines
2.2 KiB

  1. const sql = require('../utils/sql');
  2. const TEMPLATE_FILE = "blog/sideBar.html";
  3. const includes = require('../includes/includes.js');
  4. const getInformationForRecentPosts = function(templateContext)
  5. {
  6. return new Promise(function(resolve, reject)
  7. {
  8. sql.getRecentPosts().then(function(posts)
  9. {
  10. posts.forEach(function(p)
  11. {
  12. p.url = '/' + p.category + '/' + p.url;
  13. });
  14. templateContext.recentPosts = posts;
  15. resolve();
  16. }).catch(function(error)
  17. {
  18. reject(error);
  19. })
  20. });
  21. };
  22. const getInformationForCategories = function(templateContext)
  23. {
  24. return new Promise(function(resolve, reject)
  25. {
  26. sql.getCategories().then(function(categories)
  27. {
  28. categories.forEach(function(cat)
  29. {
  30. cat.url = "/category/" + cat.url;
  31. });
  32. templateContext.categories = categories;
  33. resolve();
  34. }).catch(function(error)
  35. {
  36. reject(error);
  37. });
  38. });
  39. };
  40. const getInformationForPinnedPosts = function(templateContext)
  41. {
  42. return new Promise(function(resolve, reject)
  43. {
  44. sql.getPinnedPosts().then(function(posts)
  45. {
  46. posts.forEach(function(p)
  47. {
  48. p.url = '/' + p.category + '/' + p.url;
  49. });
  50. templateContext.pinnedPosts = posts;
  51. resolve();
  52. }).catch(function(error)
  53. {
  54. reject(error);
  55. })
  56. });
  57. };
  58. module.exports=
  59. {
  60. main: function(templateContext)
  61. {
  62. return new Promise(function(resolve, reject)
  63. {
  64. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  65. getInformationForRecentPosts(templateContext),
  66. getInformationForPinnedPosts(templateContext),
  67. getInformationForCategories(templateContext)])
  68. .then(function(content)
  69. {
  70. templateContext.sideBar = content[0];
  71. resolve();
  72. }).catch(function(error)
  73. {
  74. reject(error);
  75. });
  76. })
  77. }
  78. };