11package io .nodelink .server .app .cluster .api .routes .v1 .handler ;
22
3- import io . javalin . http . Context ;
3+ import com . fasterxml . jackson . databind . JsonNode ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
55import com .fasterxml .jackson .databind .node .ObjectNode ;
6+ import io .javalin .http .Context ;
67import io .nodelink .server .app .data .BONE_LOCATION ;
78import io .nodelink .server .app .data .CLUSTER_LOCATION ;
89import io .nodelink .server .app .infra .ApiHandler ;
910import io .nodelink .server .app .infra .DatabaseService ;
1011
11- import java .util .Random ;
12-
1312public class AddClusterH implements ApiHandler {
1413 private final ObjectMapper mapper = new ObjectMapper ();
15- private final Random random = new Random ();
1614
1715 @ Override
1816 public void handle (Context ctx ) throws Exception {
19- ObjectNode requestBody = (ObjectNode ) mapper .readTree (ctx .body ());
20-
21- // 1. Validation de la localisation du Cluster (ex: "NBG_DE_EU_CLUSTER")
22- String locParam = requestBody .get ("location" ).asText ();
23- CLUSTER_LOCATION loc ;
2417 try {
25- loc = CLUSTER_LOCATION .valueOf (locParam );
26- } catch (IllegalArgumentException e ) {
27- ctx .status (400 ).result ("Erreur : CLUSTER_LOCATION invalide." );
28- return ;
29- }
18+ // 1. Lecture du JSON entrant
19+ JsonNode requestBody = mapper .readTree (ctx .body ());
3020
31- // 2. Validation du Bone Parent (ex: "EUROPE_WEST")
32- String boneParam = requestBody .get ("parentBone" ).asText ();
33- BONE_LOCATION boneLoc ;
34- try {
35- boneLoc = BONE_LOCATION .valueOf (boneParam );
36- } catch (IllegalArgumentException e ) {
37- ctx .status (400 ).result ("Erreur : BONE_LOCATION parent invalide." );
38- return ;
39- }
21+ // 2. Vérification des champs obligatoires
22+ if (requestBody == null || !requestBody .has ("id" ) || !requestBody .has ("location" ) || !requestBody .has ("parentBone" )) {
23+ ctx .status (400 ).result ("Erreur : Les champs 'id', 'location' et 'parentBone' sont requis." );
24+ return ;
25+ }
26+
27+ // 3. Extraction et formatage
28+ String rawId = requestBody .get ("id" ).asText ();
29+ String clusterId = "C" + rawId ;
30+
31+ // Validation des enums (lance une IllegalArgumentException si invalide)
32+ CLUSTER_LOCATION loc = CLUSTER_LOCATION .valueOf (requestBody .get ("location" ).asText ().toUpperCase ());
33+ BONE_LOCATION boneLoc = BONE_LOCATION .valueOf (requestBody .get ("parentBone" ).asText ().toUpperCase ());
4034
41- // 3. Génération ID "C" + random
42- String clusterId = "C" + random . nextInt ( 10000 );
35+ // 4. Préparation de l'objet final à stocker
36+ String finalUrl = String . format ( "http://%s.%s.nodelinkapp.xyz" , rawId , loc . getLocationCluster () );
4337
44- // 4. Construction de l'objet final
45- ObjectNode clusterData = mapper . createObjectNode ( );
46- clusterData .put ("id " , clusterId );
47- clusterData .put ("type " , "Cluster" );
48- clusterData .put ("name " , loc . getNameCluster () );
49- clusterData .put ("url " , loc . getLocationCluster ());
38+ ObjectNode clusterData = mapper . createObjectNode ();
39+ clusterData . put ( "id" , clusterId );
40+ clusterData .put ("type " , "Cluster" );
41+ clusterData .put ("name " , loc . getNameCluster () );
42+ clusterData .put ("url " , finalUrl );
43+ clusterData .put ("parentBone " , boneLoc . getLocation ());
5044
51- // On récupère ici la propriété "locationBone" (ex: west.eu.bone)
52- clusterData .put ("parentBone" , boneLoc .getLocation ());
45+ clusterData .putArray ("coords" )
46+ .add (loc .getLatitude ())
47+ .add (loc .getLongitude ());
5348
54- // Coordonnées auto depuis l'Enum Cluster
55- clusterData .putArray ("coords" )
56- .add (loc .getLatitude ())
57- .add (loc .getLongitude ());
49+ String link = requestBody .has ("link" ) ? requestBody .get ("link" ).asText () : "NONE" ;
50+ clusterData .put ("link" , link );
5851
59- // Le lien technique (souvent l'ID du Bone ou d'un autre Cluster)
60- clusterData .put ("link" , requestBody .get ("link" ).asText ());
52+ // 5. Appel du SAVE LOCAL (Simple SQLite)
53+ // On utilise saveCluster qui est déjà défini dans ton DatabaseService
54+ DatabaseService .saveCluster (clusterId , clusterData .toString ());
6155
62- // 5. Sauvegarde
63- DatabaseService .saveCluster (clusterId , clusterData .toString ());
56+ System .out .println ("[INFO] Cluster " + clusterId + " sauvegardé localement." );
6457
65- ctx .status (201 ).json (clusterData );
58+ // 6. Réponse au client
59+ ctx .status (201 ).json (clusterData );
60+
61+ } catch (IllegalArgumentException e ) {
62+ ctx .status (400 ).result ("Erreur : Localisation invalide." );
63+ } catch (Exception e ) {
64+ System .err .println ("--- CRASH SERVEUR ---" );
65+ System .err .println ("Cause : " + e .getMessage ());
66+ e .printStackTrace (); // <--- C'est cette ligne qui va te dire la ligne exacte du bug
67+ ctx .status (500 ).result ("Erreur : " + e .getMessage ());
68+ }
6669 }
6770}
0 commit comments