Skip to content

Commit

Permalink
Merge pull request #3 from UTSCCSCC01/feature/getRequests
Browse files Browse the repository at this point in the history
getActor: Create getActor api
  • Loading branch information
Amarygdala authored Oct 27, 2022
2 parents 84a9374 + 86c991c commit 3e55669
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
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 @@ -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<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();
}
}
43 changes: 42 additions & 1 deletion 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,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);
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 3e55669

Please sign in to comment.