123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. var ip = require('ip');
  2. var Tool = require('../models/Tool');
  3. var logger = require('../utils/logger');
  4. var validation = require('../validation');
  5. /* CREATE (GUARDED) */
  6. /**
  7. * Function for creating a new Tool in the DB.
  8. * @param req The request object sent over from the route.
  9. * @param res The response object sent over from the route.
  10. */
  11. exports.createTool = function (req, res) {
  12. //Get the toolId and toolName from the body of the request
  13. var toolId = parseInt(req.body.toolId);
  14. var toolName = req.body.toolName;
  15. //Check that the tool id and name are valid
  16. var valid = validation.validateToolIdName(res, {toolId: toolId, toolName: toolName});
  17. //Only proceed if the parameters were valid.
  18. //Bad params will be handled in helper method
  19. if (valid) {
  20. //Declare a new Tool object using the model
  21. var newTool = new Tool({
  22. _id: toolId,
  23. id: toolId,
  24. name: toolName,
  25. about: [],
  26. training: [],
  27. links: []
  28. });
  29. //Save to MongoDB
  30. newTool.save(function (err) {
  31. //If there was an error while saving, send it back to the client
  32. if (err) {
  33. var message = "";
  34. //In this case, 11000 is the error code for a duplicate key
  35. if (err.code == 11000)
  36. message = "New tool creation failed. Duplicate tool id. Please check all inputs.";
  37. //Otherwise, just set the message = error message from mongoose
  38. else
  39. message = err.errmsg;
  40. //Send an HTTP 422 (Unprocessable Entity) with the error message.
  41. //Why HTTP 422: HTTP 400 is bad syntax. Correct syntax was supplied, but something went wrong during the request
  42. res.status(422).send({created: false, message: message, errors: err});
  43. //Log this request
  44. logger.info("[" + ip.address() + "] New tool creation failed. Tool id= " + toolId + " Reason: " + message);
  45. }
  46. //Otherwise, the addition was successful
  47. else {
  48. //Send an HTTP 201 (Created)
  49. res.status(201).send({created: true});
  50. //Log this request
  51. logger.info("[" + ip.address() + "] New tool creation succeeded. toolId= " + toolId);
  52. }
  53. });
  54. }
  55. };
  56. /* READ (UNGUARDED) */
  57. /**
  58. * Get a particular tool.
  59. * @param req The request object sent over from the route.
  60. * @param res The response object sent over from the route.
  61. */
  62. exports.getTool = function (req, res) {
  63. //Get the toolId from the body of the request
  64. var toolId = parseInt(req.params.toolId);
  65. //Check that the tool id is valid.
  66. var valid = validation.validateToolId(res, {toolId: toolId});
  67. //Only proceed if the parameters were valid.
  68. //Bad params will be handled in helper method
  69. if (valid) {
  70. //Look for a matching document in the DB
  71. Tool.findOne({'id': toolId}, function (err, tool) {
  72. //Catch any general errors and send them back to the client
  73. if (err)
  74. res.status(404).send({message: "Error finding matching document.", errors: err});
  75. //Otherwise, send back the matching tool
  76. else {
  77. //If the matching matching tool could not be found
  78. if (tool == undefined || tool == null)
  79. res.status(404).send({message: "Could not find matching tool.", errors: err});
  80. //Otherwise, send back the matching tool
  81. else
  82. res.status(200).send(tool);
  83. }
  84. });
  85. }
  86. };
  87. /**
  88. * Function for getting a particular tool's section.
  89. * @param req The request object sent over from the route.
  90. * @param res The response object sent over from the route.
  91. */
  92. exports.getToolSection = function (req, res) {
  93. //Get the tool id and section name from the params of the request
  94. var toolId = parseInt(req.params.toolId);
  95. var sectionName = req.params.sectionName.toLowerCase();
  96. //Check that the tool id and section name is valid.
  97. var valid = validation.validateToolAndSection(res, {toolId: toolId, sectionName: sectionName});
  98. //Only proceed if the parameters were valid.
  99. //Bad params will be handled in helper method
  100. if (valid) {
  101. //Look for a matching document in the DB
  102. Tool.findOne({'id': toolId}, function (err, tool) {
  103. //Catch any general errors and send them back to the client
  104. if (err)
  105. res.status(404).send({message: "Error finding matching document.", errors: err});
  106. //Otherwise, send back the matching tool
  107. else {
  108. //If the matching toolId + sectionName combo was not found, send back a 404
  109. if (tool[sectionName] == undefined || tool[sectionName] == null)
  110. res.status(404).send({
  111. message: "Could not find matching toolId and sectionName combo.",
  112. errors: err
  113. });
  114. //Otherwise, send back the matching section
  115. else
  116. res.status(200).send(tool[sectionName]);
  117. }
  118. });
  119. }
  120. };
  121. /**
  122. * Function for getting a particular item from a tool's section.
  123. * @param req The request object sent over from the route.
  124. * @param res The response object sent over from the route.
  125. */
  126. exports.getToolSectionItem = function (req, res) {
  127. //Get the tool id and section name from the params of the request
  128. var toolId = parseInt(req.params.toolId);
  129. var sectionName = req.params.sectionName.toLowerCase();
  130. var itemNum = parseInt(req.params.itemNum);
  131. //Check that the tool id, section name and item number are valid
  132. var valid = validation.validateToolSectionItem(res, {toolId: toolId, sectionName: sectionName, itemNum: itemNum});
  133. if (valid) {
  134. Tool.findOne({'id': toolId}, function (err, tool) {
  135. //Catch any general errors and send them back to the client
  136. if (err)
  137. res.status(404).send({errors: err});
  138. //Otherwise, there was (or was not) a matching document
  139. else {
  140. //If the matching toolId + sectionName combo was not found, send back a 404
  141. if (tool[sectionName] == undefined || tool[sectionName] == null)
  142. res.status(404).send({
  143. message: "Could not find matching toolId and sectionName combo.",
  144. errors: err
  145. });
  146. //Otherwise, send back the item
  147. else {
  148. if (tool[sectionName][itemNum] == undefined || tool[sectionName][itemNum] == null)
  149. res.status(404).send({message: "Could not find given item.", errors: err});
  150. else
  151. res.status(200).send(tool[sectionName][itemNum]);
  152. }
  153. }
  154. });
  155. }
  156. };
  157. /**
  158. * Function for getting the count of items in a tool's section.
  159. * @param req The request object sent over from the route.
  160. * @param res The response object sent over from the route.
  161. */
  162. exports.getToolSectionItemCount = function (req, res) {
  163. //Get the tool id and section name from the params of the request
  164. var toolId = parseInt(req.params.toolId);
  165. var sectionName = req.params.sectionName.toLowerCase();
  166. //Check that the tool id and section name are valid.
  167. var valid = validation.validateToolAndSection(res, {toolId: toolId, sectionName: sectionName});
  168. //Only proceed if the parameters were valid.
  169. //Bad params will be handled in helper method
  170. if (valid) {
  171. //Look for a matching document in the DB
  172. Tool.findOne({'id': toolId}, function (err, tool) {
  173. //Catch any general errors and send them back to the client
  174. if (err)
  175. res.status(404).send({message: "Error finding matching document.", errors: err});
  176. //Otherwise, send back the matching tool
  177. else {
  178. //If the matching toolId + sectionName combo was not found, send back a 404
  179. if (tool[sectionName] == undefined || tool[sectionName] == null)
  180. res.status(404).send({
  181. message: "Could not find matching toolId and sectionName combo.",
  182. errors: err
  183. });
  184. //Otherwise, send back the matching section
  185. else
  186. res.status(200).send({count: tool[sectionName].length});
  187. }
  188. });
  189. }
  190. };
  191. /* UPDATE (GUARDED) */
  192. /**
  193. * Function for adding an item to a tool's section
  194. * @param req The request object sent over from the route.
  195. * @param res The response object sent over from the route.
  196. */
  197. //TODO: Add this
  198. exports.addItem = function (req, res) {
  199. };
  200. /* DELETE (GUARDED) */
  201. /**
  202. * Function for deleting an item from a tool's section.
  203. * @param req The request object sent over from the route.
  204. * @param res The response object sent over from the route.
  205. */
  206. //TODO: Add this
  207. exports.deleteItem = function (req, res) {
  208. };
  209. /**
  210. * Function for deleting a tool from the DB.
  211. * @param req The request object sent over from the route.
  212. * @param res The response object sent over from the route.
  213. */
  214. exports.deleteTool = function (req, res) {
  215. //Get the tool id and section name from the params of the request
  216. var toolId = parseInt(req.params.toolId);
  217. //Check that the tool id is valid.
  218. var valid = validation.validateToolId(res, {toolId: toolId});
  219. if (valid) {
  220. Tool.remove({_id: toolId}, function (err) {
  221. if (err)
  222. res.status(404).send({deleted: false, message: "Error finding matching document.", errors: err});
  223. else
  224. res.status(200).send({deleted: true});
  225. });
  226. }
  227. };