From b3b598cc0aa5e74d2e52adfd420f0b1cfe8b94f9 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Wed, 8 Feb 2023 23:30:38 +0100 Subject: [PATCH] Get PuzzleList from the database --- src/be/jeffcheasey88/peeratcode/Main.java | 6 +-- .../peeratcode/routes/PuzzleList.java | 37 ++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index b77f1aa..e1e16d2 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -40,7 +40,7 @@ public class Main { } }); - initRoutes(router); + initRoutes(router, con); ServerSocket server = new ServerSocket(80); @@ -52,8 +52,8 @@ public class Main { } } - private static void initRoutes(Router router){ - router.register(new PuzzleList()); + private static void initRoutes(Router router, Connection con){ + router.register(new PuzzleList(con)); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java index 6b33082..76b2c68 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleList.java @@ -1,8 +1,12 @@ package be.jeffcheasey88.peeratcode.routes; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.webserver.HttpReader; @@ -11,13 +15,44 @@ import be.jeffcheasey88.peeratcode.webserver.HttpWriter; import be.jeffcheasey88.peeratcode.webserver.Response; public class PuzzleList implements Response{ + + private Connection con; + + public PuzzleList(Connection con){ + this.con = con; + } @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"); + JSONArray chapters = new JSONArray(); + + PreparedStatement chapterStmt = con.prepareStatement("SELECT * FROM chapters"); + ResultSet chapterResults = chapterStmt.executeQuery(); + while(chapterResults.next()){ + JSONObject chapter = new JSONObject(); + chapter.put("id", chapterResults.getString("id_chapter")); + chapter.put("name", chapterResults.getString("name")); + chapters.add(chapter); + } + json.put("chapters", chapters); + + JSONArray puzzles = new JSONArray(); + + PreparedStatement puzzleStmt = con.prepareStatement("SELECT * FROM puzzles"); + ResultSet puzzleResults = puzzleStmt.executeQuery(); + while(puzzleResults.next()){ + JSONObject puzzle = new JSONObject(); + puzzle.put("id", puzzleResults.getString("id_puzzle")); + puzzle.put("name", puzzleResults.getString("name")); + puzzle.put("content", puzzleResults.getString("content")); + puzzle.put("chapter", puzzleResults.getString("fk_chapter")); + puzzles.add(puzzle); + } + json.put("puzzles", puzzles); + writer.write(json.toJSONString()); writer.flush(); writer.close();