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

146 lines
5.3 KiB
Java
Raw Normal View History

package res;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
2022-01-20 13:33:49 +01:00
import helper.HttpRequestException;
2022-01-13 23:14:36 +01:00
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 {
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";
2022-01-20 13:33:49 +01:00
private static final String LOGIN_WITH_TOKEN_ENDPOINT = "http://localhost:8080/user/login-with-token";
2022-01-10 13:22:59 +01:00
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";
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 {
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;
}
2022-01-23 21:21:16 +01:00
public boolean loginWithToken(long userId, String token) {
try {
2022-01-23 21:21:16 +01:00
HttpRequest.TOKEN = token;
2022-01-20 13:33:49 +01:00
Tuple<Integer, String> response = httpRequest.sendPostRequest(
LOGIN_WITH_TOKEN_ENDPOINT,
2022-01-23 21:21:16 +01:00
"userId=" + userId,
2022-01-20 13:33:49 +01:00
true
);
2022-01-23 21:21:16 +01:00
System.out.println(response.getKey() + " " + response.getValue());
if(response.getKey() != 200) return false;
} catch (Exception e) {
2022-01-20 13:33:49 +01:00
e.printStackTrace();
return false;
}
2022-01-23 21:21:16 +01:00
USER_ID = userId;
HttpRequest.TOKEN = token;
2022-01-20 13:33:49 +01:00
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);
}
}
2022-01-20 13:33:49 +01:00
public void deleteEvent(int userId, int eventId, LocalDateTime date) throws HttpRequestException {
try {
System.out.println("DELETE: userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate());
2022-01-20 13:33:49 +01:00
Tuple<Integer, String> response = httpRequest.sendPostRequest(
DELETE_EVENT_ENDPOINT,
"userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(),
true
2022-01-20 13:33:49 +01:00
);
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);
}
}
2022-01-20 13:33:49 +01:00
public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException {
ArrayList<Event> eventList = new ArrayList<>();
try {
Tuple<Integer, String> response = httpRequest.sendPostRequest(
ALL_EVENTS_ENDPOINT,
"userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(),
true
);
2022-01-20 13:33:49 +01:00
if(response.getKey() != 200){
throw new HttpRequestException(response);
}
2022-01-13 23:14:36 +01:00
String jsonResponse = response.getValue();
System.out.println(jsonResponse);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
eventList = (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>(){});
2022-01-20 13:33:49 +01:00
}catch (HttpRequestException e){
throw e;
}catch (Exception e) {
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
}
return eventList;
}
2022-01-10 13:22:59 +01:00
}