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.

192 lines
4.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. /** Whiskers template file */
  8. const TEMPLATE_FILE = "admin/adminUsers.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 addUserPostData = function(postData)
  21. {
  22. return new Promise(function(resolve, reject)
  23. {
  24. const post = qs.parse(postData);
  25. if(post.add_user)
  26. {
  27. sql.addUser(post.add_user_name, post.add_user_password)
  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 removeUserPost = function(postData)
  47. {
  48. return new Promise(function(resolve, reject)
  49. {
  50. const post = qs.parse(postData);
  51. console.log(post);
  52. if(post.delete_user)
  53. {
  54. console.log("Removing user: " + post.delete_user);
  55. sql.removeUser(post.delete_user).then(function()
  56. {
  57. resolve();
  58. }).catch(function(err)
  59. {
  60. reject(err);
  61. });
  62. }
  63. else
  64. {
  65. resolve();
  66. }
  67. });
  68. };
  69. /**
  70. * Processes post data to determine if the user requested that
  71. * a user be updated in the database.
  72. */
  73. const editUserPost = function(postData, templateContext)
  74. {
  75. return new Promise(function(resolve, reject)
  76. {
  77. const post = qs.parse(postData);
  78. if(post.edit_user)
  79. {
  80. sql.getUserByID(post.edit_user).then(function(user)
  81. {
  82. if(user.length == 1)
  83. {
  84. templateContext.edit_user = post.edit_user;
  85. templateContext.user_name = user[0].user_name;
  86. resolve();
  87. }
  88. else
  89. {
  90. resolve();
  91. }
  92. }).catch(function(err)
  93. {
  94. reject(err);
  95. });
  96. }
  97. else if(post.edit_user_2)
  98. {
  99. sql.updateUser(post.edit_user_2, post.edit_user_name, post.edit_user_password)
  100. .then(function()
  101. {
  102. resolve();
  103. })
  104. }
  105. else
  106. {
  107. resolve();
  108. }
  109. });
  110. };
  111. /**
  112. * Fetches the download items in the database so that
  113. * the template engine can use it to display them in
  114. * a table.
  115. *
  116. * @param templateContext-- context item used by whiskers
  117. * @returns {Promise}
  118. */
  119. const getUserInformation = function(templateContext)
  120. {
  121. return new Promise(function(resolve, reject)
  122. {
  123. sql.getAllUsers().then(function(users)
  124. {
  125. templateContext.users = users;
  126. resolve();
  127. }).catch(function(error)
  128. {
  129. reject(error);
  130. });
  131. });
  132. };
  133. module.exports=
  134. {
  135. /** Fetches context information for the template and handles
  136. * post data for the downloads.
  137. *
  138. * @param postData posted by user
  139. * @param templateContext json object used as the template context
  140. * @returns {Promise} renders the template used for this page
  141. */
  142. main: function(templateContext)
  143. {
  144. return new Promise(function(resolve, reject)
  145. {
  146. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  147. getUserInformation(templateContext)]).then(function(template)
  148. {
  149. templateContext.adminPage = template[0];
  150. resolve();
  151. }).catch(function(error)
  152. {
  153. console.log("error in users.js");
  154. reject(error);
  155. });
  156. });
  157. },
  158. processPostData: function(templateContext, postData)
  159. {
  160. return new Promise(function(resolve, reject)
  161. {
  162. Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
  163. addUserPostData(postData),
  164. removeUserPost(postData),
  165. editUserPost(postData, templateContext),
  166. getUserInformation(templateContext)]).then(function(template)
  167. {
  168. templateContext.adminPage = template[0];
  169. resolve();
  170. }).catch(function(error)
  171. {
  172. console.log("error in users.js");
  173. reject(error);
  174. });
  175. });
  176. }
  177. };