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.

128 lines
2.9 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(postData, templateContext)
  99. {
  100. return new Promise(function(resolve, reject)
  101. {
  102. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  103. addDownloadPostData(postData),
  104. removeDownloads(postData),
  105. displayDownloads(templateContext)]).then(function(template)
  106. {
  107. resolve(template[0]);
  108. }).catch(function(error)
  109. {
  110. console.log("error in add downloads.js");
  111. reject(error);
  112. });
  113. });
  114. }
  115. };