change json tags to array

This commit is contained in:
Francois G 2023-03-18 23:24:49 +01:00
parent 1216f3f12a
commit f4d322e716
3 changed files with 28 additions and 6 deletions

View file

@ -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<String>(Arrays.asList(tags.split(",")));
}
public int getDepend(){

View file

@ -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);

View file

@ -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());
}