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

200 lines
6.9 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.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";
2022-01-25 19:37:49 +01:00
private static final String EDIT_EVENT_ENDPOINT = "http://localhost:8080/event/edit";
private static final String ALL_USER_ENDPOINT = "http://localhost:8080/user/all";
private static final String ADD_USER_ENDPOINT = "http://localhost:8080/user/add";
private static final String DELETE_USER_ENDPOINT = "http://localhost:8080/user/del";
private static final String EDIT_USER_ENDPOINT = "http://localhost:8080/user/edit";
2022-01-10 13:22:59 +01:00
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-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];
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());
2022-01-25 20:01:22 +01:00
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;
}
2022-01-25 20:01:22 +01:00
/*********
* Event *
*********/
2022-01-20 13:33:49 +01:00
public void createEvent(Event event) throws HttpRequestException {
2022-01-25 20:01:22 +01:00
sendBasicHttpRequest(
ADD_EVENT_ENDPOINT,
event.getAsUrlParam(),
true
);
}
2022-01-20 13:33:49 +01:00
public void deleteEvent(int userId, int eventId, LocalDateTime date) throws HttpRequestException {
2022-01-25 20:01:22 +01:00
sendBasicHttpRequest(
DELETE_EVENT_ENDPOINT,
"userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(),
true
);
}
2022-01-25 19:37:49 +01:00
public void editEvent(Event oldEvent, Event event) throws HttpRequestException {
2022-01-25 20:01:22 +01:00
sendBasicHttpRequest(
EDIT_EVENT_ENDPOINT,
"eventId=" + oldEvent.getId() +
"&userId=" + oldEvent.getOwnerId() +
"&date=" + oldEvent.getDate().toLocalDate() +
"&newDate=" + event.getDate().toLocalDate() +
"&newName=" + event.getName() +
"&newStart=" + event.getStart() +
"&newEnd=" + event.getEnd() +
"&newPriority=" + event.getPriority() +
"&newIsFullDay=" + event.isFullDay() +
"&newIsPrivate=" + event.isPrivate(),
true
);
2022-01-25 19:37:49 +01:00
}
2022-01-20 13:33:49 +01:00
public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException {
try {
Tuple<Integer, String> response = httpRequest.sendPostRequest(
ALL_EVENTS_ENDPOINT,
"userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(),
true
);
2022-01-25 20:01:22 +01:00
if (response.getKey() != 200) {
2022-01-20 13:33:49 +01:00
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();
2022-01-26 13:55:12 +01:00
return (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>() {});
2022-01-25 20:01:22 +01:00
} catch (HttpRequestException e) {
2022-01-20 13:33:49 +01:00
throw e;
2022-01-25 20:01:22 +01:00
} catch (Exception e) {
2022-01-20 13:33:49 +01:00
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
}
}
2022-01-25 20:01:22 +01:00
/********
* User *
********/
2022-01-26 15:04:32 +01:00
public List<User> getAllUser() throws HttpRequestException {
2022-01-26 13:55:12 +01:00
String userJSON = sendBasicHttpRequest(
ALL_USER_ENDPOINT,
"",
true
);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
try {
return objectMapper.readValue(userJSON, new TypeReference<List<User>>() {});
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return new ArrayList<>();
}
2022-01-25 20:01:22 +01:00
public void createUser(User user) throws HttpRequestException {
sendBasicHttpRequest(
ADD_USER_ENDPOINT,
2022-01-26 13:55:12 +01:00
"",
2022-01-25 20:01:22 +01:00
true
);
}
public void deleteUser(User user) throws HttpRequestException {
sendBasicHttpRequest(
DELETE_USER_ENDPOINT,
2022-01-26 13:55:12 +01:00
"userId=" + user.getUserId(),
2022-01-25 20:01:22 +01:00
true
);
}
public void editUser(User oldUser, User user) throws HttpRequestException {
sendBasicHttpRequest(
EDIT_USER_ENDPOINT,
2022-01-26 13:55:12 +01:00
"",
2022-01-25 20:01:22 +01:00
true
);
}
2022-01-26 13:55:12 +01:00
private String sendBasicHttpRequest(String urlString, String urlParameters, boolean sendAuth) throws HttpRequestException {
2022-01-25 20:01:22 +01:00
try {
Tuple<Integer, String> response = httpRequest.sendPostRequest(
urlString,
urlParameters,
sendAuth
);
if (response.getKey() != 200) {
throw new HttpRequestException(response);
}
2022-01-26 13:55:12 +01:00
return response.getValue();
2022-01-25 20:01:22 +01:00
} catch (HttpRequestException e) {
throw e;
} catch (Exception e) {
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
}
}
2022-01-10 13:22:59 +01:00
}