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.

171 lines
4.3 KiB

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