Skip to content

Commit

Permalink
Merge branch 'feature/getRequests' of https://github.com/UTSCCSCC01/a…
Browse files Browse the repository at this point in the history
…1f22-6-c-bacon into feature/getRequests
  • Loading branch information
BillyZ1435 committed Oct 27, 2022
2 parents 7fd324e + 725a6f0 commit 1976a0e
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 1 deletion.
75 changes: 75 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,64 @@ public String getActor(String id) throws JSONException {
return response.toString();
}

public String getMovie(String movieId) throws JSONException {
JSONObject response = new JSONObject();
String query;
query = "MATCH (m:movie { id: \"%s\"}) RETURN m.name";
query = String.format(query, movieId);

Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No movie with this ID");
return "404";
}
List<Record> resultValues = result.list();
response.put("movieId", movieId);
response.put("name", resultValues.get(0).get("m.name").asString());

query = "MATCH (a:actor)-[r:ACTED_IN]->(m:movie { id: \"%s\"}) RETURN a.id";
query = String.format(query, movieId);
result = this.session.run(query);
resultValues = result.list();
JSONArray actors = new JSONArray();
resultValues.forEach((record)->{actors.put(record.get("a.id").asString());});
response.put("actors", actors);
return response.toString();
}
public String hasRelationship(String actorId, String movieId) throws JSONException {
JSONObject response = new JSONObject();
String query;

query = "MATCH (m:movie { id: \"%s\"}) RETURN m.name";
query = String.format(query, movieId);
Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No movie with this ID");
return "404";
}

query = "MATCH (a:actor { id: \"%s\"}) RETURN a.name";
query = String.format(query, actorId);
result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No actor with this ID");
return "404";
}

response.put("movieId", movieId);
response.put("actorId", actorId);

query = "MATCH (a:actor { id: \"%s\"})-[r:ACTED_IN]->(m:movie { id: \"%s\"}) RETURN r";
query = String.format(query, actorId,movieId);
result = this.session.run(query);
if(!result.hasNext()){
response.put("hasRelationship", false);
}else{
response.put("hasRelationship", true);
}
return response.toString();
}

public String computeBaconNumber(String actorId) throws JSONException{
JSONObject response = new JSONObject();
String query;
Expand Down Expand Up @@ -165,4 +223,21 @@ public String computeBaconPath(String actorId) throws JSONException {
response.put("baconPath", path);
return response.toString();
}

public void deleteMovie(String movieId) throws JSONException {
JSONObject response = new JSONObject();
String query;

query = "MATCH (m:movie { movieId: \"%s\"}) DETACH DELETE m";
query = String.format(query, movieId);
Result result = this.session.run(query);
}

public void deleteActor(String actorId) throws JSONException {
JSONObject response = new JSONObject();
String query;
query = "MATCH (a:actor { actorId: \"%s\"}) DETACH DELETE a";
query = String.format(query, actorId);
Result result = this.session.run(query);
}
}
125 changes: 124 additions & 1 deletion src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public void handle(HttpExchange exchange) throws IOException {
case "getActor":
this.getActor(exchange);
break;
case "getMovie":
this.getMovie(exchange);
break;
case "hasRelationship":
this.hasRelationship(exchange);
break;
case "computeBaconPath":
this.computeBaconPath(exchange);
break;
Expand All @@ -61,6 +67,18 @@ public void handle(HttpExchange exchange) throws IOException {
break;
}
break;
case "DELETE":
switch (request) {
case "deleteActor":
this.deleteActor(exchange);
break;
case "deleteMovie":
this.deleteMovie(exchange);
break;
default:
break;
}
break;
default:
break;
}
Expand Down Expand Up @@ -158,7 +176,7 @@ public void addRelationship(HttpExchange r) throws IOException, JSONException {
}
}

public void getActor(HttpExchange r) throws IOException, JSONException {
public void getActor(HttpExchange r) throws IOException {
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);
Expand Down Expand Up @@ -191,6 +209,72 @@ public void getActor(HttpExchange r) throws IOException, JSONException {
}
}

public void getMovie(HttpExchange r) throws IOException {
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);
String movieId;

if (deserialized.has("movieId")) {
movieId = deserialized.getString("movieId");
} else {
r.sendResponseHeaders(400, -1);
return;
}
String response = this.dao.getMovie(movieId);
try {
if(response.equals("404")){
r.sendResponseHeaders(404, -1);
return;
}
} catch (Exception e) {
r.sendResponseHeaders(500, -1);
e.printStackTrace();
return;
}
r.sendResponseHeaders(200, response.length());
OutputStream os = r.getResponseBody();
os.write(response.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
}
}
public void hasRelationship(HttpExchange r) throws IOException {
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);
String movieId, actorId;

if (deserialized.has("actorId") && deserialized.has("movieId")) {
movieId = deserialized.getString("movieId");
actorId = deserialized.getString("actorId");
} else {
r.sendResponseHeaders(400, -1);
return;
}
String response = this.dao.hasRelationship(actorId, movieId);
try {
if(response.equals("404")){
r.sendResponseHeaders(404, -1);
return;
}
} catch (Exception e) {
r.sendResponseHeaders(500, -1);
e.printStackTrace();
return;
}
r.sendResponseHeaders(200, response.length());
OutputStream os = r.getResponseBody();
os.write(response.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
}
}

public void computeBaconNumber(HttpExchange r) throws IOException, JSONException{
String body = Utils.convert(r.getRequestBody());
try {
Expand Down Expand Up @@ -256,4 +340,43 @@ public void computeBaconPath(HttpExchange r) throws IOException, JSONException{
r.sendResponseHeaders(500, -1);
}
}
public void deleteActor(HttpExchange r) throws IOException {
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);
String actorId;

if (deserialized.has("actorId")) {
actorId = deserialized.getString("actorId");
} else {
r.sendResponseHeaders(400, -1);
return;
}
this.dao.deleteActor(actorId);
r.sendResponseHeaders(200, -1);
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
}
}

public void deleteMovie(HttpExchange r) throws IOException {
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);
String movieId;

if (deserialized.has("movieId")) {
movieId = deserialized.getString("movieId");
} else {
r.sendResponseHeaders(400, -1);
return;
}
this.dao.deleteMovie(movieId);
r.sendResponseHeaders(200, -1);
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
}
}
}
64 changes: 64 additions & 0 deletions src/test/java/ca/utoronto/utm/mcs/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,68 @@ public void addRelationshipPass() throws JSONException, IOException, Interrupted
HttpResponse<String> confirmRes = sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode());
}
@Test
public void getActorPass() throws JSONException, IOException, InterruptedException {
JSONObject setupReq = new JSONObject()
.put("name", "TestActor")
.put("actorId", "12345678901");
HttpResponse<String> setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq.toString());

JSONObject confirmReq = new JSONObject()
.put("actorId", "12345678901");
HttpResponse<String> confirmRes = sendRequest("/api/v1/getActor", "GET", confirmReq.toString());

JSONObject deleteReq = new JSONObject()
.put("actorId", "12345678901");
HttpResponse<String> deleteRes = sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
}

@Test
public void getMoviePass() throws JSONException, IOException, InterruptedException {
JSONObject setupReq = new JSONObject()
.put("name", "TestMovie")
.put("movieId", "12345678901");
HttpResponse<String> setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString());

JSONObject confirmReq = new JSONObject()
.put("movieId", "12345678901");
HttpResponse<String> confirmRes = sendRequest("/api/v1/getMovie", "GET", confirmReq.toString());

JSONObject deleteReq = new JSONObject()
.put("movieId", "12345678901");
HttpResponse<String> deleteRes = sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
}

@Test
public void hasRelationshipPass() throws JSONException, IOException, InterruptedException {
JSONObject setupReq = new JSONObject()
.put("name", "TestMovie")
.put("movieId", "12345678901");
HttpResponse<String> setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString());

setupReq = new JSONObject()
.put("name", "TestActor")
.put("actorId", "12345678902");
setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq.toString());

setupReq = new JSONObject()
.put("movieId", "12345678901")
.put("actorId", "12345678902");
setupRes = sendRequest("/api/v1/addRelationship", "PUT", setupReq.toString());

JSONObject confirmReq = new JSONObject()
.put("movieId", "12345678901")
.put("actorId", "12345678902");
HttpResponse<String> confirmRes = sendRequest("/api/v1/hasRelationship", "GET", confirmReq.toString());

JSONObject deleteReq = new JSONObject()
.put("movieId", "12345678901");
HttpResponse<String> deleteRes = sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString());
deleteReq = new JSONObject()
.put("actorId", "12345678902");
deleteRes = sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
}
}

0 comments on commit 1976a0e

Please sign in to comment.