PuzzleController
This commit is contained in:
parent
9807d94971
commit
d29f86e116
7 changed files with 81 additions and 179 deletions
|
@ -1,4 +1,4 @@
|
||||||
package dev.peerat.backend.routes.admins.chapter;
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
import static dev.peerat.framework.RequestType.DELETE;
|
import static dev.peerat.framework.RequestType.DELETE;
|
||||||
import static dev.peerat.framework.RequestType.POST;
|
import static dev.peerat.framework.RequestType.POST;
|
80
src/dev/peerat/backend/routes/admins/PuzzleController.java
Normal file
80
src/dev/peerat/backend/routes/admins/PuzzleController.java
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.DELETE;
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
import static dev.peerat.framework.RequestType.PUT;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
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;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class PuzzleController{
|
||||||
|
|
||||||
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
|
public PuzzleController(DatabaseRepository repo){
|
||||||
|
this.repo = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzles/$", needLogin = true)
|
||||||
|
public void gets(Context context, HttpWriter writer) throws Exception{
|
||||||
|
List<Puzzle> puzzles = repo.getAdminPuzzles();
|
||||||
|
JsonArray array = new JsonArray();
|
||||||
|
for (Puzzle puzzle : puzzles){
|
||||||
|
JsonMap puzzleJSON = new JsonMap();
|
||||||
|
puzzleJSON.set("id", puzzle.getId());
|
||||||
|
puzzleJSON.set("name", puzzle.getName());
|
||||||
|
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||||
|
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||||
|
puzzleJSON.set("show", puzzle.hasStarted());
|
||||||
|
array.add(puzzleJSON);
|
||||||
|
}
|
||||||
|
context.response(200);
|
||||||
|
writer.write(array.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/(\\d+)$", needLogin = true)
|
||||||
|
public void get(Matcher matcher, Context context, HttpWriter writer) throws Exception{
|
||||||
|
Puzzle puzzle = this.repo.getAdminPuzzle(Integer.parseInt(matcher.group(1)));
|
||||||
|
JsonMap json = new JsonMap();
|
||||||
|
json.set("name", puzzle.getName());
|
||||||
|
json.set("content", puzzle.getContent());
|
||||||
|
json.set("soluce", new String(puzzle.getSoluce()));
|
||||||
|
json.set("score_max", puzzle.getScoreMax());
|
||||||
|
|
||||||
|
context.response(200);
|
||||||
|
writer.write(json.toString()+"\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/$", type = POST, needLogin = true)
|
||||||
|
public void add(Context context, HttpReader reader) throws Exception{
|
||||||
|
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);
|
||||||
|
performAction(context, repo.adminAddPuzzle(puzzle, json.<Long>get("chapter").intValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/(\\d+)$", type = PUT, needLogin = true)
|
||||||
|
public void edit(Matcher matcher, Context context, HttpReader reader) throws Exception{
|
||||||
|
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);
|
||||||
|
performAction(context, repo.adminUpdatePuzzle(Integer.parseInt(matcher.group(1)), puzzle, json.<Long>get("chapter").intValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/(\\d+)$", type = DELETE, needLogin = true)
|
||||||
|
public void delete(Matcher matcher, Context context) throws Exception{
|
||||||
|
performAction(context, this.repo.adminDeletePuzzle(Integer.parseInt(matcher.group(1))));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void performAction(Context context, boolean result) throws Exception{
|
||||||
|
context.response(result ? 200 : 501);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,35 +0,0 @@
|
||||||
package dev.peerat.backend.routes.admins.puzzle;
|
|
||||||
|
|
||||||
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;
|
|
||||||
import dev.peerat.framework.HttpWriter;
|
|
||||||
import dev.peerat.framework.Response;
|
|
||||||
import dev.peerat.framework.Route;
|
|
||||||
import dev.peerat.framework.utils.json.JsonMap;
|
|
||||||
|
|
||||||
public class AddPuzzle implements Response{
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
|
|
||||||
public AddPuzzle(DatabaseRepository repo){
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@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.<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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
package dev.peerat.backend.routes.admins.puzzle;
|
|
||||||
|
|
||||||
import static dev.peerat.framework.RequestType.DELETE;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import dev.peerat.backend.repository.DatabaseRepository;
|
|
||||||
import dev.peerat.framework.Context;
|
|
||||||
import dev.peerat.framework.HttpReader;
|
|
||||||
import dev.peerat.framework.HttpWriter;
|
|
||||||
import dev.peerat.framework.Response;
|
|
||||||
import dev.peerat.framework.Route;
|
|
||||||
import dev.peerat.framework.utils.json.JsonMap;
|
|
||||||
|
|
||||||
public class DeletePuzzle implements Response{
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
|
|
||||||
public DeletePuzzle(DatabaseRepository repo){
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Route(path = "^/admin/puzzle/(\\d+)$", type = DELETE, needLogin = true)
|
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
if(this.repo.adminDeletePuzzle(Integer.parseInt(matcher.group(1)))){
|
|
||||||
context.response(200);
|
|
||||||
}else{
|
|
||||||
context.response(501);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
package dev.peerat.backend.routes.admins.puzzle;
|
|
||||||
|
|
||||||
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;
|
|
||||||
import dev.peerat.framework.HttpWriter;
|
|
||||||
import dev.peerat.framework.Response;
|
|
||||||
import dev.peerat.framework.Route;
|
|
||||||
import dev.peerat.framework.utils.json.JsonMap;
|
|
||||||
|
|
||||||
public class EditPuzzle implements Response{
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
|
|
||||||
public EditPuzzle(DatabaseRepository repo){
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@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.<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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
package dev.peerat.backend.routes.admins.puzzle;
|
|
||||||
|
|
||||||
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;
|
|
||||||
import dev.peerat.framework.HttpWriter;
|
|
||||||
import dev.peerat.framework.Response;
|
|
||||||
import dev.peerat.framework.Route;
|
|
||||||
import dev.peerat.framework.utils.json.JsonMap;
|
|
||||||
|
|
||||||
public class GetPuzzle implements Response{
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
|
|
||||||
public GetPuzzle(DatabaseRepository repo){
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Route(path = "^/admin/puzzle/(\\d+)$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
Puzzle puzzle = this.repo.getAdminPuzzle(Integer.parseInt(matcher.group(1)));
|
|
||||||
JsonMap json = new JsonMap();
|
|
||||||
json.set("name", puzzle.getName());
|
|
||||||
json.set("content", puzzle.getContent());
|
|
||||||
json.set("soluce", new String(puzzle.getSoluce()));
|
|
||||||
json.set("score_max", puzzle.getScoreMax());
|
|
||||||
|
|
||||||
context.response(200);
|
|
||||||
writer.write(json.toString()+"\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,41 +0,0 @@
|
||||||
package dev.peerat.backend.routes.admins.puzzle;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
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;
|
|
||||||
import dev.peerat.framework.HttpWriter;
|
|
||||||
import dev.peerat.framework.Response;
|
|
||||||
import dev.peerat.framework.Route;
|
|
||||||
import dev.peerat.framework.utils.json.JsonArray;
|
|
||||||
import dev.peerat.framework.utils.json.JsonMap;
|
|
||||||
|
|
||||||
public class GetPuzzles implements Response{
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
|
|
||||||
public GetPuzzles(DatabaseRepository repo){
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Route(path = "^/admin/puzzles/$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
List<Puzzle> puzzles = repo.getAdminPuzzles();
|
|
||||||
JsonArray array = new JsonArray();
|
|
||||||
for (Puzzle puzzle : puzzles){
|
|
||||||
JsonMap puzzleJSON = new JsonMap();
|
|
||||||
puzzleJSON.set("id", puzzle.getId());
|
|
||||||
puzzleJSON.set("name", puzzle.getName());
|
|
||||||
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
|
||||||
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
|
||||||
puzzleJSON.set("show", puzzle.hasStarted());
|
|
||||||
array.add(puzzleJSON);
|
|
||||||
}
|
|
||||||
context.response(200);
|
|
||||||
writer.write(array.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue