From 229250d7bcd8a478fab2c63f527a828e6c840c00 Mon Sep 17 00:00:00 2001 From: Raymond Weng Date: Thu, 27 Oct 2022 15:30:05 -0400 Subject: [PATCH 1/2] getMovie, hasRelationship: added new apis --- .../java/ca/utoronto/utm/mcs/Neo4jDAO.java | 59 ++++++++++++++- .../java/ca/utoronto/utm/mcs/ReqHandler.java | 73 ++++++++++++++++++- 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 0ad3921..0c242c4 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -103,7 +103,7 @@ public String getActor(String actorId) throws JSONException { response.put("actorId", actorId); response.put("name", resultValues.get(0).get("a.name").asString()); - query = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.movieId";//maybe change Movie to lowercase + query = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.movieId"; query = String.format(query, actorId); result = this.session.run(query); resultValues = result.list(); @@ -112,4 +112,61 @@ public String getActor(String actorId) throws JSONException { response.put("movies", movies); return response.toString(); } + public String getMovie(String movieId) throws JSONException { + JSONObject response = new JSONObject(); + String query; + query = "MATCH (m:movie { movieId: \"%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 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 { movieId: \"%s\"}) RETURN a.actorId"; + 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.actorId").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 { movieId: \"%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 { actorId: \"%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 { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie { movieId: \"%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(); + } } diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java index 60bfb05..8eba8c2 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -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; default: break; } @@ -158,7 +164,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); @@ -190,4 +196,69 @@ public void getActor(HttpExchange r) throws IOException, JSONException { r.sendResponseHeaders(500, -1); } } + 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); + } + } } \ No newline at end of file From 725a6f071c99c126d6a703a00f452cbe40f37ded Mon Sep 17 00:00:00 2001 From: Raymond Weng Date: Thu, 27 Oct 2022 18:57:03 -0400 Subject: [PATCH 2/2] deleteActor, deteMovie: Added api and tests Added new api to delete actor and movie for the tests. Added 200 status tests for getActor getMovie and hasRelationship --- .../java/ca/utoronto/utm/mcs/Neo4jDAO.java | 17 +++++ .../java/ca/utoronto/utm/mcs/ReqHandler.java | 50 +++++++++++++++ .../java/ca/utoronto/utm/mcs/AppTest.java | 64 +++++++++++++++++++ 3 files changed, 131 insertions(+) diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 0c242c4..0c19d6d 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -169,4 +169,21 @@ public String hasRelationship(String actorId, String movieId) throws JSONExcepti } 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); + } } diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java index 8eba8c2..116552c 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -61,6 +61,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; } @@ -261,4 +273,42 @@ public void hasRelationship(HttpExchange r) throws IOException { 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); + } + } } \ No newline at end of file diff --git a/src/test/java/ca/utoronto/utm/mcs/AppTest.java b/src/test/java/ca/utoronto/utm/mcs/AppTest.java index ed527a0..ecc88ac 100644 --- a/src/test/java/ca/utoronto/utm/mcs/AppTest.java +++ b/src/test/java/ca/utoronto/utm/mcs/AppTest.java @@ -78,4 +78,68 @@ public void addRelationshipPass() throws JSONException, IOException, Interrupted HttpResponse 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 setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq.toString()); + + JSONObject confirmReq = new JSONObject() + .put("actorId", "12345678901"); + HttpResponse confirmRes = sendRequest("/api/v1/getActor", "GET", confirmReq.toString()); + + JSONObject deleteReq = new JSONObject() + .put("actorId", "12345678901"); + HttpResponse 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 setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString()); + + JSONObject confirmReq = new JSONObject() + .put("movieId", "12345678901"); + HttpResponse confirmRes = sendRequest("/api/v1/getMovie", "GET", confirmReq.toString()); + + JSONObject deleteReq = new JSONObject() + .put("movieId", "12345678901"); + HttpResponse 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 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 confirmRes = sendRequest("/api/v1/hasRelationship", "GET", confirmReq.toString()); + + JSONObject deleteReq = new JSONObject() + .put("movieId", "12345678901"); + HttpResponse 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()); + } }