VPR-Frontend/client/data/src/main/java/res/DataController.java

115 lines
3.7 KiB
Java
Raw Normal View History

package res;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.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";
private final HttpRequest httpRequest;
2021-12-22 15:04:15 +01:00
public DataController() {
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 {
USER_ID = Long.parseLong(httpRequest.sendPostRequest(
LOGIN_ENDPOINT,
"login=" + username
2022-01-11 17:12:37 +01:00
+ "&password=" + password,
false
2021-12-20 19:26:07 +01:00
));
} catch (Exception e) {
e.printStackTrace();
return false;
}
return USER_ID >= 0;
}
2021-12-22 15:04:15 +01:00
public void createEvent(Event event) {
try {
2022-01-11 17:12:37 +01:00
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.");
}
}
2021-12-22 15:04:15 +01:00
public void deleteEvent(int eventId) {
try {
2022-01-11 17:12:37 +01:00
System.out.println(httpRequest.sendPostRequest(DELETE_EVENT_ENDPOINT, "eventId=" + eventId, true));
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<Event> getAllVisibleEvents() {
ArrayList<Event> eventList = new ArrayList<>();
try {
2022-01-11 17:12:37 +01:00
String jsonResponse = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true);
System.out.println(jsonResponse);
ObjectMapper objectMapper = new ObjectMapper();
//String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
2021-12-22 15:04:15 +01:00
for (Object obj : objectMapper.readValue(jsonResponse, Object[].class)) {
ArrayList<Object> list = new ArrayList<>();
if (obj.getClass().isArray()) {
2021-12-22 15:04:15 +01:00
list = (ArrayList<Object>) Arrays.asList((Object[]) obj);
} else if (obj instanceof Collection) {
list = new ArrayList<>((Collection<?>) obj);
}
eventList.add(new Event(list));
}
} 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();
//String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
return objectMapper.readValue(jsonString, Event[].class);
}
2022-01-10 13:22:59 +01:00
}