Register merged routes in Main

This commit is contained in:
jeffcheasey88 2023-02-10 09:40:22 +01:00
parent 65177b21fc
commit 3693352e92
7 changed files with 32 additions and 30 deletions

View file

@ -7,7 +7,10 @@ import java.sql.DriverManager;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; 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.Client;
import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpReader;
import be.jeffcheasey88.peeratcode.webserver.HttpUtil; 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); ServerSocket server = new ServerSocket(80);
@ -52,8 +55,10 @@ public class Main {
} }
} }
private static void initRoutes(Router router, Connection con){ private static void initRoutes(Router router, DatabaseRepository repo){
router.register(new PuzzleList(con)); router.register(new ChapterElement(repo));
router.register(new ChapterList(repo));
router.register(new PuzzleElement(repo));
} }
} }

View file

@ -3,6 +3,7 @@ package be.jeffcheasey88.peeratcode.model;
import java.util.List; import java.util.List;
public class Chapter { public class Chapter {
private int id; private int id;
private String name; private String name;
private List<Puzzle> puzzles; private List<Puzzle> puzzles;
@ -37,13 +38,10 @@ public class Chapter {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object object){
if (this == o) return true; if(this == object) return true;
if (o == null || getClass() != o.getClass()) return false; if(!(object instanceof Chapter)) return false;
return this.id == (((Chapter)object).id);
Chapter chapter = (Chapter) o;
return id == chapter.id;
} }
@Override @Override

View file

@ -1,6 +1,7 @@
package be.jeffcheasey88.peeratcode.model; package be.jeffcheasey88.peeratcode.model;
public class Puzzle { public class Puzzle {
private int id; private int id;
private String name; private String name;
private String content; private String content;
@ -36,13 +37,10 @@ public class Puzzle {
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object object) {
if (this == o) return true; if(this == object) return true;
if (o == null || getClass() != o.getClass()) return false; if(!(object instanceof Puzzle)) return false;
return this.id == (((Puzzle)object).id);
Puzzle puzzle = (Puzzle) o;
return id == puzzle.id;
} }
@Override @Override

View file

@ -10,14 +10,15 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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_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 SPECIFIC_CHAPTER_QUERY = "SELECT * FROM chapter WHERE id_chapter = ?";
private static final String PUZZLES_IN_CHAPTER_QUERY = "SELECT * FROM puzzle WHERE fk_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; private final Connection con;
public DatabaseRepo(Connection con) { public DatabaseRepository(Connection con) {
this.con = con; this.con = con;
} }

View file

@ -2,7 +2,7 @@ package be.jeffcheasey88.peeratcode.routes;
import be.jeffcheasey88.peeratcode.model.Chapter; import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Puzzle; 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.HttpReader;
import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
import be.jeffcheasey88.peeratcode.webserver.HttpWriter; import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
@ -15,9 +15,9 @@ import java.util.regex.Pattern;
public class ChapterElement implements Response { public class ChapterElement implements Response {
private final DatabaseRepo databaseRepo; private final DatabaseRepository databaseRepo;
public ChapterElement(DatabaseRepo databaseRepo) { public ChapterElement(DatabaseRepository databaseRepo) {
this.databaseRepo = databaseRepo; this.databaseRepo = databaseRepo;
} }

View file

@ -1,7 +1,7 @@
package be.jeffcheasey88.peeratcode.routes; package be.jeffcheasey88.peeratcode.routes;
import be.jeffcheasey88.peeratcode.model.Chapter; 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.HttpReader;
import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
import be.jeffcheasey88.peeratcode.webserver.HttpWriter; import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
@ -15,9 +15,9 @@ import java.util.regex.Pattern;
public class ChapterList implements Response { public class ChapterList implements Response {
private final DatabaseRepo databaseRepo; private final DatabaseRepository databaseRepo;
public ChapterList(DatabaseRepo databaseRepo) { public ChapterList(DatabaseRepository databaseRepo) {
this.databaseRepo = databaseRepo; this.databaseRepo = databaseRepo;
} }

View file

@ -1,7 +1,7 @@
package be.jeffcheasey88.peeratcode.routes; package be.jeffcheasey88.peeratcode.routes;
import be.jeffcheasey88.peeratcode.model.Puzzle; 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.HttpReader;
import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
import be.jeffcheasey88.peeratcode.webserver.HttpWriter; import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
@ -13,9 +13,9 @@ import java.util.regex.Pattern;
public class PuzzleElement implements Response { public class PuzzleElement implements Response {
private final DatabaseRepo databaseRepo; private final DatabaseRepository databaseRepo;
public PuzzleElement(DatabaseRepo databaseRepo) { public PuzzleElement(DatabaseRepository databaseRepo) {
this.databaseRepo = databaseRepo; this.databaseRepo = databaseRepo;
} }