Skip to content

Commit

Permalink
Merge pull request #8 from UTSCCSCC01/feature/getRequests
Browse files Browse the repository at this point in the history
Feature/get requests
  • Loading branch information
BillyZ1435 authored Oct 28, 2022
2 parents 075384c + bd41f66 commit 03fc9f6
Show file tree
Hide file tree
Showing 4 changed files with 513 additions and 70 deletions.
2 changes: 0 additions & 2 deletions src/main/java/ca/utoronto/utm/mcs/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import io.github.cdimascio.dotenv.Dotenv;
import java.io.IOException;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;

public class App
{
Expand Down
148 changes: 109 additions & 39 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.neo4j.driver.*;
import org.neo4j.driver.Record;


import java.util.List;

import javax.inject.Inject;
Expand All @@ -15,9 +14,7 @@ public class Neo4jDAO {
private final Session session;
private final Driver driver;

private final String uriDb = "bolt://localhost:7687";
private final String username = "neo4j";
private final String password = "123456";
private final String kevinBid = "nm0000102";

@Inject
public Neo4jDAO(Driver driver) {
Expand All @@ -26,96 +23,98 @@ public Neo4jDAO(Driver driver) {

}

public boolean addActor(String name, String actorId) {
public int addActor(String Name, String id) {
String query;
query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId";
query = String.format(query, actorId);
query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id";
query = String.format(query, id);
Result result = this.session.run(query);
if(result.hasNext()){
System.out.println("Actor Found: " + actorId);
return false;
System.out.println("Actor Found: " + id);
return 400;
}
query = "CREATE (a:actor {name: \"%s\", actorId: \"%s\"})";
query = String.format(query, name, actorId);
query = "CREATE (a:actor {Name: \"%s\", id: \"%s\"})";
query = String.format(query, Name, id);
this.session.run(query);
return true;
return 200;
}

public boolean addMovie(String name, String movieId) {
public int addMovie(String Name, String id) {
String query;
query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId";
query = String.format(query, movieId);
query = "MATCH (m:movie { id: \"%s\"}) RETURN m.id";
query = String.format(query, id);
Result result = this.session.run(query);
if(result.hasNext()){
System.out.println("Movie Found: " + movieId);
return false;
System.out.println("Movie Found: " + id);
return 400;
}
query = "CREATE (m:movie {name: \"%s\", movieId: \"%s\"})";
query = String.format(query, name, movieId);
query = "CREATE (m:movie {Name: \"%s\", id: \"%s\"})";
query = String.format(query, Name, id);
this.session.run(query);
return true;
return 200;
}

public int addRelationship(String actorId, String movieId) {
String query;
query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId";
query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id";
query = String.format(query, actorId);
Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("Actor not found: " + actorId);
return 404;
}

query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId";
query = "MATCH (m:movie { id: \"%s\"}) RETURN m.id";
query = String.format(query, movieId);
result = this.session.run(query);
if(!result.hasNext()){
System.out.println("Movie not found: " + movieId);
return 404;
}

query = "MATCH (a:actor {actorId: \"%s\"})-[:ACTED_IN]->(m:movie {movieId: \"%s\"}) RETURN a.actorId";
query = "MATCH (a:actor {id: \"%s\"})-[:ACTED_IN]->(m:movie {id: \"%s\"}) RETURN a.id";
query = String.format(query, actorId, movieId);
result = this.session.run(query);
if(result.hasNext()){
System.out.println("Relationship Found: " + movieId);
System.out.println("Relationship Found: " + actorId +"->"+ movieId);
return 400;
}

query = "MATCH (a:actor {actorId: \"%s\"}), (m:movie {movieId: \"%s\"}) CREATE (a)-[r:ACTED_IN]->(m)";
query = "MATCH (a:actor {id: \"%s\"}), (m:movie {id: \"%s\"}) CREATE (a)-[r:ACTED_IN]->(m)";
query = String.format(query, actorId, movieId);
this.session.run(query);
return 200;
}

public String getActor(String actorId) throws JSONException {
public String getActor(String id) throws JSONException {
JSONObject response = new JSONObject();
String query;
query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.name";
query = String.format(query, actorId);
query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id";
query = String.format(query, id);

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());
response.put("id", id);
response.put("Name", resultValues.get(0).get("a.Name").asString());

query = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.movieId";
query = String.format(query, actorId);
query = "MATCH (a:actor { id: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.id";//maybe change Movie to lowercase
query = String.format(query, id);
result = this.session.run(query);
resultValues = result.list();
JSONArray movies = new JSONArray();
resultValues.forEach((record)->{movies.put(record.get("m.movieId").asString());});
resultValues.forEach((record)->{movies.put(record.get("m.id").asString());});
//movies.put(record.get("m.id").asString());
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 = "MATCH (m:movie { id: \"%s\"}) RETURN m.name";
query = String.format(query, movieId);

Result result = this.session.run(query);
Expand All @@ -127,28 +126,28 @@ public String getMovie(String movieId) throws JSONException {
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 = "MATCH (a:actor)-[r:ACTED_IN]->(m:movie { id: \"%s\"}) RETURN a.id";
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());});
resultValues.forEach((record)->{actors.put(record.get("a.id").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 = "MATCH (m:movie { id: \"%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 = "MATCH (a:actor { id: \"%s\"}) RETURN a.name";
query = String.format(query, actorId);
result = this.session.run(query);
if(!result.hasNext()){
Expand All @@ -159,7 +158,7 @@ public String hasRelationship(String actorId, String movieId) throws JSONExcepti
response.put("movieId", movieId);
response.put("actorId", actorId);

query = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie { movieId: \"%s\"}) RETURN r";
query = "MATCH (a:actor { id: \"%s\"})-[r:ACTED_IN]->(m:movie { id: \"%s\"}) RETURN r";
query = String.format(query, actorId,movieId);
result = this.session.run(query);
if(!result.hasNext()){
Expand All @@ -169,4 +168,75 @@ public String hasRelationship(String actorId, String movieId) throws JSONExcepti
}
return response.toString();
}

public String computeBaconNumber(String actorId) throws JSONException{
JSONObject response = new JSONObject();
String query;
int baconNumber = 0;

if(actorId.equals(kevinBid)){
response.put("baconNumber", 0);
return response.toString();
}
query = "MATCH (a:actor {id: \"%s\"}), (KevinB:actor {id: \"%s\"} ), p = shortestPath((a)-[:ACTED_IN*]-(KevinB)) RETURN nodes(p)";
query = String.format(query, actorId, kevinBid);

Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No path");
return "404";
}
Record record = result.next();
for(int i=0; i<record.get(0).size(); i++){
if(record.get(0).get(i).asNode().hasLabel("actor")){
baconNumber++;
}
}
response.put("baconPath", baconNumber);
return response.toString();
}

public String computeBaconPath(String actorId) throws JSONException {
JSONObject response = new JSONObject();
String query;
JSONArray path = new JSONArray();

if(actorId.equals(kevinBid)){
path.put(kevinBid);
response.put("baconPath", kevinBid);
return response.toString();
}
query = "MATCH (a:actor {id: \"%s\"}), (KevinB:actor {id: \"%s\"} ), p = shortestPath((a)-[:ACTED_IN*]-(KevinB)) RETURN nodes(p)";
query = String.format(query, actorId, kevinBid);

Result result = this.session.run(query);
System.out.println(result);
if(!result.hasNext()){
System.out.println("No path");
return "404";
}
Record record = result.next();
for(int i=0; i<record.get(0).size(); i++){
path.put(record.get(0).get(i).get("id").asString());
}
response.put("baconPath", path);
return response.toString();
}

public void deleteMovie(String movieId) throws JSONException {
JSONObject response = new JSONObject();
String query;

query = "MATCH (m:movie { id: \"%s\"}) DETACH DELETE m";
query = String.format(query, movieId);
Result result = this.session.run(query);
}

public void deleteActor(String actorId) throws JSONException {
JSONObject response = new JSONObject();
String query;
query = "MATCH (a:actor { id: \"%s\"}) DETACH DELETE a";
query = String.format(query, actorId);
Result result = this.session.run(query);
}
}
Loading

0 comments on commit 03fc9f6

Please sign in to comment.