Skip to content

Commit

Permalink
getMovie, hasRelationship: added new apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Amarygdala committed Oct 27, 2022
1 parent a021086 commit 229250d
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
59 changes: 58 additions & 1 deletion src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Record> 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();
}
}
73 changes: 72 additions & 1 deletion src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 229250d

Please sign in to comment.