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);
|
ServerSocket server = new ServerSocket(80);
|
||||||
|
@ -52,8 +52,8 @@ public class Main {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
private static void initRoutes(Router router){
|
private static void initRoutes(Router router, Connection con){
|
||||||
router.register(new PuzzleList());
|
router.register(new PuzzleList(con));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
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.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||||
|
@ -12,12 +16,43 @@ import be.jeffcheasey88.peeratcode.webserver.Response;
|
||||||
|
|
||||||
public class PuzzleList implements Response{
|
public class PuzzleList implements Response{
|
||||||
|
|
||||||
|
private Connection con;
|
||||||
|
|
||||||
|
public PuzzleList(Connection con){
|
||||||
|
this.con = con;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
|
|
||||||
JSONObject json = new JSONObject();
|
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.write(json.toJSONString());
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|
Loading…
Add table
Reference in a new issue