Fix register groups

This commit is contained in:
jeffcheasey88 2023-04-11 17:25:39 +02:00
parent 9ba9ab12c7
commit b954c518ec
6 changed files with 37 additions and 29 deletions

View file

@ -70,6 +70,14 @@ public class Main{
initRoutes(router, config); initRoutes(router, config);
startWebServer(config, router); 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) { private static void initRoutes(Router router, Configuration config) {

View file

@ -1,11 +1,8 @@
package be.jeffcheasey88.peeratcode.framework; package be.jeffcheasey88.peeratcode.framework;
import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
@ -14,8 +11,8 @@ public class HttpUtil{
private HttpUtil(){} private HttpUtil(){}
public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception{ public static void responseHeaders(HttpWriter writer, int code, String... headers) throws Exception{
writer.write("HTTP/1.1 " + code + " " + codeMessage(code) + "\n"); writer.write("HTTP/1.1 "+code+codeMessage(code)+"\n");
for(String header : headers) writer.write(header + "\n"); for(String header : headers) writer.write(header+"\n");
writer.write("\n"); writer.write("\n");
writer.flush(); writer.flush();
} }

View file

@ -413,20 +413,23 @@ public class DatabaseRepository {
ResultSet inserted = playerStatement.getGeneratedKeys(); ResultSet inserted = playerStatement.getGeneratedKeys();
if (inserted.next()) { if (inserted.next()) {
int newPlayerId = inserted.getInt(1); int newPlayerId = inserted.getInt(1);
if(!sgroup.isEmpty()){
try (PreparedStatement containsGroupsStatement = con try (PreparedStatement containsGroupsStatement = con
.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) { .prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
containsGroupsStatement.setInt(1, newPlayerId); containsGroupsStatement.setInt(1, newPlayerId);
containsGroupsStatement.setString(2, sgroup); containsGroupsStatement.setString(2, sgroup);
containsGroupsStatement.executeUpdate(); containsGroupsStatement.executeUpdate();
}
}
con.commit(); con.commit();
con.setAutoCommit(true); con.setAutoCommit(true);
return newPlayerId; return newPlayerId;
} }
} }
}
} catch (SQLException e) { } catch (SQLException e) {
con.rollback(); con.rollback();
con.setAutoCommit(true); con.setAutoCommit(true);
e.printStackTrace();
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -31,8 +31,8 @@ public class Register implements Response {
} }
@Route(path = "^\\/register$", type = POST) @Route(path = "^\\/register$", type = POST)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
if (user != null) { if (user != null){
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
return; return;
} }
@ -75,9 +75,8 @@ public class Register implements Response {
writer.write(error.toJSONString()); writer.write(error.toJSONString());
return; 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 { private void createFolderToSaveSourceCode(String pseudo) throws IOException {

View file

@ -43,20 +43,19 @@ public class TmpRoutesTests {
void testOnDeployed(){ void testOnDeployed(){
try { try {
JSONObject content = new JSONObject(); JSONObject content = new JSONObject();
content.put("name", "testTeam1"); content.put("avatar", "");
content.put("chapter", 2); 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("/register", "POST", content.toJSONString());
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.assertResponseCode(200); client.assertResponseCode(200);
}catch(Exception e){ }catch(Exception e){
e.printStackTrace();
fail(e); fail(e);
} }
} }

View file

@ -72,7 +72,9 @@ public class WebClient {
for(String value : content) this.writer.write(value+"\n"); for(String value : content) this.writer.write(value+"\n");
this.writer.flush(); 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; String line;
while(((line = reader.readLine()) != null) && line.length() > 0) this.headers.add(line); while(((line = reader.readLine()) != null) && line.length() > 0) this.headers.add(line);