diff --git a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java index 66785f7..74bc4c2 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java +++ b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java @@ -1,7 +1,12 @@ package be.jeffcheasey88.peeratcode.model; +import java.util.Arrays; +import java.util.HashSet; import java.util.Set; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + public class Puzzle { private int id; @@ -79,8 +84,25 @@ public class Puzzle { return this.tags; } + + /** + * DO NOT EVER EVER SHOW TO MISTER LUDWIG XD + * @return DEATH + */ + public JSONArray getJsonTags() { + if (tags == null) + return null; + JSONArray tagsJSON = new JSONArray(); + for (String tag: tags) { + JSONObject tagJSON = new JSONObject(); + tagJSON.put("name", tag); + tagsJSON.add(tagJSON); + } + return tagsJSON; + } + public void setTags(String tags){ - this.tags = null; + this.tags = new HashSet(Arrays.asList(tags.split(","))); } public int getDepend(){ diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index 0f3794f..8931419 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -39,7 +39,7 @@ public class ChapterElement implements Response { JSONObject puzzleJSON = new JSONObject(); puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("name", puzzle.getName()); - if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getTags()); + if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags()); puzzles.add(puzzleJSON); } chapterJSON.put("puzzles", puzzles); diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java index 8736e64..6db30f9 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -1,9 +1,11 @@ package be.jeffcheasey88.peeratcode.routes; import java.nio.charset.Charset; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.jose4j.json.internal.json_simple.JSONArray; import org.json.simple.JSONObject; import be.jeffcheasey88.peeratcode.model.Puzzle; @@ -26,16 +28,14 @@ public class PuzzleElement implements Response { @Route(path = "^\\/puzzle\\/([0-9]+)$") @Override public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { - HttpUtil.responseHeaders(writer, 200, - "Access-Control-Allow-Origin: *", - "Content-Type: application/json"); + HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); 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()); - if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getTags()); + if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags()); if (puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend()); writer.write(puzzleJSON.toJSONString()); }