KinderErstellen

This commit is contained in:
Sven Alteköster 2024-01-13 14:29:57 +01:00
parent 8e1084f034
commit 4780f4403b
7 changed files with 69 additions and 28 deletions

View File

@ -7,9 +7,8 @@
<option value="$PROJECT_DIR$/pom.xml" /> <option value="$PROJECT_DIR$/pom.xml" />
</list> </list>
</option> </option>
<option name="workspaceImportForciblyTurnedOn" value="true" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -12,6 +12,9 @@ public class Account {
private String name; private String name;
private String email;
private int rid; private int rid;
// endregion // endregion
@ -32,6 +35,14 @@ public class Account {
this.id = id; this.id = id;
} }
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBenutzername() { public String getBenutzername() {
return name; return name;
@ -43,7 +54,7 @@ public class Account {
// endregion // endregion
// region Konstruktoren // region Konstruktoren
public Account(String passwort, String name) { public Account(String passwort, String name, String email) {
this.passwort = passwort; this.passwort = passwort;
this.name = name; this.name = name;

View File

@ -12,19 +12,10 @@ import java.util.ArrayList;
public class ElternAccount extends Account { public class ElternAccount extends Account {
// region Felder // region Felder
private String email;
private transient ArrayList<Kind> kinder; private transient ArrayList<Kind> kinder;
// endregion // endregion
// region Getter & Setter // region Getter & Setter
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public ArrayList<Kind> getKinder() { public ArrayList<Kind> getKinder() {
return kinder; return kinder;
@ -37,8 +28,7 @@ public class ElternAccount extends Account {
// region Konstruktoren // region Konstruktoren
public ElternAccount(String passwort, String benutzername, String email) { public ElternAccount(String passwort, String benutzername, String email) {
super(passwort, benutzername); super(passwort, benutzername, email);
this.email = email;
kinder = new ArrayList<>(); kinder = new ArrayList<>();
} }
// endregion // endregion

View File

@ -12,8 +12,8 @@ import java.util.ArrayList;
public class MitarbeiterAccount extends Account { public class MitarbeiterAccount extends Account {
// region Konstrukoren // region Konstrukoren
public MitarbeiterAccount(String passwort, String benutzername) { public MitarbeiterAccount(String passwort, String benutzername, String email) {
super(passwort, benutzername); super(passwort, benutzername, email);
} }
//endregion //endregion

View File

@ -11,11 +11,11 @@ public interface IRestAPI {
// region Get // 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 //endregion

View File

@ -42,7 +42,7 @@ public class RestApiClient implements IRestAPI{
* @param controllerName Name des aufzurufenden Controllers * @param controllerName Name des aufzurufenden Controllers
*/ */
@Override @Override
public void get(String controllerName) { public String get(String controllerName) {
URI apiUri = URI.create(String.format("%s/%s", urlBase, controllerName)); URI apiUri = URI.create(String.format("%s/%s", urlBase, controllerName));
HttpRequest httpRequest = HttpRequest.newBuilder() HttpRequest httpRequest = HttpRequest.newBuilder()
@ -59,9 +59,12 @@ public class RestApiClient implements IRestAPI{
// Print the response status code and body // Print the response status code and body
System.out.println("Status Code: " + httpResponse.statusCode()); System.out.println("Status Code: " + httpResponse.statusCode());
System.out.println("Response Body: " + test.getName()); System.out.println("Response Body: " + test.getName());
return httpResponse.body();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return "";
} }
/** /**
@ -71,7 +74,7 @@ public class RestApiClient implements IRestAPI{
* @param id Id der Aufzurufenden Zeile * @param id Id der Aufzurufenden Zeile
*/ */
@Override @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)); URI apiUri = URI.create(String.format("%s/%s/%s", urlBase, controllerName, id));
HttpRequest httpRequest = HttpRequest.newBuilder() HttpRequest httpRequest = HttpRequest.newBuilder()
@ -86,10 +89,12 @@ public class RestApiClient implements IRestAPI{
// Print the response status code and body // Print the response status code and body
System.out.println("Status Code: " + httpResponse.statusCode() + httpResponse.body()); System.out.println("Status Code: " + httpResponse.statusCode() + httpResponse.body());
return httpResponse.body();
//System.out.println("Response Body: " + test); //System.out.println("Response Body: " + test);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return "";
} }
/** /**
@ -100,7 +105,7 @@ public class RestApiClient implements IRestAPI{
* @param bezahlt TODO Warum ist das hier? * @param bezahlt TODO Warum ist das hier?
*/ */
@Override @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)); URI apiUri = URI.create(String.format("%s/%s?%s&%s", urlBase, controllerName, id, bezahlt));
HttpRequest httpRequest = HttpRequest.newBuilder() HttpRequest httpRequest = HttpRequest.newBuilder()
@ -115,9 +120,12 @@ public class RestApiClient implements IRestAPI{
// Print the response status code and body // Print the response status code and body
System.out.println("Status Code: " + httpResponse.statusCode()); System.out.println("Status Code: " + httpResponse.statusCode());
System.out.println("Response Body: " + httpResponse.body()); System.out.println("Response Body: " + httpResponse.body());
return httpResponse.body();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return "";
} }
/** /**

View File

@ -1,5 +1,9 @@
package de.subway_surfers.vpr_app; package de.subway_surfers.vpr_app;
import Logik.Account;
import Logik.ElternAccount;
import Logik.Kind;
import Logik.MitarbeiterAccount;
import RestAPISchnittstelle.RestApiClient; import RestAPISchnittstelle.RestApiClient;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
@ -11,6 +15,7 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Random; import java.util.Random;
public class AccounterstellungMitarbeiter { public class AccounterstellungMitarbeiter {
@ -25,11 +30,15 @@ public class AccounterstellungMitarbeiter {
private @FXML RadioButton typEltern; private @FXML RadioButton typEltern;
private ToggleGroup accountTyp; private ToggleGroup accountTyp;
private ArrayList<Kind> kinder;
public void initialize() { public void initialize() {
accountTyp = new ToggleGroup(); accountTyp = new ToggleGroup();
accountTyp.getToggles().add(typEltern); accountTyp.getToggles().add(typEltern);
accountTyp.getToggles().add(typMitarbeiter); accountTyp.getToggles().add(typMitarbeiter);
einmalpw.setText(einmalPwGenerieren()); einmalpw.setText(einmalPwGenerieren());
kinder = new ArrayList<>();
} }
public void onZurueck(ActionEvent actionEvent) { public void onZurueck(ActionEvent actionEvent) {
@ -96,9 +105,12 @@ public class AccounterstellungMitarbeiter {
} }
if (vnameGueltig && gebGueltig && nnameGueltig) { if (vnameGueltig && gebGueltig && nnameGueltig) {
Kind kind = new Kind(nnameEingabe.getText(), vnameEingabe.getText(), 0);
kinder.add(kind);
kindanzeige.getChildren().add(neues); kindanzeige.getChildren().add(neues);
neues.setOnAction(a -> { neues.setOnAction(a -> {
((HBox) neues.getParent()).getChildren().remove(neues); ((HBox) neues.getParent()).getChildren().remove(neues);
kinder.remove(kind);
}); });
vnameEingabe.setText(""); vnameEingabe.setText("");
nnameEingabe.setText(""); nnameEingabe.setText("");
@ -126,15 +138,36 @@ public class AccounterstellungMitarbeiter {
status.setText("Daten Erfolgreich gespeichert"); status.setText("Daten Erfolgreich gespeichert");
einmalpw.setText(einmalPwGenerieren()); einmalpw.setText(einmalPwGenerieren());
String json = String.format("{\"name\":\"%s\", \"email\":\"%s\", \"passwort\":\"%d\", \"rid\":\"%d\"}",
accountname.getText(), email.getText(), einmalpw.getText().hashCode(), (typMitarbeiter.isSelected() ? 0:1));
//Gson gson = new Gson(); //String json = String.format("{\"name\":\"%s\", \"email\":\"%s\", \"passwort\":\"%d\", \"rid\":\"%d\"}",
//String jay = gson.toJson(this); // accountname.getText(), email.getText(), einmalpw.getText().hashCode(), (typMitarbeiter.isSelected() ? 0:1));
System.out.println(json); Account neuer;
if (typEltern.isSelected()) {
neuer = new ElternAccount(String.format("%d",einmalpw.getText().hashCode()), accountname.getText(), email.getText());
for (Kind k : kinder) {
((ElternAccount)neuer).getKinder().add(k);
}
}
else if (typMitarbeiter.isSelected()) {
neuer = new MitarbeiterAccount(String.format("%d",einmalpw.getText().hashCode()), accountname.getText(), email.getText());
}
else {
return;
}
kinder.clear();
Gson gson = new Gson();
System.out.println(gson.toJson(neuer));
RestApiClient api = new RestApiClient(); RestApiClient api = new RestApiClient();
api.post("Benutzerkonto", json); api.post("Benutzer", gson.toJson(neuer));
if (neuer instanceof ElternAccount) {
for (Kind k : ((ElternAccount)neuer).getKinder()) {
api.post("Kind", gson.toJson(k));
}
}
} }
else { else {
status.setText("Accounterstellung Fehlgeschlagen"); status.setText("Accounterstellung Fehlgeschlagen");