From 86c991c455a1ee5181bc1668d776001c7b64a0ca Mon Sep 17 00:00:00 2001 From: Raymond Weng Date: Wed, 26 Oct 2022 20:16:23 -0400 Subject: [PATCH] getActor: Create getActor api --- .../java/ca/utoronto/utm/mcs/Neo4jDAO.java | 29 +++++++++++++ .../java/ca/utoronto/utm/mcs/ReqHandler.java | 43 ++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index a72043e..6685a9f 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 @@ -33,4 +37,29 @@ public boolean addActor(String name, String actorId) { this.session.run(query); return true; } + + 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 e90ca21..83895a8 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,8 +29,15 @@ 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 "POST": + case "PUT": switch (request) { case "addActor": this.addActor(exchange); @@ -78,4 +86,37 @@ public void addActor(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