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);
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) {

View file

@ -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();
}

View file

@ -413,20 +413,23 @@ public class DatabaseRepository {
ResultSet inserted = playerStatement.getGeneratedKeys();
if (inserted.next()) {
int newPlayerId = inserted.getInt(1);
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();

View file

@ -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 {

View file

@ -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);
}
}

View file

@ -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);