diff --git a/src/main/java/ca/utoronto/utm/mcs/App.java b/src/main/java/ca/utoronto/utm/mcs/App.java index f087b96..e20c5bf 100644 --- a/src/main/java/ca/utoronto/utm/mcs/App.java +++ b/src/main/java/ca/utoronto/utm/mcs/App.java @@ -11,8 +11,9 @@ public class App public static void main(String[] args) throws IOException { + ReqHandlerComponent component = DaggerReqHandlerComponent.create(); HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0); - server.createContext("/api/v1/", new ReqHandler()); + server.createContext("/api/v1/", component.buildHandler()); server.start(); // TODO Create Your Server Context Here, There Should Only Be One Context diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 7d62998..0ad3921 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -4,8 +4,11 @@ import org.neo4j.driver.*; import org.neo4j.driver.Record; + import java.util.List; +import javax.inject.Inject; + // All your database transactions or queries should // go in this class public class Neo4jDAO { @@ -16,9 +19,9 @@ public class Neo4jDAO { private final String username = "neo4j"; private final String password = "123456"; - - public Neo4jDAO() { - this.driver = GraphDatabase.driver(this.uriDb, AuthTokens.basic(this.username, this.password)); + @Inject + public Neo4jDAO(Driver driver) { + this.driver = driver; this.session = this.driver.session(); } diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java index d7971b5..60bfb05 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -4,6 +4,8 @@ import java.io.OutputStream; import java.net.URI; +import javax.inject.Inject; + import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import org.json.JSONException; @@ -13,10 +15,11 @@ public class ReqHandler implements HttpHandler { // TODO Complete This Class - public Neo4jDAO dao; + private Neo4jDAO dao; - public ReqHandler() { - this.dao = new Neo4jDAO(); + @Inject + public ReqHandler(Neo4jDAO dao) { + this.dao = dao; } @Override diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandlerComponent.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandlerComponent.java index 9ed6d5e..900e72d 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandlerComponent.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandlerComponent.java @@ -5,8 +5,8 @@ @Singleton // TODO Uncomment The Line Below When You Have Implemented ReqHandlerModule -// @Component(modules = ReqHandlerModule.class) +@Component(modules = ReqHandlerModule.class) public interface ReqHandlerComponent { - + public ReqHandler buildHandler(); } diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandlerModule.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandlerModule.java index 88d65ec..bdf91d1 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandlerModule.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandlerModule.java @@ -1,8 +1,25 @@ package ca.utoronto.utm.mcs; import dagger.Module; +import dagger.Provides; +import org.neo4j.driver.*; + @Module public class ReqHandlerModule { // TODO Complete This Module + private final String uriDb = "bolt://localhost:7687"; + private final String username = "neo4j"; + private final String password = "123456"; + + @Provides + public Neo4jDAO provideNeo4jDAO(Driver driver) { + + return new Neo4jDAO(driver); + } + + @Provides + public Driver provideDriver(){ + return GraphDatabase.driver(this.uriDb, AuthTokens.basic(this.username, this.password)); + } }