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.

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