Push mailConfirmation route

This commit is contained in:
s_lanfeust 2023-09-04 14:56:48 +02:00
parent c80d9ce31d
commit ecdf99365b

View file

@ -0,0 +1,88 @@
package dev.peerat.backend.routes;
import static dev.peerat.framework.RequestType.POST;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import dev.peerat.backend.bonus.extract.RouteDoc;
import dev.peerat.backend.model.PeerAtUser;
import dev.peerat.backend.model.Player;
import dev.peerat.backend.repository.DatabaseRepository;
import dev.peerat.framework.Context;
import dev.peerat.framework.HttpReader;
import dev.peerat.framework.HttpWriter;
import dev.peerat.framework.Response;
import dev.peerat.framework.Route;
import dev.peerat.framework.Router;
public class MailConfirmation implements Response {
private DatabaseRepository databaseRepo;
private Router<PeerAtUser> router;
private String usersFilesPath;
private Map<Integer, Player> playersWaiting;
public MailConfirmation(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
Map<Integer, Player> playersWaiting) {
this.databaseRepo = databaseRepo;
this.router = router;
usersFilesPath = initUsersFilesPath;
}
@RouteDoc(path = "/confirmation", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
@Route(path = "^\\/confirmation$", type = POST)
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
if (context.getUser() != null) {
context.response(403);
return;
}
JSONObject informations = reader.readJson();
String email = (String) informations.get("email");
String password = (String) informations.get("passwd");
int code = (int) informations.get("code");
if (checkIfValided(code, email)) {
String pseudo = playersWaiting.get(code).getPseudo();
String firstname = playersWaiting.get(code).getLastname();
String lastname = playersWaiting.get(code).getLastname();
String description = playersWaiting.get(code).getDescription();
String group = null;
String avatar = null;
int id;
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
avatar)) >= 0) {
context.response(200, "Access-Control-Expose-Headers: Authorization",
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
createFolderToSaveSourceCode(pseudo);
return;
} else {
context.response(400);
JSONObject error = new JSONObject();
error.put("username_valid", pseudo);
error.put("email_valid", email);
writer.write(error.toJSONString());
return;
}
}
}
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
}
private boolean checkIfValided(int code, String email) {
return playersWaiting.get(code).getEmail().equals(email);
}
}