2021-11-29 15:44:55 +01:00
|
|
|
package res;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2022-01-19 00:16:23 +01:00
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
2021-11-29 15:44:55 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2022-01-13 23:14:36 +01:00
|
|
|
import helper.Tuple;
|
2021-11-29 15:44:55 +01:00
|
|
|
|
|
|
|
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;
|
2022-01-19 00:16:23 +01:00
|
|
|
import java.time.LocalDateTime;
|
2021-11-29 15:44:55 +01:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class DataController {
|
|
|
|
|
2021-12-20 19:26:07 +01:00
|
|
|
public static long USER_ID = -1;
|
|
|
|
|
2022-01-10 13:22:59 +01:00
|
|
|
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";
|
2022-01-13 23:14:36 +01:00
|
|
|
private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test";
|
2021-11-29 15:44:55 +01:00
|
|
|
|
|
|
|
private final HttpRequest httpRequest;
|
|
|
|
|
2021-12-22 15:04:15 +01:00
|
|
|
public DataController() {
|
2021-11-29 15:44:55 +01:00
|
|
|
httpRequest = new HttpRequest();
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:04:15 +01:00
|
|
|
public boolean login(String username, String password) {
|
2021-12-20 19:26:07 +01:00
|
|
|
try {
|
2022-01-13 23:14:36 +01:00
|
|
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(
|
2021-12-20 19:26:07 +01:00
|
|
|
LOGIN_ENDPOINT,
|
|
|
|
"login=" + username
|
2022-01-11 17:12:37 +01:00
|
|
|
+ "&password=" + password,
|
|
|
|
false
|
2022-01-13 17:35:16 +01:00
|
|
|
);
|
2022-01-13 23:14:36 +01:00
|
|
|
String[] data = response.getValue().split("\\s+");
|
2022-01-13 17:35:16 +01:00
|
|
|
|
2022-01-13 23:14:36 +01:00
|
|
|
USER_ID = Long.parseLong(data[1]);
|
2022-01-14 19:37:06 +01:00
|
|
|
HttpRequest.TOKEN = data[0];
|
2022-01-13 23:14:36 +01:00
|
|
|
|
|
|
|
Tuple<Integer, String> auth = httpRequest.sendPostRequest(
|
|
|
|
HEADER_TEST_ENDPOINT,
|
|
|
|
"",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
System.out.println("auth " + auth);
|
2021-12-20 19:26:07 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-13 23:14:36 +01:00
|
|
|
|
2021-12-20 19:26:07 +01:00
|
|
|
return USER_ID >= 0;
|
|
|
|
}
|
|
|
|
|
2021-12-22 15:04:15 +01:00
|
|
|
public void createEvent(Event event) {
|
2021-11-29 15:44:55 +01:00
|
|
|
try {
|
2022-01-11 17:12:37 +01:00
|
|
|
System.out.println(httpRequest.sendPostRequest(ADD_EVENT_ENDPOINT, event.getAsUrlParam(), true));
|
2021-11-29 15:44:55 +01:00
|
|
|
} catch (Exception e) {
|
2021-12-20 12:57:18 +01:00
|
|
|
throw new RuntimeException("Es konnte keine Verbindung mit dem Server hergestellt werden.");
|
2021-11-29 15:44:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:16:23 +01:00
|
|
|
public void deleteEvent(int userId, int eventId, LocalDateTime date) {
|
2021-11-29 15:44:55 +01:00
|
|
|
try {
|
2022-01-19 00:16:23 +01:00
|
|
|
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
|
|
|
|
));
|
2021-11-29 15:44:55 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 00:16:23 +01:00
|
|
|
public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) {
|
2021-11-29 15:44:55 +01:00
|
|
|
ArrayList<Event> eventList = new ArrayList<>();
|
|
|
|
try {
|
2022-01-19 00:16:23 +01:00
|
|
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(
|
|
|
|
ALL_EVENTS_ENDPOINT,
|
|
|
|
"userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(),
|
|
|
|
true
|
|
|
|
);
|
2022-01-13 23:14:36 +01:00
|
|
|
String jsonResponse = response.getValue();
|
2021-11-29 15:44:55 +01:00
|
|
|
System.out.println(jsonResponse);
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
2022-01-19 00:16:23 +01:00
|
|
|
objectMapper.findAndRegisterModules();
|
|
|
|
eventList = (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>(){});
|
2021-11-29 15:44:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
} 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<Event> eventList;
|
|
|
|
|
|
|
|
// Parse JSON
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
|
|
return objectMapper.readValue(jsonString, Event[].class);
|
|
|
|
}
|
2022-01-10 13:22:59 +01:00
|
|
|
}
|