From 80563cd86eb76cee4aea29349e747628c45bff01 Mon Sep 17 00:00:00 2001 From: TheNagaki Date: Thu, 9 Feb 2023 22:42:33 +0100 Subject: [PATCH] Added ChapterElement and ChapterList as routes to respectively get a chapter by id and all the chapters. Removed DatabaseQueries class and added DatabaseRepo to contain all the database queries; Edited PuzzleElement to remove all database-related code. --- .../repository/DatabaseQueries.java | 5 - .../peeratcode/repository/DatabaseRepo.java | 107 ++++++++++++++++++ .../peeratcode/routes/ChapterElement.java | 54 +++++++++ .../peeratcode/routes/ChapterList.java | 46 ++++++++ .../peeratcode/routes/PuzzleElement.java | 35 +++--- .../peeratcode/routes/PuzzleList.java | 31 ----- 6 files changed, 220 insertions(+), 58 deletions(-) delete mode 100644 src/be/jeffcheasey88/peeratcode/repository/DatabaseQueries.java create mode 100644 src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java create mode 100644 src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java create mode 100644 src/be/jeffcheasey88/peeratcode/routes/ChapterList.java delete mode 100644 src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQueries.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQueries.java deleted file mode 100644 index 370ffc9..0000000 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQueries.java +++ /dev/null @@ -1,5 +0,0 @@ -package be.jeffcheasey88.peeratcode.repository; - -public class DatabaseQueries { - public static final String SPECIFIC_PUZZLE_QUERY = "SELECT * FROM puzzle WHERE id_puzzle = ?"; -} diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java new file mode 100644 index 0000000..5465732 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java @@ -0,0 +1,107 @@ +package be.jeffcheasey88.peeratcode.repository; + +import be.jeffcheasey88.peeratcode.model.Chapter; +import be.jeffcheasey88.peeratcode.model.Puzzle; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class DatabaseRepo { + 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 final Connection con; + + public DatabaseRepo(Connection con) { + this.con = con; + } + + private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException { + return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"), puzzleResult.getString("content")); + } + + private Chapter makeChapter(ResultSet chapterResult) throws SQLException { + return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name")); + } + + private List getPuzzlesInChapter(int id) throws SQLException { + List puzzles = new ArrayList<>(); + PreparedStatement puzzleStmt = con.prepareStatement(PUZZLES_IN_CHAPTER_QUERY); + puzzleStmt.setInt(1, id); + ResultSet puzzleResult = puzzleStmt.executeQuery(); + while (puzzleResult.next()) { + puzzles.add(makePuzzle(puzzleResult)); + } + return puzzles; + } + + /** + * Get a specific puzzle + * + * @param id The id of the puzzle + * @return The puzzle or null if an error occurred + */ + public Puzzle getPuzzle(int id) { + try { + PreparedStatement puzzleStmt = con.prepareStatement(SPECIFIC_PUZZLE_QUERY); + puzzleStmt.setInt(1, id); + ResultSet puzzleResult = puzzleStmt.executeQuery(); + if (puzzleResult.next()) { + return makePuzzle(puzzleResult); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + /** + * Get a specific chapter + * + * @param id The id of the chapter + * @return The chapter or null if an error occurred + */ + public Chapter getChapter(int id) { + try { + PreparedStatement chapterStmt = con.prepareStatement(SPECIFIC_CHAPTER_QUERY); + chapterStmt.setInt(1, id); + ResultSet chapterResult = chapterStmt.executeQuery(); + if (chapterResult.next()) { + Chapter chapter = makeChapter(chapterResult); + List puzzles = getPuzzlesInChapter(id); + chapter.setPuzzles(puzzles); + return chapter; + } + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + /** + * Get all chapters in the database + * + * @return List of all chapters or null if an error occurred + */ + public List getAllChapters() { + try { + List chapterList = new ArrayList<>(); + PreparedStatement chapterStmt = con.prepareStatement(ALL_CHAPTERS_QUERY); + ResultSet chapterResult = chapterStmt.executeQuery(); + while (chapterResult.next()) { + Chapter chapter = makeChapter(chapterResult); + chapter.setPuzzles(getPuzzlesInChapter(chapter.getId())); + chapterList.add(chapter); + } + return chapterList; + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } +} \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java new file mode 100644 index 0000000..eb68faa --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -0,0 +1,54 @@ +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.webserver.HttpReader; +import be.jeffcheasey88.peeratcode.webserver.HttpUtil; +import be.jeffcheasey88.peeratcode.webserver.HttpWriter; +import be.jeffcheasey88.peeratcode.webserver.Response; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ChapterElement implements Response { + + private final DatabaseRepo databaseRepo; + + public ChapterElement(DatabaseRepo databaseRepo) { + this.databaseRepo = databaseRepo; + } + + @Override + public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception { + HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + Chapter chapter = databaseRepo.getChapter(extractId(matcher)); + if (chapter != null) { + JSONObject chapterJSON = new JSONObject(); + chapterJSON.put("id", chapter.getId()); + chapterJSON.put("name", chapter.getName()); + JSONArray puzzles = new JSONArray(); + for (Puzzle puzzle : chapter.getPuzzles()) { + JSONObject puzzleJSON = new JSONObject(); + puzzleJSON.put("id", puzzle.getId()); + puzzleJSON.put("name", puzzle.getName()); + puzzles.add(puzzleJSON); + } + chapterJSON.put("puzzles", puzzles); + writer.write(chapterJSON.toJSONString()); + } + writer.flush(); + writer.close(); + } + + @Override + public Pattern getPattern() { + return Pattern.compile("^\\/chapter\\/([0-9]+)$"); + } + + private int extractId(Matcher matcher) { + return Integer.parseInt(matcher.group(1)); + } +} diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java new file mode 100644 index 0000000..3402507 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterList.java @@ -0,0 +1,46 @@ +package be.jeffcheasey88.peeratcode.routes; + +import be.jeffcheasey88.peeratcode.model.Chapter; +import be.jeffcheasey88.peeratcode.repository.DatabaseRepo; +import be.jeffcheasey88.peeratcode.webserver.HttpReader; +import be.jeffcheasey88.peeratcode.webserver.HttpUtil; +import be.jeffcheasey88.peeratcode.webserver.HttpWriter; +import be.jeffcheasey88.peeratcode.webserver.Response; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ChapterList implements Response { + + private final DatabaseRepo databaseRepo; + + public ChapterList(DatabaseRepo databaseRepo) { + this.databaseRepo = databaseRepo; + } + + @Override + public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception { + HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + List allChapters = databaseRepo.getAllChapters(); + if (allChapters != null) { + JSONArray chaptersJSON = new JSONArray(); + for (Chapter chapter : allChapters) { + JSONObject chapterJSON = new JSONObject(); + chapterJSON.put("id", chapter.getId()); + chapterJSON.put("name", chapter.getName()); + chaptersJSON.add(chapterJSON); + } + writer.write(chaptersJSON.toJSONString()); + } + writer.flush(); + writer.close(); + } + + @Override + public Pattern getPattern() { + return Pattern.compile("^\\/chapters$"); + } +} diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java index 33cdfe0..0421de2 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -1,43 +1,34 @@ package be.jeffcheasey88.peeratcode.routes; +import be.jeffcheasey88.peeratcode.model.Puzzle; +import be.jeffcheasey88.peeratcode.repository.DatabaseRepo; import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpUtil; import be.jeffcheasey88.peeratcode.webserver.HttpWriter; import be.jeffcheasey88.peeratcode.webserver.Response; import org.json.simple.JSONObject; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static be.jeffcheasey88.peeratcode.repository.DatabaseQueries.SPECIFIC_PUZZLE_QUERY; - public class PuzzleElement implements Response { - private final Connection con; - public PuzzleElement(Connection con) { - this.con = con; + private final DatabaseRepo databaseRepo; + + public PuzzleElement(DatabaseRepo databaseRepo) { + this.databaseRepo = databaseRepo; } @Override public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); - - JSONObject puzzle = new JSONObject(); - - PreparedStatement puzzleStmt = con.prepareStatement(SPECIFIC_PUZZLE_QUERY); - puzzleStmt.setInt(1, extractId(matcher)); - - ResultSet puzzleResult = puzzleStmt.executeQuery(); - if (puzzleResult.next()) { - puzzle.put("id", puzzleResult.getString("id_puzzle")); - puzzle.put("name", puzzleResult.getString("name")); - puzzle.put("content", puzzleResult.getString("content")); - puzzle.put("chapter", puzzleResult.getString("fk_chapter")); - - writer.write(puzzle.toJSONString()); + Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher)); + if (puzzle != null) { + JSONObject puzzleJSON = new JSONObject(); + puzzleJSON.put("id", puzzle.getId()); + puzzleJSON.put("name", puzzle.getName()); + puzzleJSON.put("content", puzzle.getContent()); + writer.write(puzzleJSON.toJSONString()); } writer.flush(); writer.close(); diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java deleted file mode 100644 index 6b33082..0000000 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java +++ /dev/null @@ -1,31 +0,0 @@ -package be.jeffcheasey88.peeratcode.routes; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.json.simple.JSONObject; - -import be.jeffcheasey88.peeratcode.webserver.HttpReader; -import be.jeffcheasey88.peeratcode.webserver.HttpUtil; -import be.jeffcheasey88.peeratcode.webserver.HttpWriter; -import be.jeffcheasey88.peeratcode.webserver.Response; - -public class PuzzleList implements Response{ - - @Override - public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception { - HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); - - JSONObject json = new JSONObject(); - json.put("Theo", "GL HF"); - writer.write(json.toJSONString()); - writer.flush(); - writer.close(); - } - - @Override - public Pattern getPattern(){ - return Pattern.compile("^\\/puzzle\\/?$"); - } - -}