package res; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import helper.Tuple; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.*; public class DataController { public static long USER_ID = -1; private static final String ALL_EVENTS_ENDPOINT = "http://localhost:8080/event/all"; private static final String ADD_EVENT_ENDPOINT = "http://localhost:8080/event/add"; 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 ALL_USERS_ENDPOINT = "http://localhost:8080/user/all"; private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test"; private final HttpRequest httpRequest; public DataController() { httpRequest = new HttpRequest(); } public boolean login(String username, String password) { try { Tuple response = httpRequest.sendPostRequest( LOGIN_ENDPOINT, "login=" + username + "&password=" + password, false ); String[] data = response.getValue().split("\\s+"); USER_ID = Long.parseLong(data[1]); HttpRequest.TOKEN = data[0]; Tuple auth = httpRequest.sendPostRequest( HEADER_TEST_ENDPOINT, "", true ); System.out.println("auth " + auth); } catch (Exception e) { e.printStackTrace(); return false; } return USER_ID >= 0; } public void createEvent(Event event) { try { System.out.println(httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam(), true)); } catch (Exception e) { throw new RuntimeException("Es konnte keine Verbindung mit dem Server hergestellt werden."); } } public void deleteEvent(int userId, int eventId, LocalDateTime date) { try { System.out.println("DELETE: userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate()); System.out.println(httpRequest.sendPostRequest( DELETE_EVENT_ENDPOINT, "userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(), true )); } catch (Exception e) { e.printStackTrace(); } } public ArrayList getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) { ArrayList eventList = new ArrayList<>(); try { Tuple response = httpRequest.sendPostRequest( ALL_EVENTS_ENDPOINT, "userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(), true ); String jsonResponse = response.getValue(); System.out.println(jsonResponse); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.findAndRegisterModules(); eventList = (ArrayList) objectMapper.readValue(jsonResponse, new TypeReference>(){}); } catch (Exception e) { e.printStackTrace(); } 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); } }