diff --git a/src/uk/ac/bris/cs/databases/cwk2/API.java b/src/uk/ac/bris/cs/databases/cwk2/API.java index 9ebdb87..b6dbb30 100644 --- a/src/uk/ac/bris/cs/databases/cwk2/API.java +++ b/src/uk/ac/bris/cs/databases/cwk2/API.java @@ -121,27 +121,10 @@ public Result addNewPerson(String name, String username, String studentId) { return Result.failure(validation); } -// if (studentId != null && studentId.equals("")) { -// return Result.failure("StudentId can be null, but cannot be the empty string."); -// } -// -// if (name == null || name.equals("")) { -// return Result.failure("Name cannot be empty."); -// } -// if (username == null || username.equals("")) { -// return Result.failure("Username cannot be empty."); -// } -// if (name.length() > getMaxNameLength()) { -// return Result.failure("Name is too long. Max length a hundred characters."); -// } -// -// if (username.length() > getMaxUsernameSize()) { -// return Result.failure("Username is too long. Max length ten characters."); -// } + String query = "SELECT count(1) AS c FROM Person WHERE username = ?"; - try (PreparedStatement p = c.prepareStatement( - "SELECT count(1) AS c FROM Person WHERE username = ?" - )) { + try (PreparedStatement p = c.prepareStatement(query)) + { p.setString(1, username); ResultSet r = p.executeQuery(); @@ -152,9 +135,9 @@ public Result addNewPerson(String name, String username, String studentId) { return Result.fatal(e.getMessage()); } - try (PreparedStatement p = c.prepareStatement( - "INSERT INTO Person (name, username, stuId) VALUES (?, ?, ?)" - )) { + query = "INSERT INTO Person (name, username, stuId) VALUES (?, ?, ?)"; + + try (PreparedStatement p = c.prepareStatement(query)) { p.setString(1, name); p.setString(2, username); p.setString(3, studentId); @@ -182,8 +165,10 @@ public Result getPersonView(String username) { return Result.failure("Username cannot be empty."); } + String query = "SELECT * FROM Person WHERE username = '" + username + "'"; + try (Statement s = c.createStatement()) { - ResultSet r = s.executeQuery("SELECT * FROM Person WHERE username = '" + username + "'"); + ResultSet r = s.executeQuery(query); List data = new ArrayList(); @@ -206,9 +191,10 @@ public Result getPersonView(String username) { @Override public Result> getForums() { - try (Statement s = c.createStatement()) { - ResultSet r = s.executeQuery("SELECT id, title FROM Forum"); + String query = "SELECT id, title FROM Forum"; + try (Statement s = c.createStatement()) { + ResultSet r = s.executeQuery(query); List data = new ArrayList(); while (r.next()) { @@ -258,11 +244,13 @@ public Result getTopic(int topicId) { List posts = new ArrayList(); + String query = "SELECT Post.username, Post.postedAt, Post.text, Posts_In_Topic.topicid FROM Post \n" + + "JOIN Posts_In_Topic ON Posts_In_Topic.postid = Post.id\n" + + "WHERE Posts_In_Topic.topicid = " + topicId; + try (Statement s = c.createStatement()) { - ResultSet r = s.executeQuery("SELECT Post.username, Post.postedAt, Post.text, Posts_In_Topic.topicid FROM Post \n" + - "JOIN Posts_In_Topic ON Posts_In_Topic.postid = Post.id\n" + - "WHERE Posts_In_Topic.topicid = " + topicId); + ResultSet r = s.executeQuery(query); int counter = 1; @@ -277,9 +265,12 @@ public Result getTopic(int topicId) { return Result.fatal("database error - " + ex.getMessage()); } + query = "SELECT id, title FROM Topic WHERE id = " + topicId; + try (Statement s = c.createStatement()) { + // Determines link to create new post - ResultSet r = s.executeQuery("SELECT id, title FROM Topic WHERE id = " + topicId); + ResultSet r = s.executeQuery(query); List data = new ArrayList(); diff --git a/target/uk/ac/bris/cs/databases/cwk2/API.class b/target/uk/ac/bris/cs/databases/cwk2/API.class index 5351d71..373c11b 100644 Binary files a/target/uk/ac/bris/cs/databases/cwk2/API.class and b/target/uk/ac/bris/cs/databases/cwk2/API.class differ