diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 7bb5a17..7d62998 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -1,6 +1,10 @@ package ca.utoronto.utm.mcs; +import org.json.*; import org.neo4j.driver.*; +import org.neo4j.driver.Record; + +import java.util.List; // All your database transactions or queries should // go in this class @@ -80,4 +84,29 @@ public int addRelationship(String actorId, String movieId) { this.session.run(query); return 200; } + + public String getActor(String actorId) throws JSONException { + JSONObject response = new JSONObject(); + String query; + query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.name"; + query = String.format(query, actorId); + + Result result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("No actor with this ID"); + return "404"; + } + List resultValues = result.list(); + 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 = String.format(query, actorId); + result = this.session.run(query); + resultValues = result.list(); + JSONArray movies = new JSONArray(); + resultValues.forEach((record)->{movies.put(record.get("m.movieId").asString());}); + response.put("movies", movies); + 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 cb0b473..4a77611 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -1,6 +1,7 @@ package ca.utoronto.utm.mcs; import java.io.IOException; +import java.io.OutputStream; import java.net.URI; import com.sun.net.httpserver.HttpExchange; @@ -28,6 +29,13 @@ public void handle(HttpExchange exchange) throws IOException { try { switch (exchange.getRequestMethod()) { case "GET": + switch (request) { + case "getActor": + this.getActor(exchange); + break; + default: + break; + } break; case "PUT": switch (request) { @@ -146,4 +154,37 @@ public void addRelationship(HttpExchange r) throws IOException, JSONException { r.sendResponseHeaders(500, -1); } } + + public void getActor(HttpExchange r) throws IOException, JSONException { + 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; + } + String response = this.dao.getActor(actorId); + 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