Commit 26693b83cd566888d02432f07a51d158de133dc8

Authored by kristinlaahneolsen
1 parent f6ebb02a

Rute til aktiviteter basert på id og liste over alle. Vi ser vi må gjøre om på a…

…dressen som sendes inn for å få det mer i tråd med rest. Vi har også gjort det slik at cassandra-databasen med de tilhørende tabellene blir laget hvis de ikke allerede eksisterer
Showing 2 changed files with 97 additions and 63 deletions   Show diff stats
restApi/.idea/workspace.xml
... ... @@ -2,7 +2,6 @@
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/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
6 5 <change beforePath="$PROJECT_DIR$/api/routes/routes.js" afterPath="$PROJECT_DIR$/api/routes/routes.js" />
7 6 </list>
8 7 <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
... ... @@ -55,8 +54,8 @@
55 54 <file leaf-file-name="routes.js" pinned="false" current-in-tab="true">
56 55 <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
57 56 <provider selected="true" editor-type-id="text-editor">
58   - <state relative-caret-position="481">
59   - <caret line="211" column="37" lean-forward="true" selection-start-line="211" selection-start-column="37" selection-end-line="211" selection-end-column="42" />
  57 + <state relative-caret-position="690">
  58 + <caret line="46" column="4" lean-forward="false" selection-start-line="46" selection-start-column="4" selection-end-line="46" selection-end-column="4" />
60 59 <folding />
61 60 </state>
62 61 </provider>
... ... @@ -115,8 +114,8 @@
115 114 <file leaf-file-name="activity.js" pinned="false" current-in-tab="false">
116 115 <entry file="file://$PROJECT_DIR$/api/modules/activity.js">
117 116 <provider selected="true" editor-type-id="text-editor">
118   - <state relative-caret-position="0">
119   - <caret line="0" column="19" lean-forward="false" selection-start-line="0" selection-start-column="19" selection-end-line="0" selection-end-column="19" />
  117 + <state relative-caret-position="105">
  118 + <caret line="7" column="23" lean-forward="true" selection-start-line="7" selection-start-column="23" selection-end-line="7" selection-end-column="23" />
120 119 <folding />
121 120 </state>
122 121 </provider>
... ... @@ -291,12 +290,12 @@
291 290 <workItem from="1538928470276" duration="5791000" />
292 291 <workItem from="1538934478875" duration="76000" />
293 292 <workItem from="1540900056741" duration="8173000" />
294   - <workItem from="1540976986771" duration="2664000" />
  293 + <workItem from="1540976986771" duration="6708000" />
295 294 </task>
296 295 <servers />
297 296 </component>
298 297 <component name="TimeTrackingManager">
299   - <option name="totallyTimeSpent" value="28301000" />
  298 + <option name="totallyTimeSpent" value="32345000" />
300 299 </component>
301 300 <component name="ToolWindowManager">
302 301 <frame x="1440" y="-180" width="1920" height="1080" extended-state="0" />
... ... @@ -465,14 +464,6 @@
465 464 </state>
466 465 </provider>
467 466 </entry>
468   - <entry file="file://$PROJECT_DIR$/api/modules/activity.js">
469   - <provider selected="true" editor-type-id="text-editor">
470   - <state relative-caret-position="0">
471   - <caret line="0" column="19" lean-forward="false" selection-start-line="0" selection-start-column="19" selection-end-line="0" selection-end-column="19" />
472   - <folding />
473   - </state>
474   - </provider>
475   - </entry>
476 467 <entry file="file://$PROJECT_DIR$/api/modules/incident.js">
477 468 <provider selected="true" editor-type-id="text-editor">
478 469 <state relative-caret-position="135">
... ... @@ -489,10 +480,18 @@
489 480 </state>
490 481 </provider>
491 482 </entry>
  483 + <entry file="file://$PROJECT_DIR$/api/modules/activity.js">
  484 + <provider selected="true" editor-type-id="text-editor">
  485 + <state relative-caret-position="105">
  486 + <caret line="7" column="23" lean-forward="true" selection-start-line="7" selection-start-column="23" selection-end-line="7" selection-end-column="23" />
  487 + <folding />
  488 + </state>
  489 + </provider>
  490 + </entry>
492 491 <entry file="file://$PROJECT_DIR$/api/routes/routes.js">
493 492 <provider selected="true" editor-type-id="text-editor">
494   - <state relative-caret-position="481">
495   - <caret line="211" column="37" lean-forward="true" selection-start-line="211" selection-start-column="37" selection-end-line="211" selection-end-column="42" />
  493 + <state relative-caret-position="690">
  494 + <caret line="46" column="4" lean-forward="false" selection-start-line="46" selection-start-column="4" selection-end-line="46" selection-end-column="4" />
496 495 <folding />
497 496 </state>
498 497 </provider>
... ...
restApi/api/routes/routes.js
1 1 //All endpoints will be defined in this file
2 2 const Incident = require("../modules/incident.js");
3 3 const Evaluation = require("../modules/evaluation.js");
  4 +const Activity = require("../modules/activity.js");
4 5  
5 6  
6 7 const uuid = require('uuid');
... ... @@ -8,11 +9,29 @@ var cassandra = require(&quot;cassandra-driver&quot;);
8 9  
9 10 var client = new cassandra.Client({contactPoints: ['127.0.0.1']});
10 11 client.connect(function (err, result) {
11   - if(err) console.log("Could not connect to Cassandra")
  12 + if(err) console.log("Could not connect to Cassandra");
12 13 console.log('Successfully connected to Cassandra');
13 14 });
14 15  
15 16  
  17 +client.execute("CREATE KEYSPACE IF NOT EXISTS risk WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1};", function (err) {
  18 + if (err) console.log("Failed to create keyspace. Error: " + err);
  19 +
  20 + client.execute("CREATE TABLE risk.incident(uuid uuid, id int, date date, location text, title text, description text, associatedEval int, PRIMARY KEY(id));", function (err) {
  21 + if (err) console.log("Failed to create table incident. Error: " + err)
  22 + });
  23 +
  24 + client.execute("CREATE TABLE risk.evaluation(id int, uuid uuid, title text, outcome text, measures text, consequence int, probability int, associatedActivity int, PRIMARY KEY(id));", function (err) {
  25 + if (err) console.log("Failed to create table evaluation. Error: " + err)
  26 + });
  27 +
  28 + client.execute("CREATE TABLE risk.activity(id int, uuid uuid, title text, PRIMARY KEY(id));", function (err) {
  29 + if (err) console.log("Failed to create table activity. Error: " + err)
  30 + });
  31 +});
  32 +
  33 +
  34 +
16 35 var mockIncidents = [
17 36 new Incident("1", "03.03.2018", "Bodø", "Datt på isen", "Datt på isen når jeg skuplle gå til bilen", "4"),
18 37 new Incident("2", "11.11.2012", "Oslo", "Bilkræsj", "Kræsjet med en annen bil", "2"),
... ... @@ -21,31 +40,17 @@ var mockIncidents = [
21 40  
22 41 var appRouter = function(app) {
23 42  
24   - //Made only methods for Incident for demonstration. Other methods can be easily added later when we actually need to use them (there is no point in making them with mock if they are not gonna be used yet)
25   -
26 43 //Root endpoint (http://localhost:3000)
27 44 app.get("/", function(req, res) {
28 45 res.send("Evaluation API by Ali, Kristin and Anna is up and running...");
29 46 });
  47 +
30 48  
31   - //Get incident by id endpoint (http://localhost:3000/getIncident?id=1 will return the mock object with id 1)
32   - app.get("/getIncident", function (req, res) {
33   - /* MOCK
34   - var foundIncident = null;
35   -
36   - //Search the array with mockIncidents and if id is found stop the loop and set foundIncident variable
37   - mockIncidents.forEach(function (value) {
38   - if (value.id === req.query.id) {
39   - foundIncident = value;
40   - return true;
41   - }
42   - });
43   - //Check if id is specified as a query param and check if id is 1 (only id that we have as of this mock server)
44   - if (foundIncident == null) return res.send({"status": "error", "message": "id not found or missing"});
45   -
46   - return res.send(foundIncident);
47   - */
  49 + /**
  50 + * Get incident by id endpoint (http://localhost:3000/getIncident?id=1 will return incident with id 1)
  51 + */
48 52  
  53 + app.get("/getIncident", function (req, res) {
49 54  
50 55 client.execute('SELECT * FROM risk.incident WHERE id=' + req.query.id, [], function(err, result) {
51 56 if(err) {
... ... @@ -62,16 +67,12 @@ var appRouter = function(app) {
62 67  
63 68 });
64 69  
65   - //Get a JSON array of all mockIncidents if not empty
  70 + /**
  71 + * See all incidents
  72 + */
66 73  
67 74 app.get("/getAllIncidents", function (req, res) {
68 75  
69   - /* MOCKDATA
70   - if (mockIncidents != null && mockIncidents.length !== 0) return res.send(mockIncidents);
71   -
72   - return res.send({"status": "error", "message": "No mockIncidents found on the server"});
73   - */
74   -
75 76 client.execute('SELECT * FROM risk.incident', [], function (err, result) {
76 77 if(err) {
77 78 console.log(err);
... ... @@ -88,32 +89,20 @@ var appRouter = function(app) {
88 89  
89 90 });
90 91  
  92 + /**
  93 + * Add a new incident (http://localhost:3000/addIncident - with an "x-www-form-urlencoded" body of params)
  94 + */
91 95  
92   -
93   - //Add a new incident (http://localhost:3000/addIncident - with an "x-www-form-urlencoded" body of params)
94 96 app.post("/addIncident", function(req, res) {
95 97  
96   - /* Mocdata
97 98 // All these parameters must exist in the body with the exact name for this method to work
98 99 if(!req.body.id || !req.body.date || !req.body.location || !req.body.title || !req.body.description || !req.body.associatedEval) {
99 100 return res.send({"status": "error", "message": "missing a parameters"});
100 101 } else {
101   - /*
102   - Here the request is successful and should be saved in the database but this is only a mock server
103   - so the request body is returned to show that it is working (can be tested using Postman)
104   -
105   - return res.send(req.body);
106   - }
107   - */
108   -
109   -
110   - if(!req.body.id || !req.body.date || !req.body.location || !req.body.title || !req.body.description || !req.body.associatedEval) {
111   - return res.send({"status": "error", "message": "missing a parameters"});
112   - } else {
113 102 const query = 'INSERT INTO risk.incident(id, uuid, date, location, title, description, associatedEval) VALUES(?,?,?,?,?,?,?)';
114 103 const params = [req.body.id, uuid(), new Date(req.body.date), req.body.location, req.body.title, req.body.description, req.body.associatedEval];
115 104 client.execute(query, params, { prepare: true }, function (err) {
116   -
  105 + //Here the request is successful and should be saved in the database
117 106 if (err) {
118 107 console.log(err);
119 108 return res.send({"status": "error", "message": "Something went wrong"})
... ... @@ -124,9 +113,11 @@ var appRouter = function(app) {
124 113  
125 114 });
126 115  
  116 + /**
  117 + * Update an incident (http://localhost:3000/updateIncident?id=1 will update the incident with id=1 with the params passed in the request body)
  118 + */
127 119  
128 120  
129   - //Update an incident (http://localhost:3000/updateIncident?id=1 will update the incident with id=1 with the params passed in the request body)
130 121 app.patch("/updateIncident", function (req, res) {
131 122 var foundIncident = null;
132 123  
... ... @@ -152,7 +143,11 @@ var appRouter = function(app) {
152 143 });
153 144 });
154 145  
155   - //Delete an incident (http://localhost:3000/deleteIncident?id=1 should delete incident with id=1)
  146 +
  147 + /**
  148 + * Delete an incident (http://localhost:3000/deleteIncident?id=1 should delete incident with id=1)
  149 + */
  150 +
156 151 app.delete("/deleteIncident", function (req, res) {
157 152 if (!req.query.id) return res.send({"status": "failed", "message": "Id missing"});
158 153  
... ... @@ -210,7 +205,7 @@ var appRouter = function(app) {
210 205  
211 206 app.post("/addEvaluation", function(req, res) {
212 207 if(!req.body.id || !req.body.title || !req.body.outcome || !req.body.measures || !req.body.consequence || !req.body.probability || !req.body.associatedActivity) {
213   - return res.send({"status": "error", "message": "missing a parameters"});
  208 + return res.send({"status": "error", "message": "missing a parameter"});
214 209 } else {
215 210 const query = 'INSERT INTO risk.evaluation(id, uuid, title, outcome, measures, consequence, probability, associatedActivity) VALUES(?,?,?,?,?,?,?,?)';
216 211 const params = [req.body.id, uuid(), req.body.title, req.body.outcome, req.body.measures, req.body.consequence, req.body.probability, req.body.associatedActivity];
... ... @@ -227,10 +222,50 @@ var appRouter = function(app) {
227 222 });
228 223  
229 224  
  225 + /**
  226 + * See an activity with id x
  227 + */
  228 +
  229 + app.get("/getActivity", function (req, res) {
  230 + client.execute('SELECT * FROM risk.activity WHERE id=' + req.query.id, [], function(err, result) {
  231 + if(err) {
  232 + console.log(err);
  233 + return res.send({"status": "error", "message": "Missing id, please use /getActivity?id=[id]"})
  234 + } else {
  235 + if (result.rows.length != 0)
  236 + return res.send(result.rows[0])
  237 + else
  238 + return res.send({"status": "error", "message": "Id not found"})
  239 + }
  240 + });
230 241  
  242 + })
231 243  
232 244  
233 245  
  246 + /**
  247 + * See all activities
  248 + */
  249 +
  250 + app.get("/getAllActivities", function (req, res) {
  251 +
  252 + client.execute('SELECT * FROM risk.activity', [], function (err, result) {
  253 + if(err) {
  254 + console.log(err);
  255 + return res.send({"status": "error", "message": "Something went wrong"})
  256 +
  257 + } else {
  258 + if (result.rows.length != 0)
  259 + return res.send(result.rows);
  260 + else
  261 + return res.send({"status": "error", "message": "No registered activities"})
  262 + }
  263 +
  264 + })
  265 +
  266 + });
  267 +
  268 +
234 269 };
235 270  
236 271  
... ...