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.
This commit is contained in:
parent
27f0b3718b
commit
80563cd86e
6 changed files with 220 additions and 58 deletions
|
@ -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 = ?";
|
||||
}
|
107
src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java
Normal file
107
src/be/jeffcheasey88/peeratcode/repository/DatabaseRepo.java
Normal file
|
@ -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<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
|
||||
List<Puzzle> 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<Puzzle> 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<Chapter> getAllChapters() {
|
||||
try {
|
||||
List<Chapter> 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;
|
||||
}
|
||||
}
|
54
src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java
Normal file
54
src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java
Normal file
|
@ -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));
|
||||
}
|
||||
}
|
46
src/be/jeffcheasey88/peeratcode/routes/ChapterList.java
Normal file
46
src/be/jeffcheasey88/peeratcode/routes/ChapterList.java
Normal file
|
@ -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<Chapter> 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$");
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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\\/?$");
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue