diff --git a/src/dev/peerat/backend/repository/DatabaseRepository.java b/src/dev/peerat/backend/repository/DatabaseRepository.java index 68b386e..2bb299c 100644 --- a/src/dev/peerat/backend/repository/DatabaseRepository.java +++ b/src/dev/peerat/backend/repository/DatabaseRepository.java @@ -725,4 +725,62 @@ public class DatabaseRepository { } return null; } + + public boolean adminAddChapter(Chapter chapter) throws SQLException { + ensureConnection(); + PreparedStatement statement = DatabaseQuery.ADD_CHAPTER.prepare(this.con); + statement.setString(1, chapter.getName()); + statement.setTimestamp(2, chapter.getStartDate()); + statement.setTimestamp(3, chapter.getEndDate()); + return (statement.executeUpdate() >= 0); + } + + public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException { + ensureConnection(); + PreparedStatement statement = DatabaseQuery.ADD_PUZZLE.prepare(this.con); + statement.setString(1, puzzle.getName()); + statement.setString(2, puzzle.getContent()); + statement.setBytes(3, puzzle.getSoluce()); + statement.setString(4, ""); + statement.setInt(5, puzzle.getScoreMax()); + statement.setInt(6, chapter); + return (statement.executeUpdate() >= 0); + } + + public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException { + ensureConnection(); + PreparedStatement statement = DatabaseQuery.EDIT_CHAPTER.prepare(this.con); + statement.setString(1, chapter.getName()); + statement.setTimestamp(2, chapter.getStartDate()); + statement.setTimestamp(3, chapter.getEndDate()); + statement.setInt(4, id); + return (statement.executeUpdate() >= 0); + } + + public boolean adminUpdatePuzzle(int id, Puzzle puzzle, int chapter) throws SQLException { + ensureConnection(); + PreparedStatement statement = DatabaseQuery.EDIT_PUZZLE.prepare(this.con); + statement.setString(1, puzzle.getName()); + statement.setString(2, puzzle.getContent()); + statement.setBytes(3, puzzle.getSoluce()); + statement.setString(4, ""); + statement.setInt(5, puzzle.getScoreMax()); + statement.setInt(6, chapter); + statement.setInt(7, id); + return (statement.executeUpdate() >= 0); + } + + public boolean adminDeleteChapter(int id) throws SQLException { + ensureConnection(); + PreparedStatement statement = DatabaseQuery.DELETE_CHAPTER.prepare(this.con); + statement.setInt(1, id); + return (statement.executeUpdate() >= 0); + } + + public boolean adminDeletePuzzle(int id) throws SQLException { + ensureConnection(); + PreparedStatement statement = DatabaseQuery.DELETE_PUZZLE.prepare(this.con); + statement.setInt(1, id); + return (statement.executeUpdate() >= 0); + } } \ No newline at end of file diff --git a/src/dev/peerat/backend/routes/admins/chapter/AddChapter.java b/src/dev/peerat/backend/routes/admins/chapter/AddChapter.java index 0b6f66a..84f9dd5 100644 --- a/src/dev/peerat/backend/routes/admins/chapter/AddChapter.java +++ b/src/dev/peerat/backend/routes/admins/chapter/AddChapter.java @@ -25,9 +25,12 @@ public class AddChapter implements Response{ @Route(path = "^/admin/chapter/$", type = POST, needLogin = true) public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{ JsonMap json = reader.readJson(); - Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.get("start")), Timestamp.valueOf(json.get("end"))); - + if(repo.adminAddChapter(chapter)){ + context.response(200); + }else{ + context.response(501); + } } } diff --git a/src/dev/peerat/backend/routes/admins/chapter/DeleteChapter.java b/src/dev/peerat/backend/routes/admins/chapter/DeleteChapter.java index 53ae487..1097d0a 100644 --- a/src/dev/peerat/backend/routes/admins/chapter/DeleteChapter.java +++ b/src/dev/peerat/backend/routes/admins/chapter/DeleteChapter.java @@ -21,7 +21,11 @@ public class DeleteChapter implements Response{ @Route(path = "^/admin/chapter/(\\d+)$", type = DELETE, needLogin = true) public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{ - int id = Integer.parseInt(matcher.group(1)); + if(this.repo.adminDeleteChapter(Integer.parseInt(matcher.group(1)))){ + context.response(200); + }else{ + context.response(501); + } } } diff --git a/src/dev/peerat/backend/routes/admins/chapter/EditChapter.java b/src/dev/peerat/backend/routes/admins/chapter/EditChapter.java index a8069f0..12c8f6d 100644 --- a/src/dev/peerat/backend/routes/admins/chapter/EditChapter.java +++ b/src/dev/peerat/backend/routes/admins/chapter/EditChapter.java @@ -2,8 +2,10 @@ package dev.peerat.backend.routes.admins.chapter; import static dev.peerat.framework.RequestType.PUT; +import java.sql.Timestamp; import java.util.regex.Matcher; +import dev.peerat.backend.model.Chapter; import dev.peerat.backend.repository.DatabaseRepository; import dev.peerat.framework.Context; import dev.peerat.framework.HttpReader; @@ -23,7 +25,12 @@ public class EditChapter implements Response{ @Route(path = "^/admin/chapter/(\\d+)$", type = PUT, needLogin = true) public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{ JsonMap json = reader.readJson(); - + Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.get("start")), Timestamp.valueOf(json.get("end"))); + if(repo.adminUpdateChapter(Integer.parseInt(matcher.group(1)), chapter)){ + context.response(200); + }else{ + context.response(501); + } } diff --git a/src/dev/peerat/backend/routes/admins/puzzle/AddPuzzle.java b/src/dev/peerat/backend/routes/admins/puzzle/AddPuzzle.java index 6f1114a..02117dc 100644 --- a/src/dev/peerat/backend/routes/admins/puzzle/AddPuzzle.java +++ b/src/dev/peerat/backend/routes/admins/puzzle/AddPuzzle.java @@ -4,6 +4,7 @@ import static dev.peerat.framework.RequestType.POST; import java.util.regex.Matcher; +import dev.peerat.backend.model.Puzzle; import dev.peerat.backend.repository.DatabaseRepository; import dev.peerat.framework.Context; import dev.peerat.framework.HttpReader; @@ -23,8 +24,12 @@ public class AddPuzzle implements Response{ @Route(path = "^/admin/puzzle/$", type = POST, needLogin = true) public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{ JsonMap json = reader.readJson(); - - + Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.get("soluce").getBytes(), null, json.get("scoreMax").intValue(), null, -1, null); + if(repo.adminAddPuzzle(puzzle, json.get("chapter").intValue())){ + context.response(200); + }else{ + context.response(501); + } } } diff --git a/src/dev/peerat/backend/routes/admins/puzzle/DeletePuzzle.java b/src/dev/peerat/backend/routes/admins/puzzle/DeletePuzzle.java index a2af169..ba068e3 100644 --- a/src/dev/peerat/backend/routes/admins/puzzle/DeletePuzzle.java +++ b/src/dev/peerat/backend/routes/admins/puzzle/DeletePuzzle.java @@ -22,9 +22,11 @@ public class DeletePuzzle implements Response{ @Route(path = "^/admin/puzzle/(\\d+)$", type = DELETE, needLogin = true) public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{ - JsonMap json = reader.readJson(); - - + if(this.repo.adminDeletePuzzle(Integer.parseInt(matcher.group(1)))){ + context.response(200); + }else{ + context.response(501); + } } } diff --git a/src/dev/peerat/backend/routes/admins/puzzle/EditPuzzle.java b/src/dev/peerat/backend/routes/admins/puzzle/EditPuzzle.java index 05dc415..969ba40 100644 --- a/src/dev/peerat/backend/routes/admins/puzzle/EditPuzzle.java +++ b/src/dev/peerat/backend/routes/admins/puzzle/EditPuzzle.java @@ -4,6 +4,7 @@ import static dev.peerat.framework.RequestType.PUT; import java.util.regex.Matcher; +import dev.peerat.backend.model.Puzzle; import dev.peerat.backend.repository.DatabaseRepository; import dev.peerat.framework.Context; import dev.peerat.framework.HttpReader; @@ -23,8 +24,12 @@ public class EditPuzzle implements Response{ @Route(path = "^/admin/puzzle/(\\d+)$", type = PUT, needLogin = true) public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{ JsonMap json = reader.readJson(); - - + Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.get("soluce").getBytes(), null, json.get("scoreMax").intValue(), null, -1, null); + if(repo.adminUpdatePuzzle(Integer.parseInt(matcher.group(1)), puzzle, json.get("chapter").intValue())){ + context.response(200); + }else{ + context.response(501); + } } }