diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index e1e16d2..aabecea 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -7,7 +7,10 @@ import java.sql.DriverManager; import java.util.regex.Matcher; import java.util.regex.Pattern; -import be.jeffcheasey88.peeratcode.routes.PuzzleList; +import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; +import be.jeffcheasey88.peeratcode.routes.ChapterElement; +import be.jeffcheasey88.peeratcode.routes.ChapterList; +import be.jeffcheasey88.peeratcode.routes.PuzzleElement; import be.jeffcheasey88.peeratcode.webserver.Client; import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpUtil; @@ -40,7 +43,7 @@ public class Main { } }); - initRoutes(router, con); + initRoutes(router, new DatabaseRepository(con)); ServerSocket server = new ServerSocket(80); @@ -52,8 +55,10 @@ public class Main { } } - private static void initRoutes(Router router, Connection con){ - router.register(new PuzzleList(con)); + private static void initRoutes(Router router, DatabaseRepository repo){ + router.register(new ChapterElement(repo)); + router.register(new ChapterList(repo)); + router.register(new PuzzleElement(repo)); } } diff --git a/src/be/jeffcheasey88/peeratcode/model/Chapter.java b/src/be/jeffcheasey88/peeratcode/model/Chapter.java index 34c014c..8d98cad 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Chapter.java +++ b/src/be/jeffcheasey88/peeratcode/model/Chapter.java @@ -3,6 +3,7 @@ package be.jeffcheasey88.peeratcode.model; import java.util.List; public class Chapter { + private int id; private String name; private List puzzles; @@ -37,13 +38,10 @@ public class Chapter { } @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Chapter chapter = (Chapter) o; - - return id == chapter.id; + public boolean equals(Object object){ + if(this == object) return true; + if(!(object instanceof Chapter)) return false; + return this.id == (((Chapter)object).id); } @Override diff --git a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java index ec17add..2067536 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java +++ b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java @@ -1,6 +1,7 @@ package be.jeffcheasey88.peeratcode.model; public class Puzzle { + private int id; private String name; private String content; @@ -36,13 +37,10 @@ public class Puzzle { } @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - Puzzle puzzle = (Puzzle) o; - - return id == puzzle.id; + public boolean equals(Object object) { + if(this == object) return true; + if(!(object instanceof Puzzle)) return false; + return this.id == (((Puzzle)object).id); } @Override diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java similarity index 95% rename from src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java rename to src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 5465732..bdd8b7c 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -10,14 +10,15 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -public class DatabaseRepo { +public class DatabaseRepository { + private static final String SPECIFIC_PUZZLE_QUERY = "SELECT * FROM puzzle WHERE id_puzzle = ?"; private static final String SPECIFIC_CHAPTER_QUERY = "SELECT * FROM chapter WHERE id_chapter = ?"; private static final String PUZZLES_IN_CHAPTER_QUERY = "SELECT * FROM puzzle WHERE fk_chapter = ?"; - public static final String ALL_CHAPTERS_QUERY = "SELECT * FROM chapter"; + private static final String ALL_CHAPTERS_QUERY = "SELECT * FROM chapter"; private final Connection con; - public DatabaseRepo(Connection con) { + public DatabaseRepository(Connection con) { this.con = con; } diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index eb68faa..dab5d2b 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -2,7 +2,7 @@ package be.jeffcheasey88.peeratcode.routes; import be.jeffcheasey88.peeratcode.model.Chapter; import be.jeffcheasey88.peeratcode.model.Puzzle; -import be.jeffcheasey88.peeratcode.repository.DatabaseRepo; +import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpWriter; @@ -15,9 +15,9 @@ import java.util.regex.Pattern; public class ChapterElement implements Response { - private final DatabaseRepo databaseRepo; + private final DatabaseRepository databaseRepo; - public ChapterElement(DatabaseRepo databaseRepo) { + public ChapterElement(DatabaseRepository databaseRepo) { this.databaseRepo = databaseRepo; } diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java index 3402507..cc6abc7 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java @@ -1,7 +1,7 @@ package be.jeffcheasey88.peeratcode.routes; import be.jeffcheasey88.peeratcode.model.Chapter; -import be.jeffcheasey88.peeratcode.repository.DatabaseRepo; +import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpWriter; @@ -15,9 +15,9 @@ import java.util.regex.Pattern; public class ChapterList implements Response { - private final DatabaseRepo databaseRepo; + private final DatabaseRepository databaseRepo; - public ChapterList(DatabaseRepo databaseRepo) { + public ChapterList(DatabaseRepository databaseRepo) { this.databaseRepo = databaseRepo; } diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java index 0421de2..ec15de8 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -1,7 +1,7 @@ package be.jeffcheasey88.peeratcode.routes; import be.jeffcheasey88.peeratcode.model.Puzzle; -import be.jeffcheasey88.peeratcode.repository.DatabaseRepo; +import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpWriter; @@ -13,9 +13,9 @@ import java.util.regex.Pattern; public class PuzzleElement implements Response { - private final DatabaseRepo databaseRepo; + private final DatabaseRepository databaseRepo; - public PuzzleElement(DatabaseRepo databaseRepo) { + public PuzzleElement(DatabaseRepository databaseRepo) { this.databaseRepo = databaseRepo; }