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.

142 lines
3.2 KiB

  1. /**
  2. * File which deals with adding and removing downloads from
  3. * the admin section of the website.
  4. *
  5. * @author Jeffery Russell 6-30-18
  6. */
  7. /** Whiskers template file */
  8. const TEMPLATE_FILE = "admin/adminDownloads.html";
  9. const includes = require('../includes/includes.js');
  10. //updates db
  11. const sql = require('../utils/sql');
  12. //parses post data
  13. const qs = require('querystring');
  14. /**
  15. * Processes post requests from the addDownload form
  16. *
  17. * @param postData
  18. * @returns {*|Promise}
  19. */
  20. const addDownloadPostData = function(postData)
  21. {
  22. return new Promise(function(resolve, reject)
  23. {
  24. const post = qs.parse(postData);
  25. if(post.add_download)
  26. {
  27. sql.addDownload(post.add_download_name, post.add_download_file)
  28. .then(function()
  29. {
  30. resolve();
  31. }).catch(function(error)
  32. {
  33. reject(error);
  34. })
  35. }
  36. else
  37. {
  38. resolve();
  39. }
  40. });
  41. };
  42. /**
  43. * Removes a download if requested by the
  44. * post data from an admin.
  45. */
  46. const removeDownloads = function(postData)
  47. {
  48. return new Promise(function(resolve, reject)
  49. {
  50. const post = qs.parse(postData);
  51. if(post.delete_download)
  52. {
  53. sql.removeDownload(post.delete_download).then(function()
  54. {
  55. resolve();
  56. }).catch(function(err)
  57. {
  58. reject(err);
  59. });
  60. }
  61. else
  62. {
  63. resolve();
  64. }
  65. });
  66. };
  67. /**
  68. * Fetches the download items in the database so that
  69. * the template engine can use it to display them in
  70. * a table.
  71. *
  72. * @param templateContext-- context item used by whiskers
  73. * @returns {Promise}
  74. */
  75. const displayDownloads = function(templateContext)
  76. {
  77. return new Promise(function(resolve, reject)
  78. {
  79. sql.getAllDownloads().then(function(downloads)
  80. {
  81. templateContext.downloads = downloads;
  82. resolve();
  83. }).catch(function(error)
  84. {
  85. reject(error);
  86. });
  87. });
  88. };
  89. module.exports=
  90. {
  91. /** Fetches context information for the template and handles
  92. * post data for the downloads.
  93. *
  94. * @param postData posted by user
  95. * @param templateContext json object used as the template context
  96. * @returns {Promise} renders the template used for this page
  97. */
  98. main: function(templateContext)
  99. {
  100. return new Promise(function(resolve, reject)
  101. {
  102. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  103. displayDownloads(templateContext)]).then(function(template)
  104. {
  105. resolve(template[0]);
  106. }).catch(function(error)
  107. {
  108. console.log("error in add downloads.js");
  109. reject(error);
  110. });
  111. });
  112. },
  113. processPostData: function(postData, templateContext)
  114. {
  115. return new Promise(function(resolve, reject)
  116. {
  117. Promise.all([addDownloadPostData(postData),
  118. removeDownloads(postData)]).then(function()
  119. {
  120. resolve();
  121. }).catch(function(error)
  122. {
  123. console.log(error);
  124. reject(error);
  125. })
  126. })
  127. }
  128. };