Skip to content

Commit

Permalink
DependencyInjection: created DI for reqHandler and Neo4jDAO
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyZ1435 committed Oct 27, 2022
1 parent 7ee45e8 commit 7a9924c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/main/java/ca/utoronto/utm/mcs/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();

}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandlerComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
17 changes: 17 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandlerModule.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit 7a9924c

Please sign in to comment.