Commit df758bae30d054cec39a2e78a9c21a9ae90c5c22

Authored by Ali B
1 parent 7d057d72

Completed the API so it now fully follows the RESTful standard. Edited the respo…

…nses sent back to the client with status codes and messages. Cleaned up some code and fixed the automated keyspace and table creation when the api starts running (so it creates keyspace and all the tables in cassandra if they dont exist). Fixed the patch and delete methods so they now work as expected
restApi/.idea/restApi.iml
... ... @@ -4,15 +4,5 @@
4 4 <content url="file://$MODULE_DIR$" />
5 5 <orderEntry type="inheritedJdk" />
6 6 <orderEntry type="sourceFolder" forTests="false" />
7   - <orderEntry type="module-library">
8   - <library name="PHP Runtime" type="php">
9   - <CLASSES>
10   - <root url="jar://$APPLICATION_HOME_DIR$/plugins/php/lib/php.jar!/stubs/standard" />
11   - </CLASSES>
12   - <SOURCES>
13   - <root url="jar://$APPLICATION_HOME_DIR$/plugins/php/lib/php.jar!/stubs/standard" />
14   - </SOURCES>
15   - </library>
16   - </orderEntry>
17 7 </component>
18 8 </module>
19 9 \ No newline at end of file
... ...
restApi/.idea/workspace.xml
... ... @@ -2,8 +2,10 @@
2 2 <project version="4">
3 3 <component name="ChangeListManager">
4 4 <list default="true" id="8a337026-05ab-47b0-872d-ab1e8037e9a0" name="Default" comment="">
  5 + <change beforePath="$PROJECT_DIR$/.idea/restApi.iml" afterPath="$PROJECT_DIR$/.idea/restApi.iml" />
5 6 <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
6 7 <change beforePath="$PROJECT_DIR$/api/routes/routes.js" afterPath="$PROJECT_DIR$/api/routes/routes.js" />
  8 + <change beforePath="$PROJECT_DIR$/server.js" afterPath="$PROJECT_DIR$/server.js" />
7 9 </list>
8 10 <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
9 11 <option name="TRACKING_ENABLED" value="true" />
... ... @@ -51,28 +53,76 @@
51 53 </session>
52 54 </component>
53 55 <component name="FileEditorManager">
54   - <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
55   - <file leaf-file-name="routes.js" pinned="false" current-in-tab="true">
56   - <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
57   - <provider selected="true" editor-type-id="text-editor">
58   - <state relative-caret-position="595">
59   - <caret line="94" column="56" lean-forward="true" selection-start-line="94" selection-start-column="56" selection-end-line="94" selection-end-column="56" />
60   - <folding />
61   - </state>
62   - </provider>
63   - </entry>
64   - </file>
65   - <file leaf-file-name="package.json" pinned="false" current-in-tab="false">
66   - <entry file="file://$PROJECT_DIR$/package.json">
67   - <provider selected="true" editor-type-id="text-editor">
68   - <state relative-caret-position="408">
69   - <caret line="24" column="0" lean-forward="false" selection-start-line="24" selection-start-column="0" selection-end-line="24" selection-end-column="0" />
70   - <folding />
71   - </state>
72   - </provider>
73   - </entry>
74   - </file>
75   - </leaf>
  56 + <splitter split-orientation="horizontal" split-proportion="0.49200428">
  57 + <split-first>
  58 + <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
  59 + <file leaf-file-name="routes.js" pinned="false" current-in-tab="true">
  60 + <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
  61 + <provider selected="true" editor-type-id="text-editor">
  62 + <state relative-caret-position="1373">
  63 + <caret line="153" column="0" lean-forward="true" selection-start-line="153" selection-start-column="0" selection-end-line="153" selection-end-column="0" />
  64 + <folding />
  65 + </state>
  66 + </provider>
  67 + </entry>
  68 + </file>
  69 + <file leaf-file-name="package.json" pinned="false" current-in-tab="false">
  70 + <entry file="file://$PROJECT_DIR$/package.json">
  71 + <provider selected="true" editor-type-id="text-editor">
  72 + <state relative-caret-position="408">
  73 + <caret line="24" column="0" lean-forward="false" selection-start-line="24" selection-start-column="0" selection-end-line="24" selection-end-column="0" />
  74 + <folding />
  75 + </state>
  76 + </provider>
  77 + </entry>
  78 + </file>
  79 + <file leaf-file-name="package-lock.json" pinned="false" current-in-tab="false">
  80 + <entry file="file://$PROJECT_DIR$/package-lock.json">
  81 + <provider selected="true" editor-type-id="text-editor">
  82 + <state relative-caret-position="0">
  83 + <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
  84 + <folding />
  85 + </state>
  86 + </provider>
  87 + </entry>
  88 + </file>
  89 + <file leaf-file-name="README.txt" pinned="false" current-in-tab="false">
  90 + <entry file="file://$PROJECT_DIR$/README.txt">
  91 + <provider selected="true" editor-type-id="text-editor">
  92 + <state relative-caret-position="136">
  93 + <caret line="8" column="58" lean-forward="false" selection-start-line="8" selection-start-column="58" selection-end-line="8" selection-end-column="58" />
  94 + <folding />
  95 + </state>
  96 + </provider>
  97 + </entry>
  98 + </file>
  99 + <file leaf-file-name="server.js" pinned="false" current-in-tab="false">
  100 + <entry file="file://$PROJECT_DIR$/server.js">
  101 + <provider selected="true" editor-type-id="text-editor">
  102 + <state relative-caret-position="17">
  103 + <caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="2" selection-end-column="40" />
  104 + <folding />
  105 + </state>
  106 + </provider>
  107 + </entry>
  108 + </file>
  109 + </leaf>
  110 + </split-first>
  111 + <split-second>
  112 + <leaf>
  113 + <file leaf-file-name="routes.js" pinned="false" current-in-tab="true">
  114 + <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
  115 + <provider selected="true" editor-type-id="text-editor">
  116 + <state relative-caret-position="595">
  117 + <caret line="188" column="0" lean-forward="true" selection-start-line="188" selection-start-column="0" selection-end-line="188" selection-end-column="0" />
  118 + <folding />
  119 + </state>
  120 + </provider>
  121 + </entry>
  122 + </file>
  123 + </leaf>
  124 + </split-second>
  125 + </splitter>
76 126 </component>
77 127 <component name="FileTemplateManagerImpl">
78 128 <option name="RECENT_TEMPLATES">
... ... @@ -113,8 +163,8 @@
113 163 <option value="$PROJECT_DIR$/api/modules/incident.js" />
114 164 <option value="$PROJECT_DIR$/api/modules/evaluation.js" />
115 165 <option value="$PROJECT_DIR$/api/modules/activity.js" />
116   - <option value="$PROJECT_DIR$/server.js" />
117 166 <option value="$PROJECT_DIR$/package.json" />
  167 + <option value="$PROJECT_DIR$/server.js" />
118 168 <option value="$PROJECT_DIR$/api/routes/routes.js" />
119 169 </list>
120 170 </option>
... ... @@ -139,9 +189,7 @@
139 189 <servers />
140 190 </component>
141 191 <component name="PhpWorkspaceProjectConfiguration" backward_compatibility_performed="true" />
142   - <component name="ProjectFrameBounds" fullScreen="true">
143   - <option name="x" value="1440" />
144   - <option name="y" value="-180" />
  192 + <component name="ProjectFrameBounds" extendedState="7">
145 193 <option name="width" value="1920" />
146 194 <option name="height" value="1080" />
147 195 </component>
... ... @@ -162,7 +210,6 @@
162 210 </navigator>
163 211 <panes>
164 212 <pane id="Scratches" />
165   - <pane id="Scope" />
166 213 <pane id="ProjectPane">
167 214 <subPane>
168 215 <expand>
... ... @@ -191,13 +238,14 @@
191 238 <select />
192 239 </subPane>
193 240 </pane>
  241 + <pane id="Scope" />
194 242 </panes>
195 243 </component>
196 244 <component name="PropertiesComponent">
197 245 <property name="WebServerToolWindowFactoryState" value="false" />
198 246 <property name="last_opened_file_path" value="$PROJECT_DIR$" />
199 247 <property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
200   - <property name="nodejs_interpreter_path" value="/usr/local/bin/node" />
  248 + <property name="nodejs_interpreter_path" value="node" />
201 249 </component>
202 250 <component name="RunDashboard">
203 251 <option name="ruleStates">
... ... @@ -236,17 +284,18 @@
236 284 <workItem from="1538934478875" duration="76000" />
237 285 <workItem from="1540900056741" duration="8173000" />
238 286 <workItem from="1540976986771" duration="6708000" />
  287 + <workItem from="1541075117729" duration="8803000" />
239 288 </task>
240 289 <servers />
241 290 </component>
242 291 <component name="TimeTrackingManager">
243   - <option name="totallyTimeSpent" value="37232000" />
  292 + <option name="totallyTimeSpent" value="46156000" />
244 293 </component>
245 294 <component name="ToolWindowManager">
246   - <frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
  295 + <frame x="-8" y="-8" width="1936" height="1056" extended-state="7" />
247 296 <editor active="true" />
248 297 <layout>
249   - <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.20309168" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
  298 + <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.18923241" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
250 299 <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
251 300 <window_info id="Docker" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
252 301 <window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="10" side_tool="true" content_ui="tabs" />
... ... @@ -259,12 +308,10 @@
259 308 <window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="3" side_tool="true" content_ui="tabs" />
260 309 <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
261 310 <window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
262   - <window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
263 311 <window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
264 312 <window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
265 313 <window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
266 314 <window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
267   - <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" show_stripe_button="true" weight="0.18746708" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
268 315 <window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.33" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
269 316 <window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" show_stripe_button="true" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
270 317 </layout>
... ... @@ -282,6 +329,22 @@
282 329 <component name="editorHistoryManager">
283 330 <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
284 331 <provider selected="true" editor-type-id="text-editor">
  332 + <state relative-caret-position="0">
  333 + <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
  334 + <folding />
  335 + </state>
  336 + </provider>
  337 + </entry>
  338 + <entry file="file://$PROJECT_DIR$/package.json">
  339 + <provider selected="true" editor-type-id="text-editor">
  340 + <state relative-caret-position="408">
  341 + <caret line="24" column="0" lean-forward="false" selection-start-line="24" selection-start-column="0" selection-end-line="24" selection-end-column="0" />
  342 + <folding />
  343 + </state>
  344 + </provider>
  345 + </entry>
  346 + <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
  347 + <provider selected="true" editor-type-id="text-editor">
285 348 <state relative-caret-position="119">
286 349 <caret line="7" column="14" lean-forward="false" selection-start-line="7" selection-start-column="14" selection-end-line="7" selection-end-column="14" />
287 350 <folding />
... ... @@ -300,7 +363,6 @@
300 363 <provider selected="true" editor-type-id="text-editor">
301 364 <state relative-caret-position="170">
302 365 <caret line="10" column="9" lean-forward="false" selection-start-line="10" selection-start-column="9" selection-end-line="10" selection-end-column="9" />
303   - <folding />
304 366 </state>
305 367 </provider>
306 368 </entry>
... ... @@ -308,7 +370,6 @@
308 370 <provider selected="true" editor-type-id="text-editor">
309 371 <state relative-caret-position="0">
310 372 <caret line="0" column="19" lean-forward="false" selection-start-line="0" selection-start-column="19" selection-end-line="0" selection-end-column="19" />
311   - <folding />
312 373 </state>
313 374 </provider>
314 375 </entry>
... ... @@ -316,7 +377,6 @@
316 377 <provider selected="true" editor-type-id="text-editor">
317 378 <state relative-caret-position="153">
318 379 <caret line="9" column="49" lean-forward="false" selection-start-line="9" selection-start-column="49" selection-end-line="9" selection-end-column="49" />
319   - <folding />
320 380 </state>
321 381 </provider>
322 382 </entry>
... ... @@ -340,7 +400,6 @@
340 400 <provider selected="true" editor-type-id="text-editor">
341 401 <state relative-caret-position="153">
342 402 <caret line="9" column="1" lean-forward="false" selection-start-line="9" selection-start-column="1" selection-end-line="9" selection-end-column="1" />
343   - <folding />
344 403 </state>
345 404 </provider>
346 405 </entry>
... ... @@ -348,7 +407,6 @@
348 407 <provider selected="true" editor-type-id="text-editor">
349 408 <state relative-caret-position="0">
350 409 <caret line="0" column="19" lean-forward="false" selection-start-line="0" selection-start-column="19" selection-end-line="0" selection-end-column="19" />
351   - <folding />
352 410 </state>
353 411 </provider>
354 412 </entry>
... ... @@ -356,7 +414,6 @@
356 414 <provider selected="true" editor-type-id="text-editor">
357 415 <state relative-caret-position="170">
358 416 <caret line="10" column="1" lean-forward="false" selection-start-line="10" selection-start-column="1" selection-end-line="10" selection-end-column="1" />
359   - <folding />
360 417 </state>
361 418 </provider>
362 419 </entry>
... ... @@ -367,7 +424,6 @@
367 424 <provider selected="true" editor-type-id="text-editor">
368 425 <state relative-caret-position="153">
369 426 <caret line="9" column="49" lean-forward="false" selection-start-line="9" selection-start-column="49" selection-end-line="9" selection-end-column="49" />
370   - <folding />
371 427 </state>
372 428 </provider>
373 429 </entry>
... ... @@ -375,7 +431,6 @@
375 431 <provider selected="true" editor-type-id="text-editor">
376 432 <state relative-caret-position="0">
377 433 <caret line="0" column="19" lean-forward="false" selection-start-line="0" selection-start-column="19" selection-end-line="0" selection-end-column="19" />
378   - <folding />
379 434 </state>
380 435 </provider>
381 436 </entry>
... ... @@ -391,7 +446,6 @@
391 446 <provider selected="true" editor-type-id="text-editor">
392 447 <state relative-caret-position="170">
393 448 <caret line="10" column="9" lean-forward="false" selection-start-line="10" selection-start-column="9" selection-end-line="10" selection-end-column="9" />
394   - <folding />
395 449 </state>
396 450 </provider>
397 451 </entry>
... ... @@ -501,45 +555,48 @@
501 555 <entry file="file://$PROJECT_DIR$/api/models/evaluationModel.js" />
502 556 <entry file="file://$PROJECT_DIR$/api/models/models.js" />
503 557 <entry file="file://$PROJECT_DIR$/api/controllers/evaluationController.js" />
504   - <entry file="file://$PROJECT_DIR$/server.js">
  558 + <entry file="file://$PROJECT_DIR$/api/modules/incident.js">
505 559 <provider selected="true" editor-type-id="text-editor">
506   - <state relative-caret-position="105">
507   - <caret line="7" column="0" lean-forward="false" selection-start-line="7" selection-start-column="0" selection-end-line="7" selection-end-column="0" />
  560 + <state relative-caret-position="135">
  561 + <caret line="9" column="1" lean-forward="false" selection-start-line="9" selection-start-column="1" selection-end-line="9" selection-end-column="1" />
508 562 </state>
509 563 </provider>
510 564 </entry>
511   - <entry file="file://$PROJECT_DIR$/README.txt">
  565 + <entry file="file://$PROJECT_DIR$/api/modules/evaluation.js">
512 566 <provider selected="true" editor-type-id="text-editor">
513   - <state relative-caret-position="120">
514   - <caret line="8" column="58" lean-forward="false" selection-start-line="8" selection-start-column="58" selection-end-line="8" selection-end-column="58" />
  567 + <state relative-caret-position="30">
  568 + <caret line="2" column="9" lean-forward="false" selection-start-line="2" selection-start-column="9" selection-end-line="2" selection-end-column="19" />
515 569 </state>
516 570 </provider>
517 571 </entry>
518   - <entry file="file://$PROJECT_DIR$/package-lock.json">
  572 + <entry file="file://$PROJECT_DIR$/api/modules/activity.js">
519 573 <provider selected="true" editor-type-id="text-editor">
520   - <state relative-caret-position="0">
521   - <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
  574 + <state relative-caret-position="105">
  575 + <caret line="7" column="23" lean-forward="true" selection-start-line="7" selection-start-column="23" selection-end-line="7" selection-end-column="23" />
522 576 </state>
523 577 </provider>
524 578 </entry>
525   - <entry file="file://$PROJECT_DIR$/api/modules/incident.js">
  579 + <entry file="file://$PROJECT_DIR$/package-lock.json">
526 580 <provider selected="true" editor-type-id="text-editor">
527   - <state relative-caret-position="135">
528   - <caret line="9" column="1" lean-forward="false" selection-start-line="9" selection-start-column="1" selection-end-line="9" selection-end-column="1" />
  581 + <state relative-caret-position="0">
  582 + <caret line="0" column="0" lean-forward="false" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
  583 + <folding />
529 584 </state>
530 585 </provider>
531 586 </entry>
532   - <entry file="file://$PROJECT_DIR$/api/modules/evaluation.js">
  587 + <entry file="file://$PROJECT_DIR$/README.txt">
533 588 <provider selected="true" editor-type-id="text-editor">
534   - <state relative-caret-position="30">
535   - <caret line="2" column="9" lean-forward="false" selection-start-line="2" selection-start-column="9" selection-end-line="2" selection-end-column="19" />
  589 + <state relative-caret-position="136">
  590 + <caret line="8" column="58" lean-forward="false" selection-start-line="8" selection-start-column="58" selection-end-line="8" selection-end-column="58" />
  591 + <folding />
536 592 </state>
537 593 </provider>
538 594 </entry>
539   - <entry file="file://$PROJECT_DIR$/api/modules/activity.js">
  595 + <entry file="file://$PROJECT_DIR$/server.js">
540 596 <provider selected="true" editor-type-id="text-editor">
541   - <state relative-caret-position="105">
542   - <caret line="7" column="23" lean-forward="true" selection-start-line="7" selection-start-column="23" selection-end-line="7" selection-end-column="23" />
  597 + <state relative-caret-position="17">
  598 + <caret line="1" column="0" lean-forward="false" selection-start-line="1" selection-start-column="0" selection-end-line="2" selection-end-column="40" />
  599 + <folding />
543 600 </state>
544 601 </provider>
545 602 </entry>
... ... @@ -554,7 +611,7 @@
554 611 <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
555 612 <provider selected="true" editor-type-id="text-editor">
556 613 <state relative-caret-position="595">
557   - <caret line="94" column="56" lean-forward="true" selection-start-line="94" selection-start-column="56" selection-end-line="94" selection-end-column="56" />
  614 + <caret line="188" column="0" lean-forward="true" selection-start-line="188" selection-start-column="0" selection-end-line="188" selection-end-column="0" />
558 615 <folding />
559 616 </state>
560 617 </provider>
... ...
restApi/api/routes/routes.js
... ... @@ -6,7 +6,7 @@ const Activity = require(&quot;../modules/activity.js&quot;);
6 6 const uuid = require('uuid');
7 7 var cassandra = require("cassandra-driver");
8 8  
9   -setupDatabase('127.0.0.1');
  9 +var client = setupDatabase('127.0.0.1');
10 10  
11 11 var appRouter = function(app) {
12 12  
... ... @@ -17,19 +17,21 @@ var appRouter = function(app) {
17 17  
18 18  
19 19 /**
20   - * Get incident by id endpoint (http://localhost:3000/getIncident?id=1 will return incident with id 1)
  20 + * Get incident by id endpoint (a get request at http://localhost:3000/incident/1 will return incident with id 1)
21 21 */
22 22 app.get("/incident/:id", function (req, res) {
  23 + if (!req.params.id) return req.status(400).send("No id entered");
23 24  
24   - client.execute('SELECT * FROM risk.incident WHERE id=' + req.params.id, [], function(err, result) {
  25 + const params = [req.params.id];
  26 + client.execute('SELECT * FROM risk.incident WHERE id = ?', params,{ prepare: true }, function(err, result) {
25 27 if(err) {
26 28 console.log(err);
27   - return res.send({"status": "error", "message": "Missing id, please use /getIncident/{id}"})
  29 + return res.status(400).send("Bad Request, make sure to send a request with an id")
28 30 } else {
29 31 if (result.rows.length != 0)
30   - return res.send(result.rows[0]);
  32 + return res.status(200).send(result.rows[0]);
31 33 else
32   - return res.send({"status": "error", "message": "Id not found"})
  34 + return res.status(204).send("Id not found in database");
33 35  
34 36 }
35 37 });
... ... @@ -37,20 +39,20 @@ var appRouter = function(app) {
37 39 });
38 40  
39 41 /**
40   - * See all incidents
  42 + * See all incidents (a get request at http://localhost:3000/incident will return all incidents)
41 43 */
42 44 app.get("/incident", function (req, res) {
43 45  
44   - client.execute('SELECT * FROM risk.incident', [], function (err, result) {
  46 + client.execute('SELECT * FROM risk.incident', function (err, result) {
45 47 if(err) {
46 48 console.log(err);
47   - return res.send({"status": "error", "message": "Something went wrong"})
  49 + return res.status(400).send("Bad Request");
48 50  
49 51 } else {
50 52 if (result.rows.length != 0)
51   - return res.send(result.rows);
  53 + return res.status(200).send(result.rows);
52 54 else
53   - return res.send({"status": "error", "message": "No registered incidents"})
  55 + return res.status(204).send("No registered incidents");
54 56 }
55 57  
56 58 })
... ... @@ -64,16 +66,16 @@ var appRouter = function(app) {
64 66  
65 67 // All these parameters must exist in the body with the exact name for this method to work
66 68 if(!req.body.id || !req.body.date || !req.body.location || !req.body.title || !req.body.description || !req.body.associatedEval) {
67   - return res.send({"status": "error", "message": "missing a parameters"});
  69 + return res.status(400).send("Missing params");
68 70 } else {
69 71 const query = 'INSERT INTO risk.incident(id, uuid, date, location, title, description, associatedEval) VALUES(?,?,?,?,?,?,?)';
70 72 const params = [req.body.id, uuid(), new Date(req.body.date), req.body.location, req.body.title, req.body.description, req.body.associatedEval];
71 73 client.execute(query, params, { prepare: true }, function (err) {
72   - //Here the request is successful and should be saved in the database
73 74 if (err) {
74 75 console.log(err);
75   - return res.send({"status": "error", "message": "Something went wrong"})
  76 + return res.status(400).send("Bad Request");
76 77 }
  78 + return res.status(200).send("Incident Created");
77 79  
78 80 })
79 81 }
... ... @@ -81,62 +83,59 @@ var appRouter = function(app) {
81 83 });
82 84  
83 85 /**
84   - * Update an incident (a patch request at http://localhost:3000/incident/{id} will update the incident with id=1 with the params passed in the request body)
  86 + * Update an incident (a patch request at http://localhost:3000/incident/{id} will update the incident with the params passed in the request body)
85 87 */
86 88 app.patch("/incident/:id", function (req, res) {
87   - var foundIncident = null;
88 89  
89   - //Only mock so nothing is really happening here, here the database should be updated
90   - if (req.params.id) {
91 90  
92   - client.execute("UPDATE risk.incident SET " +
93   - (req.body.date !== null ? "date = ?" : "") +
94   - (req.body.location !== null ? "location = ?" : "") +
95   - (req.body.title !== null ? "title = ?");
  91 + if (!req.params.id) return res.status(400).send("Missing id");
96 92  
97   - if (req.body.date !== null)
98   -
99   - if (req.body.location !== null) foundIncident.location = req.body.location;
100   - if (req.body.title !== null) foundIncident.title = req.body.title;
101   - if (req.body.description !== null) foundIncident.description = req.body.description;
102   - if (req.body.associatedEval !== null) foundIncident.associatedEval = req.body.associatedEval;
103   -
104   - return res.send({"status": "success", "message": "Incident updated"});
  93 + if(!req.body.date || !req.body.location || !req.body.title || !req.body.description || !req.body.associatedEval) {
  94 + return res.status(400).send("Missing params");
105 95 }
106   - return res.send({"status": "error", "message": "Id missing"});
107 96  
  97 + const params = [new Date(req.body.date), req.body.location, req.body.title, req.body.description, req.body.associatedEval, req.params.id];
  98 + client.execute("UPDATE risk.incident SET date=?, location=?, title=?, description=?, associatedEval=? WHERE id=?", params,{ prepare: true }, function (err) {
  99 + if (err) {
  100 + console.log(err);
  101 + return res.status(400).send("Bad Request");
  102 + }
  103 + return res.status(200).send("Incident updated");
  104 + });
108 105  
109   - });
110   -
111   - function updateTable(tableName, co) {
112 106  
113   - }
114 107  
  108 + });
115 109  
116 110 /**
117 111 * Delete an incident (a delete request at http://localhost:3000/incident/{id} should delete incident with id=1)
118 112 */
119 113 app.delete("/incident/:id", function (req, res) {
120   - if (!req.params.id) return res.send({"status": "failed", "message": "Id missing"});
  114 + if (!req.params.id) return res.status(400).send("Missing id");
  115 + const params = [req.params.id];
  116 + client.execute("DELETE FROM risk.incident WHERE id = ?", params,{ prepare: true }, function (err) {
  117 + if (err) return res.status(400).send("Bad request");
  118 + return res.status(200).send("Incident deleted");
121 119  
122   - return res.send({"status": "success", "message": "Nothing really deleted because this is a mock server but this should delete incident with id: " + req.query.id});
  120 + });
123 121 });
124 122  
125   -
126   -
127 123 /**
128 124 * See an evaluation with id x
129 125 */
130 126 app.get("/evaluation/:id", function (req, res) {
131   - client.execute('SELECT * FROM risk.evaluation WHERE id=' + req.params.id, [], function(err, result) {
  127 + if (!req.params.id) return req.status(400).send("No id entered");
  128 +
  129 + const params = [req.params.id];
  130 + client.execute('SELECT * FROM risk.evaluation WHERE id = ?', params,{ prepare: true}, function(err, result) {
132 131 if(err) {
133 132 console.log(err);
134   - return res.send({"status": "error", "message": "Missing id, please use /getEvaluation?id=[id]"})
  133 + return res.status(400).send("Bad Request, make sure to send a request with an id")
135 134 } else {
136 135 if (result.rows.length != 0)
137   - return res.send(result.rows[0])
  136 + return res.status(200).send(result.rows[0]);
138 137 else
139   - return res.send({"status": "error", "message": "Id not found"})
  138 + return res.status(204).send("Id not found in database");
140 139 }
141 140 });
142 141  
... ... @@ -151,13 +150,13 @@ var appRouter = function(app) {
151 150 client.execute('SELECT * FROM risk.evaluation', [], function (err, result) {
152 151 if(err) {
153 152 console.log(err);
154   - return res.send({"status": "error", "message": "Something went wrong"})
  153 + return res.status(400).send("Bad Request");
155 154  
156 155 } else {
157 156 if (result.rows.length != 0)
158   - return res.send(result.rows);
  157 + return res.status(200).send(result.rows);
159 158 else
160   - return res.send({"status": "error", "message": "No registered events"})
  159 + return res.status(204).send("No registered evaluations");
161 160 }
162 161  
163 162 })
... ... @@ -170,7 +169,7 @@ var appRouter = function(app) {
170 169 */
171 170 app.post("/evaluation", function(req, res) {
172 171 if(!req.body.id || !req.body.title || !req.body.outcome || !req.body.measures || !req.body.consequence || !req.body.probability || !req.body.associatedActivity) {
173   - return res.send({"status": "error", "message": "missing a parameter"});
  172 + return res.status(400).send("Missing params");
174 173 } else {
175 174 const query = 'INSERT INTO risk.evaluation(id, uuid, title, outcome, measures, consequence, probability, associatedActivity) VALUES(?,?,?,?,?,?,?,?)';
176 175 const params = [req.body.id, uuid(), req.body.title, req.body.outcome, req.body.measures, req.body.consequence, req.body.probability, req.body.associatedActivity];
... ... @@ -178,8 +177,9 @@ var appRouter = function(app) {
178 177  
179 178 if (err) {
180 179 console.log(err);
181   - return res.send({"status": "error", "message": "Something went wrong"})
  180 + return res.status(400).send("Bad Request");
182 181 }
  182 + return res.status(200).send("Evaluation Created");
183 183  
184 184 })
185 185 }
... ... @@ -191,15 +191,18 @@ var appRouter = function(app) {
191 191 * See an activity with id x
192 192 */
193 193 app.get("/activity/:id", function (req, res) {
194   - client.execute('SELECT * FROM risk.activity WHERE id=' + req.params.id, [], function(err, result) {
  194 + if (!req.params.id) return req.status(400).send("No id entered");
  195 +
  196 + const params = [req.params.id];
  197 + client.execute('SELECT * FROM risk.activity WHERE id = ?', params,{ prepare : true}, function(err, result) {
195 198 if(err) {
196 199 console.log(err);
197   - return res.send({"status": "error", "message": "Missing id, please use /getActivity?id=[id]"})
  200 + return res.status(400).send("Bad Request, make sure to send a request with an id")
198 201 } else {
199 202 if (result.rows.length != 0)
200   - return res.send(result.rows[0])
  203 + return res.status(200).send(result.rows[0]);
201 204 else
202   - return res.send({"status": "error", "message": "Id not found"})
  205 + return res.status(204).send("Id not found in database");
203 206 }
204 207 });
205 208  
... ... @@ -215,13 +218,13 @@ var appRouter = function(app) {
215 218 client.execute('SELECT * FROM risk.activity', [], function (err, result) {
216 219 if(err) {
217 220 console.log(err);
218   - return res.send({"status": "error", "message": "Something went wrong"})
  221 + return res.status(400).send("Bad Request");
219 222  
220 223 } else {
221 224 if (result.rows.length != 0)
222   - return res.send(result.rows);
  225 + return res.status(200).send(result.rows);
223 226 else
224   - return res.send({"status": "error", "message": "No registered activities"})
  227 + return res.status(204).send("No registered activities");
225 228 }
226 229  
227 230 })
... ... @@ -240,15 +243,15 @@ function setupDatabase(contactPoint) {
240 243 client.connect(function (err, result) {
241 244 if(err) console.log("Could not connect to Cassandra at " + contactPoint);
242 245 console.log('Successfully connected to Cassandra at ' + contactPoint);
  246 + setupTables(client);
243 247 });
244   -
245   - setupTables();
  248 + return client;
246 249 }
247 250  
248 251 /**
249 252 Creates keyspace and all tables needed if they don't exist
250 253 */
251   -function setupTables() {
  254 +function setupTables(client) {
252 255 client.execute("CREATE KEYSPACE IF NOT EXISTS risk WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1};", function (err) {
253 256 if (err) console.log("Failed to create keyspace. Error: " + err);
254 257  
... ...
restApi/server.js
... ... @@ -4,10 +4,6 @@ var bodyParser = require(&quot;body-parser&quot;);
4 4 var app = express();
5 5  
6 6  
7   -
8   -
9   -
10   -
11 7 app.use(bodyParser.json());
12 8 app.use(bodyParser.urlencoded({ extended: true }));
13 9  
... ...