From 2f0c4b2a4c33de9f8cacd8ebd50a76c3f2ca9b50 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Thu, 20 Jan 2022 13:33:49 +0100 Subject: [PATCH] Added edit-function --- .../main/java/main/CreateEventController.java | 11 ++- .../main/java/main/EditEventController.java | 25 ++++++ .../src/main/java/main/MainController.java | 22 +++++- .../src/main/resources/main/edit-event.fxml | 2 +- .../java/helper/HttpRequestException.java | 23 ++++++ .../src/main/java/res/DataController.java | 76 ++++++++++--------- 6 files changed, 117 insertions(+), 42 deletions(-) create mode 100644 client/app/src/main/java/main/EditEventController.java create mode 100644 client/data/src/main/java/helper/HttpRequestException.java diff --git a/client/app/src/main/java/main/CreateEventController.java b/client/app/src/main/java/main/CreateEventController.java index c67fdca..47f69c6 100644 --- a/client/app/src/main/java/main/CreateEventController.java +++ b/client/app/src/main/java/main/CreateEventController.java @@ -1,6 +1,7 @@ package main; import com.jfoenix.controls.*; +import helper.HttpRequestException; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.Node; @@ -74,16 +75,20 @@ public class CreateEventController { System.out.println(event.getAsUrlParam()); - DataController dataController = new DataController(); - dataController.createEvent(event); + sendHttpRequest(event); Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow(); stage.close(); - } catch (RuntimeException e) { + } catch (HttpRequestException e) { labelError.setText(e.getMessage()); } } + protected void sendHttpRequest(Event event) throws HttpRequestException { + DataController dataController = new DataController(); + dataController.createEvent(event); + } + @FXML protected void abortBtnClick(ActionEvent event) { Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); diff --git a/client/app/src/main/java/main/EditEventController.java b/client/app/src/main/java/main/EditEventController.java new file mode 100644 index 0000000..3e34d04 --- /dev/null +++ b/client/app/src/main/java/main/EditEventController.java @@ -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); + } +} diff --git a/client/app/src/main/java/main/MainController.java b/client/app/src/main/java/main/MainController.java index dbdcce6..f16a2c0 100644 --- a/client/app/src/main/java/main/MainController.java +++ b/client/app/src/main/java/main/MainController.java @@ -1,5 +1,6 @@ package main; +import helper.HttpRequestException; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.geometry.Pos; @@ -56,11 +57,17 @@ public class MainController { } DataController dataController = new DataController(); - ArrayList eventList = dataController.getAllVisibleEvents(weekStartDateTime, weekStartDateTime.plusDays(7)); + try { + ArrayList eventList = dataController.getAllVisibleEvents(weekStartDateTime, weekStartDateTime.plusDays(7)); - for (Event event : eventList) { - addEvent(event); + for (Event event : eventList) { + addEvent(event); + } + } catch (HttpRequestException e) { + e.printStackTrace(); } + + } @FXML @@ -143,7 +150,11 @@ public class MainController { deleteBtn.setTextValue(" X "); deleteBtn.setOnAction(e -> { 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(); }); Button editBtn = new Button(); @@ -160,7 +171,10 @@ public class MainController { stage.setScene(scene); stage.initModality(Modality.APPLICATION_MODAL); stage.setResizable(false); + EditEventController editEventController = fxmlLoader.getController(); + editEventController.setCurrentEvent(event); stage.showAndWait(); + updateEvents(); } catch (IOException e) { e.printStackTrace(); } diff --git a/client/app/src/main/resources/main/edit-event.fxml b/client/app/src/main/resources/main/edit-event.fxml index 9180344..939dddb 100644 --- a/client/app/src/main/resources/main/edit-event.fxml +++ b/client/app/src/main/resources/main/edit-event.fxml @@ -8,7 +8,7 @@ + fx:controller="main.EditEventController"> diff --git a/client/data/src/main/java/helper/HttpRequestException.java b/client/data/src/main/java/helper/HttpRequestException.java new file mode 100644 index 0000000..972dd3f --- /dev/null +++ b/client/data/src/main/java/helper/HttpRequestException.java @@ -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 response) { + super(response.getValue()); + this.status = response.getKey(); + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } +} diff --git a/client/data/src/main/java/res/DataController.java b/client/data/src/main/java/res/DataController.java index 2937b9b..279688a 100644 --- a/client/data/src/main/java/res/DataController.java +++ b/client/data/src/main/java/res/DataController.java @@ -3,6 +3,7 @@ package res; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import helper.HttpRequestException; import helper.Tuple; 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 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 HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test"; @@ -60,28 +62,52 @@ public class DataController { return USER_ID >= 0; } - public void createEvent(Event event) { + public boolean loginWIthToken(String username, String password) { try { - System.out.println(httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam(), true)); + Tuple response = httpRequest.sendPostRequest( + LOGIN_WITH_TOKEN_ENDPOINT, + "userId=" + USER_ID, + true + ); } 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 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 { System.out.println("DELETE: userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate()); - System.out.println(httpRequest.sendPostRequest( + Tuple response = httpRequest.sendPostRequest( DELETE_EVENT_ENDPOINT, "userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(), true - )); - } catch (Exception e) { - e.printStackTrace(); + ); + 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 ArrayList getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) { + public ArrayList getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException { ArrayList eventList = new ArrayList<>(); try { Tuple response = httpRequest.sendPostRequest( @@ -89,6 +115,9 @@ public class DataController { "userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(), true ); + if(response.getKey() != 200){ + throw new HttpRequestException(response); + } String jsonResponse = response.getValue(); System.out.println(jsonResponse); @@ -97,35 +126,14 @@ public class DataController { eventList = (ArrayList) objectMapper.readValue(jsonResponse, new TypeReference>(){}); - } catch (Exception e) { - e.printStackTrace(); + }catch (HttpRequestException e){ + throw e; + }catch (Exception e) { + throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600); } 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 eventList; - - // Parse JSON - ObjectMapper objectMapper = new ObjectMapper(); - - return objectMapper.readValue(jsonString, Event[].class); - } } \ No newline at end of file