Get PuzzleList from the database
This commit is contained in:
parent
a3b1c06455
commit
b3b598cc0a
2 changed files with 39 additions and 4 deletions
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue