From 71c2c5d005af8232bb08091af3fe01b48624d6ac Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 18 Dec 2023 08:50:46 +0100 Subject: [PATCH 1/8] @author changes --- src/main/java/RestAPISchnittstelle/RestApiClient.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 1805d87..8fa5486 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -1,9 +1,3 @@ -/** - * @author Samuel Wolff - * noch nicht getestet - * TODO FERTIG MACHEN - */ - package RestAPISchnittstelle; import java.net.URI; @@ -12,6 +6,11 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; +/** + * noch nicht getestet + * TODO FERTIG MACHEN + * @author Samuel Wolff + */ public class RestApiClient implements IRestAPI{ private final String urlBase = "https://pbg2h22awo.web.pb.bib.de/VPR_Schnittstelle/VPR_Schnittstelle/restAPI.php"; From db26bca5a3c486c4862ac19f3052e344a20523d7 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 18 Dec 2023 09:12:03 +0100 Subject: [PATCH 2/8] GSON dependency --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 8505bb5..9e4eaf5 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,11 @@ ${junit.version} test + + com.google.code.gson + gson + 2.10.1 + From bf9c99672d24d12fccb2307b7ee34c6866fcf874 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 18 Dec 2023 09:25:08 +0100 Subject: [PATCH 3/8] GSON Test --- src/main/java/RestAPISchnittstelle/RestApiClient.java | 11 +++++++++-- src/main/java/module-info.java | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 8fa5486..7f28ca7 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -5,6 +5,10 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; +import java.nio.file.WatchEvent; + +import Logik.Kind; +import com.google.gson.Gson; /** * noch nicht getestet @@ -25,7 +29,7 @@ public class RestApiClient implements IRestAPI{ public static void main(String[] args){ - new RestApiClient().get("Kind", 2); + new RestApiClient().get("Kind", 1); } @@ -45,9 +49,12 @@ public class RestApiClient implements IRestAPI{ // Send the request and get the response HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + Gson gson = new Gson(); + Kind test = gson.fromJson(httpResponse.body(), Kind.class); + // Print the response status code and body System.out.println("Status Code: " + httpResponse.statusCode()); - System.out.println("Response Body: " + httpResponse.body()); + System.out.println("Response Body: " + test.getName()); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 336a917..a308e0e 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -2,6 +2,7 @@ module de.subway_surfers.vpr_app { requires javafx.controls; requires javafx.fxml; requires java.net.http; + requires com.google.gson; opens de.subway_surfers.vpr_app to javafx.fxml; From 456e55a66e88e8b348bf21ac2537a85234d8d347 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Wed, 20 Dec 2023 09:28:32 +0100 Subject: [PATCH 4/8] GSON works --- src/main/java/Logik/Kind.java | 17 +++++++-- .../RestAPISchnittstelle/RestApiClient.java | 36 +++++++++++++++++-- src/main/java/module-info.java | 3 +- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/main/java/Logik/Kind.java b/src/main/java/Logik/Kind.java index 0d573d1..a84a7c2 100644 --- a/src/main/java/Logik/Kind.java +++ b/src/main/java/Logik/Kind.java @@ -5,9 +5,12 @@ import java.util.ArrayList; public class Kind { // region Felder + + private transient int id; private String name; private String vorname; - private int id; + private int bid; + // TODO Zutat implementieren! // private ArrayList filter; // endregion @@ -37,6 +40,14 @@ public class Kind { this.id = id; } + public int getBid() { + return bid; + } + + public void setBid(int bid) { + this.bid = bid; + } + // TODO Zutat implementieren! /* public ArrayList getFilter() { @@ -50,10 +61,12 @@ public class Kind { // endregion // region Konstruktoren - public Kind(String name, String vorname) { + public Kind(String name, String vorname, int bid) { this.name = name; this.vorname = vorname; + this.bid = bid; } + // endregion } diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 7f28ca7..6827f5a 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -21,15 +21,22 @@ public class RestApiClient implements IRestAPI{ private final HttpClient client; + private final Gson gson; + public RestApiClient(){ client = HttpClient.newHttpClient(); + gson = new Gson(); } public static void main(String[] args){ - new RestApiClient().get("Kind", 1); + Kind kind = new Kind("Klein", "Kevin", 2); + + String json = new Gson().toJson(kind); + + new RestApiClient().post("Kind", json); } @@ -49,7 +56,6 @@ public class RestApiClient implements IRestAPI{ // Send the request and get the response HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - Gson gson = new Gson(); Kind test = gson.fromJson(httpResponse.body(), Kind.class); // Print the response status code and body @@ -77,9 +83,14 @@ public class RestApiClient implements IRestAPI{ // Send the request and get the response HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + Kind[] test = gson.fromJson(httpResponse.body(), Kind[].class); + // Print the response status code and body System.out.println("Status Code: " + httpResponse.statusCode()); - System.out.println("Response Body: " + httpResponse.body()); + for(Kind i : test){ + System.out.println(i.getVorname()); + } + //System.out.println("Response Body: " + test); } catch (Exception e) { e.printStackTrace(); } @@ -143,7 +154,26 @@ public class RestApiClient implements IRestAPI{ */ @Override public void post(String controllerName, String jsonData) { + URI apiUri = URI.create(String.format("%s/%s", urlBase,controllerName)); + System.out.println(apiUri); + + HttpRequest httpRequest = HttpRequest.newBuilder() + .uri(apiUri) + .header("Content-Type", "application/json") + .POST(HttpRequest.BodyPublishers.ofString(jsonData, StandardCharsets.UTF_8)) + .build(); + + try { + // Send the request and get the response + HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + + // Print the response status code and body + System.out.println("Status Code: " + httpResponse.statusCode()); + System.out.println("Response Body: " + httpResponse.body()); + } catch (Exception e) { + e.printStackTrace(); + } } /** diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index a308e0e..dfa6c61 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -5,6 +5,7 @@ module de.subway_surfers.vpr_app { requires com.google.gson; - opens de.subway_surfers.vpr_app to javafx.fxml; + opens de.subway_surfers.vpr_app to javafx.fxml, com.google.gson; + opens Logik to com.google.gson; exports de.subway_surfers.vpr_app; } \ No newline at end of file From 98826d1b9feb0fd4065226c81855787cf17156ea Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 8 Jan 2024 08:34:13 +0100 Subject: [PATCH 5/8] Erster Commit 2024. *smileyface* keinen Plan was der macht tho --- src/main/java/RestAPISchnittstelle/RestApiClient.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 6827f5a..a703f30 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -36,7 +36,9 @@ public class RestApiClient implements IRestAPI{ String json = new Gson().toJson(kind); - new RestApiClient().post("Kind", json); + System.out.println(json); + + //new RestApiClient().post("Kind", json); } From c967746bba2858e4f28684fe2505a785c1bb9279 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 8 Jan 2024 09:14:17 +0100 Subject: [PATCH 6/8] Misc changes + Kommentare --- .idea/misc.xml | 2 +- src/main/java/Logik/Kind.java | 3 - .../RestAPISchnittstelle/RestApiClient.java | 70 ++++++++++++------- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index a855768..fdc35ea 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/src/main/java/Logik/Kind.java b/src/main/java/Logik/Kind.java index 191ca90..1174262 100644 --- a/src/main/java/Logik/Kind.java +++ b/src/main/java/Logik/Kind.java @@ -11,7 +11,6 @@ public class Kind { private String vorname; private int bid; - // TODO Zutat implementieren! private ArrayList filter; // endregion @@ -48,8 +47,6 @@ public class Kind { this.bid = bid; } - // TODO Zutat implementieren! - public ArrayList getFilter() { return filter; } diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index a703f30..2135cd8 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -5,7 +5,6 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; -import java.nio.file.WatchEvent; import Logik.Kind; import com.google.gson.Gson; @@ -28,22 +27,10 @@ public class RestApiClient implements IRestAPI{ gson = new Gson(); } - - - public static void main(String[] args){ - - Kind kind = new Kind("Klein", "Kevin", 2); - - String json = new Gson().toJson(kind); - - System.out.println(json); - - //new RestApiClient().post("Kind", json); - - } - /** - * @param controllerName + * Methode für einen Get-Aufruf. Ruft alle Elemente einer Tabelle auf. + * + * @param controllerName Name des aufzurufenden Controllers */ @Override public void get(String controllerName) { @@ -69,8 +56,10 @@ public class RestApiClient implements IRestAPI{ } /** - * @param controllerName - * @param id + * Methode für einen Get-Aufruf. Ruft ein spezifisches Element auf. + * + * @param controllerName Name des aufzurufenden Controllers + * @param id Id der Aufzurufenden Zeile */ @Override public void get(String controllerName, int id) { @@ -99,9 +88,11 @@ public class RestApiClient implements IRestAPI{ } /** - * @param controllerName - * @param id - * @param bezahlt + * Methode für einen Get-Aufruf. Ruft ein spezielles Element auf. + * + * @param controllerName Name des aufzurufenden Controllers + * @param id Id der Aufzurufenden Zeile + * @param bezahlt TODO Warum ist das hier? */ @Override public void get(String controllerName, int id, boolean bezahlt) { @@ -125,8 +116,11 @@ public class RestApiClient implements IRestAPI{ } /** - * @param controllerName - * @param id + * Methode für einen Put-Aufruf. Aktualisiert einen Eintrag. + * + * @param controllerName Name des aufzurufenden Controllers. + * @param id Id des zu änderenden Eintrags. + * @param jsonData JsonString mit den neuen Daten. */ @Override public void put(String controllerName, int id, String jsonData) { @@ -152,7 +146,10 @@ public class RestApiClient implements IRestAPI{ } /** - * @param controllerName + * Methode für einen Post-Aufruf. Fügt einen Eintrag in eine Datenbank hinzu. + * + * @param controllerName Name des aufzurufenden Controllers. + * @param jsonData JsonString mit den Daten des Eintrags. */ @Override public void post(String controllerName, String jsonData) { @@ -179,12 +176,33 @@ public class RestApiClient implements IRestAPI{ } /** - * @param controllerName - * @param id + * Methode für einen Delete-Aufruf. Löscht einen Eintrag mit einer Id. + * + * @param controllerName Name des aufzurufenden Controllers + * @param id Id des zu löschenden Eintrags. */ @Override public void delete(String controllerName, int id) { + URI apiUri = URI.create(String.format("%s/%s/%d", urlBase,controllerName, id)); + System.out.println(apiUri); + + HttpRequest httpRequest = HttpRequest.newBuilder() + .uri(apiUri) + .header("Content-Type", "application/json") + .DELETE() + .build(); + + try { + // Send the request and get the response + HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); + + // Print the response status code and body + System.out.println("Status Code: " + httpResponse.statusCode()); + System.out.println("Response Body: " + httpResponse.body()); + } catch (Exception e) { + e.printStackTrace(); + } } } From 8be72640a72a84993399304b07dab57006d194dc Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 8 Jan 2024 11:13:24 +0100 Subject: [PATCH 7/8] diverses --- src/main/java/Logik/Account.java | 19 +++++++++++++------ .../{Benutzer.java => ElternAccount.java} | 7 +++---- src/main/java/Logik/MitarbeiterAccount.java | 2 +- .../RestAPISchnittstelle/RestApiClient.java | 13 +++++++++++++ 4 files changed, 30 insertions(+), 11 deletions(-) rename src/main/java/Logik/{Benutzer.java => ElternAccount.java} (84%) diff --git a/src/main/java/Logik/Account.java b/src/main/java/Logik/Account.java index aab04cb..e6aee52 100644 --- a/src/main/java/Logik/Account.java +++ b/src/main/java/Logik/Account.java @@ -8,9 +8,11 @@ public class Account { // region Felder private String passwort; - private int id; + private transient int id; - private String benutzername; + private String name; + + private int rid; // endregion // region Getter & Setter @@ -32,18 +34,23 @@ public class Account { public String getBenutzername() { - return benutzername; + return name; } public void setBenutzername(String benutzername) { - this.benutzername = benutzername; + this.name = benutzername; } // endregion // region Konstruktoren - public Account(String passwort, String benutzername) { + public Account(String passwort, String name) { this.passwort = passwort; - this.benutzername = benutzername; + this.name = name; + + if(this instanceof MitarbeiterAccount) + rid = 0; + else if (this instanceof ElternAccount) + rid = 1; } // endregion diff --git a/src/main/java/Logik/Benutzer.java b/src/main/java/Logik/ElternAccount.java similarity index 84% rename from src/main/java/Logik/Benutzer.java rename to src/main/java/Logik/ElternAccount.java index d2a41b8..59f5335 100644 --- a/src/main/java/Logik/Benutzer.java +++ b/src/main/java/Logik/ElternAccount.java @@ -7,15 +7,14 @@ package Logik; -import java.nio.file.WatchEvent; import java.util.ArrayList; -public class Benutzer extends Account { +public class ElternAccount extends Account { // region Felder private String email; - private ArrayList kinder; + private transient ArrayList kinder; // endregion // region Getter & Setter @@ -37,7 +36,7 @@ public class Benutzer extends Account { // endregion // region Konstruktoren - public Benutzer(String passwort, String benutzername, String email) { + public ElternAccount(String passwort, String benutzername, String email) { super(passwort, benutzername); this.email = email; kinder = new ArrayList<>(); diff --git a/src/main/java/Logik/MitarbeiterAccount.java b/src/main/java/Logik/MitarbeiterAccount.java index b1a3c33..05fbe8f 100644 --- a/src/main/java/Logik/MitarbeiterAccount.java +++ b/src/main/java/Logik/MitarbeiterAccount.java @@ -51,7 +51,7 @@ public class MitarbeiterAccount extends Account { * @param kinder Eine Liste mit allen zugehörigen Kindern des Accounts */ public void accountErstellen(String passwort, String benutzername, String email, ArrayList kinder) { - Benutzer newAccount = new Benutzer(passwort, benutzername, email); + ElternAccount newAccount = new ElternAccount(passwort, benutzername, email); newAccount.setKinder(kinder); // Id muss aus der Datenbank geholt werden und dann gesetzt werden } diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 2135cd8..df31d26 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -6,6 +6,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; +import Logik.ElternAccount; import Logik.Kind; import com.google.gson.Gson; @@ -27,6 +28,18 @@ public class RestApiClient implements IRestAPI{ gson = new Gson(); } + public static void main(String[] args){ + + RestApiClient client1 = new RestApiClient(); + + ElternAccount elternAccount = new ElternAccount("hiiiiiiii", "124", "pimmel@pimmel.com"); + + String json = client1.gson.toJson(elternAccount); + + client1.put("Benutzer",6, json); + + } + /** * Methode für einen Get-Aufruf. Ruft alle Elemente einer Tabelle auf. * From 93ecea334f12eaac55790e78daa389f3a1a82f41 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Thu, 11 Jan 2024 12:09:00 +0100 Subject: [PATCH 8/8] diverses --- .idea/misc.xml | 1 + .../java/RestAPISchnittstelle/RestApiClient.java | 14 +++----------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fdc35ea..de4b033 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,6 +7,7 @@ + diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index df31d26..8939301 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -32,11 +32,7 @@ public class RestApiClient implements IRestAPI{ RestApiClient client1 = new RestApiClient(); - ElternAccount elternAccount = new ElternAccount("hiiiiiiii", "124", "pimmel@pimmel.com"); - - String json = client1.gson.toJson(elternAccount); - - client1.put("Benutzer",6, json); + client1.post("Gericht", "{\"name\" : \"Svens Beine\", \"69.69\", \"beschreibung\" : \"Muss net schmegge, muss wirge\"}"); } @@ -87,13 +83,9 @@ public class RestApiClient implements IRestAPI{ // Send the request and get the response HttpResponse httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - Kind[] test = gson.fromJson(httpResponse.body(), Kind[].class); - // Print the response status code and body - System.out.println("Status Code: " + httpResponse.statusCode()); - for(Kind i : test){ - System.out.println(i.getVorname()); - } + System.out.println("Status Code: " + httpResponse.statusCode() + httpResponse.body()); + //System.out.println("Response Body: " + test); } catch (Exception e) { e.printStackTrace();