Skip to content

Commit

Permalink
Merge branch 'dev' into feature/put-requests
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyZ1435 committed Oct 27, 2022
2 parents 25b3315 + 3e55669 commit b008b06
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<Record> 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();
}
}
41 changes: 41 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit b008b06

Please sign in to comment.