Added edit-function

This commit is contained in:
Marc Beyer 2022-01-20 13:33:49 +01:00
parent 2269f05f52
commit 2f0c4b2a4c
6 changed files with 117 additions and 42 deletions

View File

@ -1,6 +1,7 @@
package main; package main;
import com.jfoenix.controls.*; import com.jfoenix.controls.*;
import helper.HttpRequestException;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Node; import javafx.scene.Node;
@ -74,16 +75,20 @@ public class CreateEventController {
System.out.println(event.getAsUrlParam()); System.out.println(event.getAsUrlParam());
DataController dataController = new DataController(); sendHttpRequest(event);
dataController.createEvent(event);
Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow(); Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
stage.close(); stage.close();
} catch (RuntimeException e) { } catch (HttpRequestException e) {
labelError.setText(e.getMessage()); labelError.setText(e.getMessage());
} }
} }
protected void sendHttpRequest(Event event) throws HttpRequestException {
DataController dataController = new DataController();
dataController.createEvent(event);
}
@FXML @FXML
protected void abortBtnClick(ActionEvent event) { protected void abortBtnClick(ActionEvent event) {
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();

View File

@ -0,0 +1,25 @@
package main;
import helper.HttpRequestException;
import res.DataController;
import res.Event;
public class EditEventController extends CreateEventController{
private Event currentEvent;
public Event getCurrentEvent() {
return currentEvent;
}
public void setCurrentEvent(Event currentEvent) {
this.currentEvent = currentEvent;
}
@Override
protected void sendHttpRequest(Event event) throws HttpRequestException {
DataController dataController = new DataController();
dataController.deleteEvent(currentEvent.getOwnerId(), currentEvent.getId(), currentEvent.getDate());
dataController.createEvent(event);
}
}

View File

@ -1,5 +1,6 @@
package main; package main;
import helper.HttpRequestException;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos; import javafx.geometry.Pos;
@ -56,11 +57,17 @@ public class MainController {
} }
DataController dataController = new DataController(); DataController dataController = new DataController();
ArrayList<Event> eventList = dataController.getAllVisibleEvents(weekStartDateTime, weekStartDateTime.plusDays(7)); try {
ArrayList<Event> eventList = dataController.getAllVisibleEvents(weekStartDateTime, weekStartDateTime.plusDays(7));
for (Event event : eventList) { for (Event event : eventList) {
addEvent(event); addEvent(event);
}
} catch (HttpRequestException e) {
e.printStackTrace();
} }
} }
@FXML @FXML
@ -143,7 +150,11 @@ public class MainController {
deleteBtn.setTextValue(" X "); deleteBtn.setTextValue(" X ");
deleteBtn.setOnAction(e -> { deleteBtn.setOnAction(e -> {
DataController dataController = new DataController(); DataController dataController = new DataController();
dataController.deleteEvent(event.getOwnerId(), event.getId(), event.getDate()); try {
dataController.deleteEvent(event.getOwnerId(), event.getId(), event.getDate());
} catch (HttpRequestException ex) {
ex.printStackTrace();
}
updateEvents(); updateEvents();
}); });
Button editBtn = new Button(); Button editBtn = new Button();
@ -160,7 +171,10 @@ public class MainController {
stage.setScene(scene); stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL); stage.initModality(Modality.APPLICATION_MODAL);
stage.setResizable(false); stage.setResizable(false);
EditEventController editEventController = fxmlLoader.getController();
editEventController.setCurrentEvent(event);
stage.showAndWait(); stage.showAndWait();
updateEvents();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -8,7 +8,7 @@
<?import com.jfoenix.controls.*?> <?import com.jfoenix.controls.*?>
<GridPane fx:id="mainGrid" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1" <GridPane fx:id="mainGrid" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="main.CreateEventController"> fx:controller="main.EditEventController">
<columnConstraints> <columnConstraints>
<ColumnConstraints/> <ColumnConstraints/>

View File

@ -0,0 +1,23 @@
package helper;
public class HttpRequestException extends Exception{
private int status;
public HttpRequestException(String message, int status) {
super(message);
this.status = status;
}
public HttpRequestException(Tuple<Integer, String> response) {
super(response.getValue());
this.status = response.getKey();
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}

View File

@ -3,6 +3,7 @@ package res;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import helper.HttpRequestException;
import helper.Tuple; import helper.Tuple;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -24,6 +25,7 @@ public class DataController {
private static final String DELETE_EVENT_ENDPOINT = "http://localhost:8080/event/del"; private static final String DELETE_EVENT_ENDPOINT = "http://localhost:8080/event/del";
private static final String LOGIN_ENDPOINT = "http://localhost:8080/user/login"; private static final String LOGIN_ENDPOINT = "http://localhost:8080/user/login";
private static final String LOGIN_WITH_TOKEN_ENDPOINT = "http://localhost:8080/user/login-with-token";
private static final String ALL_USERS_ENDPOINT = "http://localhost:8080/user/all"; private static final String ALL_USERS_ENDPOINT = "http://localhost:8080/user/all";
private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test"; private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test";
@ -60,28 +62,52 @@ public class DataController {
return USER_ID >= 0; return USER_ID >= 0;
} }
public void createEvent(Event event) { public boolean loginWIthToken(String username, String password) {
try { try {
System.out.println(httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam(), true)); Tuple<Integer, String> response = httpRequest.sendPostRequest(
LOGIN_WITH_TOKEN_ENDPOINT,
"userId=" + USER_ID,
true
);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Es konnte keine Verbindung mit dem Server hergestellt werden."); e.printStackTrace();
return false;
}
return USER_ID >= 0;
}
public void createEvent(Event event) throws HttpRequestException {
try {
Tuple<Integer, String> response = httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam(), true);
if(response.getKey() != 200){
throw new HttpRequestException(response);
}
}catch (HttpRequestException e){
throw e;
}catch (Exception e) {
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
} }
} }
public void deleteEvent(int userId, int eventId, LocalDateTime date) { public void deleteEvent(int userId, int eventId, LocalDateTime date) throws HttpRequestException {
try { try {
System.out.println("DELETE: userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate()); System.out.println("DELETE: userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate());
System.out.println(httpRequest.sendPostRequest( Tuple<Integer, String> response = httpRequest.sendPostRequest(
DELETE_EVENT_ENDPOINT, DELETE_EVENT_ENDPOINT,
"userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(), "userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(),
true true
)); );
} catch (Exception e) { if(response.getKey() != 200){
e.printStackTrace(); throw new HttpRequestException(response);
}
}catch (HttpRequestException e){
throw e;
}catch (Exception e) {
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
} }
} }
public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) { public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException {
ArrayList<Event> eventList = new ArrayList<>(); ArrayList<Event> eventList = new ArrayList<>();
try { try {
Tuple<Integer, String> response = httpRequest.sendPostRequest( Tuple<Integer, String> response = httpRequest.sendPostRequest(
@ -89,6 +115,9 @@ public class DataController {
"userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(), "userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(),
true true
); );
if(response.getKey() != 200){
throw new HttpRequestException(response);
}
String jsonResponse = response.getValue(); String jsonResponse = response.getValue();
System.out.println(jsonResponse); System.out.println(jsonResponse);
@ -97,35 +126,14 @@ public class DataController {
eventList = (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>(){}); eventList = (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>(){});
} catch (Exception e) { }catch (HttpRequestException e){
e.printStackTrace(); throw e;
}catch (Exception e) {
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
} }
return eventList; return eventList;
} }
public Event[] getAllEvents() {
Event[] eventList = null;
try {
String jsonResponse = httpRequest.sendGetRequest("http://localhost:8080/vpr/all-events-test");
eventList = parseJsonToEventList(jsonResponse);
for (Event e : eventList) {
System.out.println(e);
}
} catch (Exception e) {
e.printStackTrace();
}
return eventList;
}
private Event[] parseJsonToEventList(String jsonString) throws JsonProcessingException {
ArrayList<Event> eventList;
// Parse JSON
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(jsonString, Event[].class);
}
} }