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.

130 lines
2.8 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. const TEMPLATE_FILE = "admin/adminDownloads.html";
  8. //file IO
  9. const utils = require('../utils/utils.js');
  10. const includes = require('../includes/includes.js');
  11. //updates db
  12. const sql = require('../utils/sql');
  13. //parses post data
  14. const qs = require('querystring');
  15. /**
  16. * Processes post requests from the addDownload form
  17. *
  18. * @param postData
  19. * @returns {*|Promise}
  20. */
  21. const addDownloadPostData = function(postData)
  22. {
  23. return new Promise(function(resolve, reject)
  24. {
  25. const post = qs.parse(postData);
  26. if(post.add_download)
  27. {
  28. sql.addDownload(post.add_download_name, post.add_download_file)
  29. .then(function()
  30. {
  31. resolve();
  32. }).catch(function(error)
  33. {
  34. reject(error);
  35. })
  36. }
  37. else
  38. {
  39. resolve();
  40. }
  41. });
  42. };
  43. /**
  44. * Handel form requests from the downloads table
  45. *
  46. * @param postData
  47. * @returns {*|Promise}
  48. */
  49. const removeDownloads = function(postData)
  50. {
  51. return new Promise(function(resolve, reject)
  52. {
  53. const post = qs.parse(postData);
  54. if(post.delete_download)
  55. {
  56. sql.removeDownload(post.delete_download).then(function()
  57. {
  58. resolve();
  59. }).catch(function(err)
  60. {
  61. reject(err);
  62. });
  63. }
  64. else
  65. {
  66. resolve();
  67. }
  68. });
  69. };
  70. /**
  71. * Displays all the download information in a table
  72. * @param postData
  73. * @returns {*|Promise}
  74. */
  75. const displayDownloads = function(postData, 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. /**
  92. * Renders tha download section of the admin page
  93. *
  94. * @param postData
  95. * @returns {Promise}
  96. */
  97. main: function(postData, templateContext)
  98. {
  99. console.log(postData);
  100. console.log("downloads page called");
  101. return new Promise(function(resolve, reject)
  102. {
  103. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  104. addDownloadPostData(postData),
  105. removeDownloads(postData),
  106. displayDownloads(postData, templateContext)]).then(function(template)
  107. {
  108. resolve(template[0]);
  109. }).catch(function(error)
  110. {
  111. console.log("error in add downloads.js");
  112. reject(error);
  113. });
  114. });
  115. }
  116. };