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.

190 lines
4.6 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. //file IO
  8. const utils = require('../../utils/utils.js');
  9. //updates db
  10. const sql = require('../../utils/sql');
  11. //parses post data
  12. const qs = require('querystring');
  13. /**
  14. * Processes post requests from the addDownload form
  15. *
  16. * @param postData
  17. * @returns {*|Promise}
  18. */
  19. const addDownloadPostData = function(postData)
  20. {
  21. return new Promise(function(resolve, reject)
  22. {
  23. const post = qs.parse(postData);
  24. if(post.add_download)
  25. {
  26. sql.addDownload(post.add_download_name, post.add_download_file)
  27. .then(function()
  28. {
  29. resolve("");
  30. }).catch(function(error)
  31. {
  32. reject(error);
  33. })
  34. }
  35. else
  36. {
  37. resolve("");
  38. }
  39. });
  40. };
  41. /**
  42. * Displays the addDownload form the the user
  43. *
  44. * @param postData
  45. * @returns {*|Promise}
  46. */
  47. const addDownload = function(postData)
  48. {
  49. return new Promise(function(resolve, reject)
  50. {
  51. Promise.all([addDownloadPostData(postData),
  52. utils.include("./admin/downloads/addDownload.html")]).then(function(html)
  53. {
  54. resolve("<div class=\"col-md-6\">" + html.join('') + "</div>");
  55. }).catch(function(error)
  56. {
  57. reject(error);
  58. })
  59. });
  60. };
  61. /**
  62. * Handel form requests from the downloads table
  63. *
  64. * @param postData
  65. * @returns {*|Promise}
  66. */
  67. const displayDownloadsPostData = function(postData)
  68. {
  69. return new Promise(function(resolve, reject)
  70. {
  71. const post = qs.parse(postData);
  72. if(post.delete_download)
  73. {
  74. sql.removeDownload(post.delete_download).then(function()
  75. {
  76. resolve(postData);
  77. }).catch(function(err)
  78. {
  79. reject(err);
  80. });
  81. }
  82. else
  83. {
  84. resolve(postData);
  85. }
  86. });
  87. };
  88. /**
  89. * Renders a single download row in the downloads table
  90. *
  91. * @param download
  92. * @returns {*|Promise}
  93. */
  94. const renderDownloadRow = function(download)
  95. {
  96. return "<tr>" +
  97. "<td>" + download.name + "</td>" +
  98. "<td>" + download.file + "</td>" +
  99. "<td>" + download.download_count + "</td>" +
  100. "<td><form action=\"/admin\" method =\"post\" >\n" +
  101. " <input type=\"submit\" name=\"submit\" value=\"Delete\"\n" +
  102. " class=\"btn btn-secondary\"/>\n" +
  103. "<input type='hidden' name='delete_download' value='" +
  104. download.download_id + "'/>"+
  105. "</form></td>" +
  106. "</tr>";
  107. };
  108. /**
  109. * Displays all the download information in a table
  110. * @param postData
  111. * @returns {*|Promise}
  112. */
  113. const displayDownloads = function(postData)
  114. {
  115. var html = "<div class=\"col-md-6\">";
  116. return new Promise(function(resolve, reject)
  117. {
  118. displayDownloadsPostData(postData).then(function()
  119. {
  120. html += "<div class='blogPost'>" +
  121. "<h1 class=\"text-center\">Downloads</h1>" +
  122. "<div class=\"\"><table class=\"table table-striped\">" +
  123. "<thead class=\"thead-dark\"><tr>" +
  124. "<td>Download Name</td><td>File</td>" +
  125. "<td>Download Count</td><td>Delete</td>" +
  126. "</tr></thead><tbody>";
  127. sql.getAllDownloads().then(function(downloads)
  128. {
  129. var downloadPromises = [];
  130. downloads.forEach(function(download)
  131. {
  132. downloadPromises.push(renderDownloadRow(download));
  133. });
  134. Promise.all(downloadPromises).then(function(htmls)
  135. {
  136. const htmlafter = "</tbody></table></div></div><br>" +
  137. "</div>";
  138. resolve(html + htmls.join('') + htmlafter);
  139. });
  140. }).catch(function(error)
  141. {
  142. reject(error);
  143. });
  144. });
  145. });
  146. };
  147. module.exports=
  148. {
  149. /**
  150. * Renders tha download section of the admin page
  151. *
  152. * @param postData
  153. * @returns {Promise}
  154. */
  155. main: function(postData)
  156. {
  157. return new Promise(function(resolve, reject)
  158. {
  159. Promise.all([addDownload(postData),
  160. displayDownloads(postData)]).then(function(html)
  161. {
  162. resolve("<div class=\"row\">" + html.join('') + "</div>");
  163. }).catch(function(error)
  164. {
  165. console.log("error in add downloads.js");
  166. reject(error);
  167. });
  168. });
  169. }
  170. };