From b954c518ec8a7571a8746b0f49997f2473fd14d5 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Tue, 11 Apr 2023 17:25:39 +0200 Subject: [PATCH] Fix register groups --- src/be/jeffcheasey88/peeratcode/Main.java | 8 +++++++ .../peeratcode/framework/HttpUtil.java | 7 ++----- .../repository/DatabaseRepository.java | 19 ++++++++++------- .../peeratcode/routes/Register.java | 7 +++---- .../peeratcode/routes/TmpRoutesTests.java | 21 +++++++++---------- .../peeratcode/webclient/WebClient.java | 4 +++- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index af75795..9d3d298 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -70,6 +70,14 @@ public class Main{ initRoutes(router, config); startWebServer(config, router); + + new TimerTask(){ + int i = 4; + @Override + public void run() { + System.out.println("oui "+i); + } + }; } private static void initRoutes(Router router, Configuration config) { diff --git a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java b/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java index e65413e..2960886 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java +++ b/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java @@ -1,11 +1,8 @@ package be.jeffcheasey88.peeratcode.framework; -import java.io.IOException; import java.security.MessageDigest; import java.util.ArrayList; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import org.json.simple.parser.JSONParser; @@ -14,8 +11,8 @@ public class HttpUtil{ private HttpUtil(){} public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception{ - writer.write("HTTP/1.1 " + code + " " + codeMessage(code) + "\n"); - for(String header : headers) writer.write(header + "\n"); + writer.write("HTTP/1.1 "+code+codeMessage(code)+"\n"); + for(String header : headers) writer.write(header+"\n"); writer.write("\n"); writer.flush(); } diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index a4a6c79..d9db14d 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -413,20 +413,23 @@ public class DatabaseRepository { ResultSet inserted = playerStatement.getGeneratedKeys(); if (inserted.next()) { int newPlayerId = inserted.getInt(1); - try (PreparedStatement containsGroupsStatement = con - .prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) { - containsGroupsStatement.setInt(1, newPlayerId); - containsGroupsStatement.setString(2, sgroup); - containsGroupsStatement.executeUpdate(); - con.commit(); - con.setAutoCommit(true); - return newPlayerId; + if(!sgroup.isEmpty()){ + try (PreparedStatement containsGroupsStatement = con + .prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) { + containsGroupsStatement.setInt(1, newPlayerId); + containsGroupsStatement.setString(2, sgroup); + containsGroupsStatement.executeUpdate(); + } } + con.commit(); + con.setAutoCommit(true); + return newPlayerId; } } } catch (SQLException e) { con.rollback(); con.setAutoCommit(true); + e.printStackTrace(); } } catch (SQLException e) { e.printStackTrace(); diff --git a/src/be/jeffcheasey88/peeratcode/routes/Register.java b/src/be/jeffcheasey88/peeratcode/routes/Register.java index d80c0a0..09924fe 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/Register.java +++ b/src/be/jeffcheasey88/peeratcode/routes/Register.java @@ -31,8 +31,8 @@ public class Register implements Response { } @Route(path = "^\\/register$", type = POST) - public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { - if (user != null) { + public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ + if (user != null){ HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); return; } @@ -75,9 +75,8 @@ public class Register implements Response { writer.write(error.toJSONString()); return; } - } else { - HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); } + HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *"); } private void createFolderToSaveSourceCode(String pseudo) throws IOException { diff --git a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java index 97beaa4..f94d3f3 100644 --- a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java +++ b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java @@ -43,20 +43,19 @@ public class TmpRoutesTests { void testOnDeployed(){ try { JSONObject content = new JSONObject(); - content.put("name", "testTeam1"); - content.put("chapter", 2); + content.put("avatar", ""); + content.put("description", ""); + content.put("email", "ouistiti@gmail.com"); + content.put("firstname", "Stiti"); + content.put("lastname", "Oui"); + content.put("passwd", "TheoPueDesPieds"); + content.put("pseudo", "Ouistiti"); + content.put("sgroup", ""); - client.auth("JeffCheasey88", "TheoPueDesPieds"); - - client.route("/groupCreate", "POST", content.toJSONString()); - client.assertResponseCode(200); - - client.route("/groupJoin","POST",content.toJSONString()); - client.assertResponseCode(200); - - client.route("/groupQuit", "POST", content.toJSONString()); + client.route("/register", "POST", content.toJSONString()); client.assertResponseCode(200); }catch(Exception e){ + e.printStackTrace(); fail(e); } } diff --git a/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java b/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java index 0455b41..32d00ee 100644 --- a/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java +++ b/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java @@ -72,7 +72,9 @@ public class WebClient { for(String value : content) this.writer.write(value+"\n"); this.writer.flush(); - this.responseCode = Integer.parseInt(this.reader.readLine().split("\\s+")[1]); + String t = this.reader.readLine(); + System.out.println(t); + this.responseCode = Integer.parseInt(t.split("\\s+")[1]); String line; while(((line = reader.readLine()) != null) && line.length() > 0) this.headers.add(line);