From 1b2a0c0309587213802baa79e8299ec02da34d18 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Mon, 15 Jan 2024 09:31:57 +0100 Subject: [PATCH 1/2] nextId Methode --- .../RestAPISchnittstelle/RestApiClient.java | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 8939301..b4b21c4 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -8,7 +8,7 @@ import java.nio.charset.StandardCharsets; import Logik.ElternAccount; import Logik.Kind; -import com.google.gson.Gson; +import com.google.gson.*; /** * noch nicht getestet @@ -32,8 +32,8 @@ public class RestApiClient implements IRestAPI{ RestApiClient client1 = new RestApiClient(); - client1.post("Gericht", "{\"name\" : \"Svens Beine\", \"69.69\", \"beschreibung\" : \"Muss net schmegge, muss wirge\"}"); + System.out.println(client1.nextId("Benutzer")); } /** @@ -54,11 +54,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()); - System.out.println("Response Body: " + test.getName()); + System.out.println("Response Body: " + httpResponse.body()); } catch (Exception e) { e.printStackTrace(); } @@ -210,4 +208,34 @@ public class RestApiClient implements IRestAPI{ } } + + public int nextId(String controllerName){ + URI apiUri = URI.create(String.format("%s/%s/nextId", urlBase, controllerName)); + + HttpRequest httpRequest = HttpRequest.newBuilder() + .uri(apiUri) + .GET() + .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()); + + JsonElement jsonElement = JsonParser.parseString(httpResponse.body()); + JsonArray jsonArray = jsonElement.getAsJsonArray(); + + JsonObject json = jsonArray.get(0).getAsJsonObject(); + + return json.get("auto_increment").getAsInt(); + + } catch (Exception e) { + e.printStackTrace(); + return -1; + } + } + } From 72a6d9a7e5af7e05eedd57298997041b79ebc176 Mon Sep 17 00:00:00 2001 From: Samuel Wolff Date: Wed, 17 Jan 2024 10:30:11 +0100 Subject: [PATCH 2/2] get ist jetzt String hehe --- src/main/java/RestAPISchnittstelle/IRestAPI.java | 6 +++--- .../java/RestAPISchnittstelle/RestApiClient.java | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/RestAPISchnittstelle/IRestAPI.java b/src/main/java/RestAPISchnittstelle/IRestAPI.java index fc6ffaf..565dda5 100644 --- a/src/main/java/RestAPISchnittstelle/IRestAPI.java +++ b/src/main/java/RestAPISchnittstelle/IRestAPI.java @@ -11,11 +11,11 @@ public interface IRestAPI { // region Get - void get(String controllerName); + String get(String controllerName); - void get (String controllerName, int id); + String get (String controllerName, int id); - void get (String controllerName, int id, boolean bezahlt); + String get (String controllerName, int id, boolean bezahlt); //endregion diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index b4b21c4..7e7d8a8 100644 --- a/src/main/java/RestAPISchnittstelle/RestApiClient.java +++ b/src/main/java/RestAPISchnittstelle/RestApiClient.java @@ -42,7 +42,7 @@ public class RestApiClient implements IRestAPI{ * @param controllerName Name des aufzurufenden Controllers */ @Override - public void get(String controllerName) { + public String get(String controllerName) { URI apiUri = URI.create(String.format("%s/%s", urlBase, controllerName)); HttpRequest httpRequest = HttpRequest.newBuilder() @@ -57,8 +57,10 @@ public class RestApiClient implements IRestAPI{ // Print the response status code and body System.out.println("Status Code: " + httpResponse.statusCode()); System.out.println("Response Body: " + httpResponse.body()); + return httpResponse.body(); } catch (Exception e) { e.printStackTrace(); + return null; } } @@ -69,7 +71,7 @@ public class RestApiClient implements IRestAPI{ * @param id Id der Aufzurufenden Zeile */ @Override - public void get(String controllerName, int id) { + public String get(String controllerName, int id) { URI apiUri = URI.create(String.format("%s/%s/%s", urlBase, controllerName, id)); HttpRequest httpRequest = HttpRequest.newBuilder() @@ -84,9 +86,12 @@ public class RestApiClient implements IRestAPI{ // Print the response status code and body System.out.println("Status Code: " + httpResponse.statusCode() + httpResponse.body()); + return httpResponse.body(); + //System.out.println("Response Body: " + test); } catch (Exception e) { e.printStackTrace(); + return null; } } @@ -98,7 +103,7 @@ public class RestApiClient implements IRestAPI{ * @param bezahlt TODO Warum ist das hier? */ @Override - public void get(String controllerName, int id, boolean bezahlt) { + public String get(String controllerName, int id, boolean bezahlt) { URI apiUri = URI.create(String.format("%s/%s?%s&%s", urlBase, controllerName, id, bezahlt)); HttpRequest httpRequest = HttpRequest.newBuilder() @@ -113,8 +118,10 @@ public class RestApiClient implements IRestAPI{ // Print the response status code and body System.out.println("Status Code: " + httpResponse.statusCode()); System.out.println("Response Body: " + httpResponse.body()); + return httpResponse.body(); } catch (Exception e) { e.printStackTrace(); + return null; } }