diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 6685a9f..7d62998 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -38,6 +38,53 @@ public boolean addActor(String name, String actorId) { return true; } + public boolean addMovie(String name, String movieId) { + String query; + query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId"; + query = String.format(query, movieId); + Result result = this.session.run(query); + if(result.hasNext()){ + System.out.println("Movie Found: " + movieId); + return false; + } + query = "CREATE (m:movie {name: \"%s\", movieId: \"%s\"})"; + query = String.format(query, name, movieId); + this.session.run(query); + return true; + } + + public int addRelationship(String actorId, String movieId) { + String query; + query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId"; + query = String.format(query, actorId); + Result result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("Actor not found: " + actorId); + return 404; + } + + query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId"; + query = String.format(query, movieId); + result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("Movie not found: " + movieId); + return 404; + } + + query = "MATCH (a:actor {actorId: \"%s\"})-[:ACTED_IN]->(m:movie {movieId: \"%s\"}) RETURN a.actorId"; + query = String.format(query, actorId, movieId); + result = this.session.run(query); + if(result.hasNext()){ + System.out.println("Relationship Found: " + movieId); + return 400; + } + + query = "MATCH (a:actor {actorId: \"%s\"}), (m:movie {movieId: \"%s\"}) CREATE (a)-[r:ACTED_IN]->(m)"; + query = String.format(query, actorId, movieId); + this.session.run(query); + return 200; + } + public String getActor(String actorId) throws JSONException { JSONObject response = new JSONObject(); String query; diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java index 83895a8..d7971b5 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -42,6 +42,12 @@ public void handle(HttpExchange exchange) throws IOException { case "addActor": this.addActor(exchange); break; + case "addMovie": + this.addMovie(exchange); + break; + case "addRelationship": + this.addRelationship(exchange); + break; default: break; } @@ -62,7 +68,7 @@ public void addActor(HttpExchange r) throws IOException, JSONException { String name, actorId; - if (deserialized.length() == 2 && deserialized.has("name") && deserialized.has("actorId")) { + if (deserialized.has("name") && deserialized.has("actorId")) { name = deserialized.getString("name"); actorId = deserialized.getString("actorId"); } else { @@ -87,6 +93,68 @@ public void addActor(HttpExchange r) throws IOException, JSONException { } } + public void addMovie(HttpExchange r) throws IOException, JSONException { + String body = Utils.convert(r.getRequestBody()); + try { + JSONObject deserialized = new JSONObject(body); + + String name, movieId; + + if (deserialized.has("name") && deserialized.has("movieId")) { + name = deserialized.getString("name"); + movieId = deserialized.getString("movieId"); + } else { + r.sendResponseHeaders(400, -1); + return; + } + + try { + if(this.dao.addMovie(name, movieId) == false){ + r.sendResponseHeaders(400, -1); + return; + } + } catch (Exception e) { + r.sendResponseHeaders(500, -1); + e.printStackTrace(); + return; + } + r.sendResponseHeaders(200, -1); + } catch (Exception e) { + e.printStackTrace(); + r.sendResponseHeaders(500, -1); + } + } + + public void addRelationship(HttpExchange r) throws IOException, JSONException { + String body = Utils.convert(r.getRequestBody()); + try { + JSONObject deserialized = new JSONObject(body); + + String actorId, movieId; + + if (deserialized.has("actorId") && deserialized.has("movieId")) { + actorId = deserialized.getString("actorId"); + movieId = deserialized.getString("movieId"); + } else { + r.sendResponseHeaders(400, -1); + return; + } + + try { + int rCode = this.dao.addRelationship(actorId, movieId); + r.sendResponseHeaders(rCode, -1); + return; + } catch (Exception e) { + r.sendResponseHeaders(500, -1); + e.printStackTrace(); + return; + } + } catch (Exception e) { + e.printStackTrace(); + r.sendResponseHeaders(500, -1); + } + } + public void getActor(HttpExchange r) throws IOException, JSONException { String body = Utils.convert(r.getRequestBody()); try { diff --git a/src/test/java/ca/utoronto/utm/mcs/AppTest.java b/src/test/java/ca/utoronto/utm/mcs/AppTest.java index 9e49f18..ed527a0 100644 --- a/src/test/java/ca/utoronto/utm/mcs/AppTest.java +++ b/src/test/java/ca/utoronto/utm/mcs/AppTest.java @@ -37,12 +37,45 @@ public void exampleTest() { assertTrue(true); } + // @Test + // public void addActorPass() throws JSONException, IOException, InterruptedException { + // JSONObject confirmReq = new JSONObject() + // .put("name", "TestActor") + // .put("actorId", "12345678901"); + // HttpResponse confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString()); + // assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + // } + + @Test + public void addActorFail() throws JSONException, IOException, InterruptedException { + JSONObject confirmReq = new JSONObject() + .put("name", "TestActor"); + HttpResponse confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); + } + + // @Test + // public void addMoviePass() throws JSONException, IOException, InterruptedException { + // JSONObject confirmReq = new JSONObject() + // .put("name", "TestMovie") + // .put("movieId", "12345678901"); + // HttpResponse confirmRes = sendRequest("/api/v1/addMovie", "PUT", confirmReq.toString()); + // assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + // } + + @Test + public void addMovieFail() throws JSONException, IOException, InterruptedException { + JSONObject confirmReq = new JSONObject() + .put("name", "TestMovie"); + HttpResponse confirmRes = sendRequest("/api/v1/addMovie", "PUT", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); + } + @Test - public void addActorPass() throws JSONException, IOException, InterruptedException { + public void addRelationshipPass() throws JSONException, IOException, InterruptedException { JSONObject confirmReq = new JSONObject() - .put("name", "TestActor") - .put("secondNumber", "1234567890"); - HttpResponse confirmRes = sendRequest("/api/v1/addActor", "POST", confirmReq.toString()); - assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + .put("movieId", "x"); + HttpResponse confirmRes = sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); } }