diff --git a/src/main/java/RestAPISchnittstelle/RestApiClient.java b/src/main/java/RestAPISchnittstelle/RestApiClient.java index 1fdb3e2..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")); } /** @@ -42,7 +42,7 @@ public class RestApiClient implements IRestAPI{ * @param controllerName Name des aufzurufenden Controllers */ @Override - public String get(String controllerName) { + public void get(String controllerName) { URI apiUri = URI.create(String.format("%s/%s", urlBase, controllerName)); HttpRequest httpRequest = HttpRequest.newBuilder() @@ -54,17 +54,12 @@ 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()); - - return httpResponse.body(); + System.out.println("Response Body: " + httpResponse.body()); } catch (Exception e) { e.printStackTrace(); } - return ""; } /** @@ -74,7 +69,7 @@ public class RestApiClient implements IRestAPI{ * @param id Id der Aufzurufenden Zeile */ @Override - public String get(String controllerName, int id) { + public void get(String controllerName, int id) { URI apiUri = URI.create(String.format("%s/%s/%s", urlBase, controllerName, id)); HttpRequest httpRequest = HttpRequest.newBuilder() @@ -89,12 +84,10 @@ 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 ""; } /** @@ -105,7 +98,7 @@ public class RestApiClient implements IRestAPI{ * @param bezahlt TODO Warum ist das hier? */ @Override - public String get(String controllerName, int id, boolean bezahlt) { + public void 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() @@ -120,12 +113,9 @@ 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 ""; } /** @@ -218,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; + } + } + }