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()); + } }