From 7dee5b60f9a2a923101e08a96cbd18dde25861bf Mon Sep 17 00:00:00 2001 From: Francois G Date: Mon, 10 Apr 2023 14:51:36 +0200 Subject: [PATCH] Make chapter or puzzle optional for group --- src/be/jeffcheasey88/peeratcode/model/Group.java | 14 ++++++++------ .../peeratcode/repository/DatabaseQuery.java | 2 +- .../peeratcode/repository/DatabaseRepository.java | 8 +++++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/model/Group.java b/src/be/jeffcheasey88/peeratcode/model/Group.java index 74a7c7b..8798326 100644 --- a/src/be/jeffcheasey88/peeratcode/model/Group.java +++ b/src/be/jeffcheasey88/peeratcode/model/Group.java @@ -11,14 +11,16 @@ import org.json.simple.JSONObject; public class Group implements Comparable { private String name; - private int linkToChapter; - private int linkToPuzzle; + private Integer linkToChapter; + private Integer linkToPuzzle; private List players; public Group(JSONObject json) { this.name = (String) json.get("name"); - this.linkToChapter = ((Number) json.get("chapter")).intValue(); - this.linkToPuzzle = ((Number) json.get("puzzle")).intValue(); + if (json.containsKey("chapter")) + this.linkToChapter = ((Number) json.get("chapter")).intValue(); + if (json.containsKey("puzzle")) + this.linkToPuzzle = ((Number) json.get("puzzle")).intValue(); } public Group(String name, int initChap, int initPuzz) { @@ -67,11 +69,11 @@ public class Group implements Comparable { return name; } - public int getLinkToChapter() { + public Integer getLinkToChapter() { return linkToChapter; } - public int getLinkToPuzzle() { + public Integer getLinkToPuzzle() { return linkToPuzzle; } diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java index a30a7a0..0d0eb57 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java @@ -17,7 +17,7 @@ public enum DatabaseQuery { // GROUPS ALL_GROUPS("SELECT * FROM groups"), - GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND fk_chapter = ? AND fk_puzzle = ?"), + GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ? OR fk_puzzle = ?)"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"), INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"), LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"), diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index 368f0d2..fc88541 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -504,8 +504,8 @@ public class DatabaseRepository { ensureConnection(); PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con); stmt.setString(1, group.getName()); - stmt.setInt(2, group.getLinkToChapter()); - stmt.setInt(3, group.getLinkToPuzzle()); + stmt.setObject(2, group.getLinkToChapter()); + stmt.setObject(3, group.getLinkToPuzzle()); ResultSet result = stmt.executeQuery(); if(result.next()) return result.getInt("id_group"); @@ -521,7 +521,9 @@ public class DatabaseRepository { stmt.setInt(2, id); return stmt.executeUpdate() >= 0; - }catch(Exception e){} + }catch(Exception e){ + e.printStackTrace(); + } return false; }