admin CRUD chapter & puzzle
This commit is contained in:
parent
3b98624df9
commit
7c3558f5a8
7 changed files with 95 additions and 11 deletions
|
@ -725,4 +725,62 @@ public class DatabaseRepository {
|
||||||
}
|
}
|
||||||
return null;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -25,9 +25,12 @@ public class AddChapter implements Response{
|
||||||
@Route(path = "^/admin/chapter/$", type = POST, needLogin = true)
|
@Route(path = "^/admin/chapter/$", type = POST, needLogin = true)
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
JsonMap json = reader.readJson();
|
JsonMap json = reader.readJson();
|
||||||
|
|
||||||
Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.<String>get("start")), Timestamp.valueOf(json.<String>get("end")));
|
Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.<String>get("start")), Timestamp.valueOf(json.<String>get("end")));
|
||||||
|
if(repo.adminAddChapter(chapter)){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,11 @@ public class DeleteChapter implements Response{
|
||||||
|
|
||||||
@Route(path = "^/admin/chapter/(\\d+)$", type = DELETE, needLogin = true)
|
@Route(path = "^/admin/chapter/(\\d+)$", type = DELETE, needLogin = true)
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,10 @@ package dev.peerat.backend.routes.admins.chapter;
|
||||||
|
|
||||||
import static dev.peerat.framework.RequestType.PUT;
|
import static dev.peerat.framework.RequestType.PUT;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
import dev.peerat.backend.repository.DatabaseRepository;
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
import dev.peerat.framework.Context;
|
import dev.peerat.framework.Context;
|
||||||
import dev.peerat.framework.HttpReader;
|
import dev.peerat.framework.HttpReader;
|
||||||
|
@ -23,7 +25,12 @@ public class EditChapter implements Response{
|
||||||
@Route(path = "^/admin/chapter/(\\d+)$", type = PUT, needLogin = true)
|
@Route(path = "^/admin/chapter/(\\d+)$", type = PUT, needLogin = true)
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
JsonMap json = reader.readJson();
|
JsonMap json = reader.readJson();
|
||||||
|
Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.<String>get("start")), Timestamp.valueOf(json.<String>get("end")));
|
||||||
|
if(repo.adminUpdateChapter(Integer.parseInt(matcher.group(1)), chapter)){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
import dev.peerat.backend.repository.DatabaseRepository;
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
import dev.peerat.framework.Context;
|
import dev.peerat.framework.Context;
|
||||||
import dev.peerat.framework.HttpReader;
|
import dev.peerat.framework.HttpReader;
|
||||||
|
@ -23,8 +24,12 @@ public class AddPuzzle implements Response{
|
||||||
@Route(path = "^/admin/puzzle/$", type = POST, needLogin = true)
|
@Route(path = "^/admin/puzzle/$", type = POST, needLogin = true)
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
JsonMap json = reader.readJson();
|
JsonMap json = reader.readJson();
|
||||||
|
Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.<String>get("soluce").getBytes(), null, json.<Long>get("scoreMax").intValue(), null, -1, null);
|
||||||
|
if(repo.adminAddPuzzle(puzzle, json.<Long>get("chapter").intValue())){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,9 +22,11 @@ public class DeletePuzzle implements Response{
|
||||||
|
|
||||||
@Route(path = "^/admin/puzzle/(\\d+)$", type = DELETE, needLogin = true)
|
@Route(path = "^/admin/puzzle/(\\d+)$", type = DELETE, needLogin = true)
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import static dev.peerat.framework.RequestType.PUT;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
import dev.peerat.backend.repository.DatabaseRepository;
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
import dev.peerat.framework.Context;
|
import dev.peerat.framework.Context;
|
||||||
import dev.peerat.framework.HttpReader;
|
import dev.peerat.framework.HttpReader;
|
||||||
|
@ -23,8 +24,12 @@ public class EditPuzzle implements Response{
|
||||||
@Route(path = "^/admin/puzzle/(\\d+)$", type = PUT, needLogin = true)
|
@Route(path = "^/admin/puzzle/(\\d+)$", type = PUT, needLogin = true)
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
JsonMap json = reader.readJson();
|
JsonMap json = reader.readJson();
|
||||||
|
Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.<String>get("soluce").getBytes(), null, json.<Long>get("scoreMax").intValue(), null, -1, null);
|
||||||
|
if(repo.adminUpdatePuzzle(Integer.parseInt(matcher.group(1)), puzzle, json.<Long>get("chapter").intValue())){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue