From 2bb0328206e757e3d9d130efafc6829ce2912cf3 Mon Sep 17 00:00:00 2001 From: Francois G Date: Fri, 17 Mar 2023 22:44:20 +0100 Subject: [PATCH] add tags support --- .../jeffcheasey88/peeratcode/model/Puzzle.java | 16 +++++++++++++--- .../repository/DatabaseRepository.java | 6 +++--- .../peeratcode/routes/ChapterElement.java | 1 + .../peeratcode/routes/PuzzleElement.java | 4 ++-- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java index 6c23ede..7495d53 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Puzzle.java +++ b/src/be/jeffcheasey88/peeratcode/model/Puzzle.java @@ -8,18 +8,20 @@ public class Puzzle { private byte[] soluce; private String verify; private int scoreMax; + private String tags; private int depend; - public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax){ - this(id, name, content, soluce, verify, scoreMax, -1); + public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags){ + this(id, name, content, soluce, verify, scoreMax, tags, -1); } - public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, int depend){ + public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags, int depend){ this.id = id; this.name = name; this.content = content; this.soluce = soluce; this.verify = verify; this.scoreMax = scoreMax; + this.tags = tags; this.depend = depend; } @@ -71,6 +73,14 @@ public class Puzzle { this.scoreMax = max; } + public String getTags(){ + return this.tags; + } + + public void setTags(int tags){ + this.scoreMax = tags; + } + public int getDepend(){ return this.depend; } diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 6dac7d3..f385784 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -23,9 +23,9 @@ import be.jeffcheasey88.peeratcode.model.Player; import be.jeffcheasey88.peeratcode.model.Puzzle; public class DatabaseRepository { - private static final String SPECIFIC_PUZZLE_QUERY = "SELECT p.*, np.origin FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next WHERE p.id_puzzle = ?;"; + private static final String SPECIFIC_PUZZLE_QUERY = "SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"; private static final String SPECIFIC_CHAPTER_QUERY = "SELECT * FROM chapters WHERE id_chapter = ?"; - private static final String PUZZLES_IN_CHAPTER_QUERY = "SELECT * FROM puzzles WHERE fk_chapter = ?"; + private static final String PUZZLES_IN_CHAPTER_QUERY = "SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle"; private static final String ALL_CHAPTERS_QUERY = "SELECT * FROM chapters WHERE id_chapter > 0"; private static final String CHECK_PSEUDO_AVAILABLE_QUERY = "SELECT * FROM players WHERE pseudo = ?"; private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?"; @@ -69,7 +69,7 @@ public class DatabaseRepository { private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException { return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"), - puzzleResult.getString("content"), null, "", 0, + puzzleResult.getString("content"), null, "", 0, puzzleResult.getString("tags"), hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1); } diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index 0c5d5e0..69fe8f0 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -38,6 +38,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()); 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 2c3f67b..d4224f9 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -33,8 +33,8 @@ public class PuzzleElement implements Response { puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("name", puzzle.getName()); puzzleJSON.put("content", puzzle.getContent()); - if (puzzle.getDepend() > 0) - puzzleJSON.put("depend", puzzle.getDepend()); + if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getTags()); + if (puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend()); writer.write(puzzleJSON.toJSONString()); } }