Map user with JSON

This commit is contained in:
Marc Beyer 2022-01-26 13:55:12 +01:00
parent 4aafae9896
commit f133257194
2 changed files with 45 additions and 25 deletions

View File

@ -118,7 +118,6 @@ public class DataController {
} }
public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException { public ArrayList<Event> getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException {
ArrayList<Event> eventList = new ArrayList<>();
try { try {
Tuple<Integer, String> response = httpRequest.sendPostRequest( Tuple<Integer, String> response = httpRequest.sendPostRequest(
ALL_EVENTS_ENDPOINT, ALL_EVENTS_ENDPOINT,
@ -133,28 +132,40 @@ public class DataController {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules(); objectMapper.findAndRegisterModules();
eventList = (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>() { return (ArrayList<Event>) objectMapper.readValue(jsonResponse, new TypeReference<List<Event>>() {});
});
} catch (HttpRequestException e) { } catch (HttpRequestException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600); throw new HttpRequestException("Es konnte keine Verbindung mit dem Server hergestellt werden.", 600);
} }
return eventList;
} }
/******** /********
* User * * User *
********/ ********/
/* public List<User> getUser() throws HttpRequestException {
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<>();
}
public void createUser(User user) throws HttpRequestException { public void createUser(User user) throws HttpRequestException {
sendBasicHttpRequest( sendBasicHttpRequest(
ADD_USER_ENDPOINT, ADD_USER_ENDPOINT,
user.getAsUrlParam(), "",
true true
); );
} }
@ -162,7 +173,7 @@ public class DataController {
public void deleteUser(User user) throws HttpRequestException { public void deleteUser(User user) throws HttpRequestException {
sendBasicHttpRequest( sendBasicHttpRequest(
DELETE_USER_ENDPOINT, DELETE_USER_ENDPOINT,
user.getAsUrlParam(), "userId=" + user.getUserId(),
true true
); );
} }
@ -170,13 +181,12 @@ public class DataController {
public void editUser(User oldUser, User user) throws HttpRequestException { public void editUser(User oldUser, User user) throws HttpRequestException {
sendBasicHttpRequest( sendBasicHttpRequest(
EDIT_USER_ENDPOINT, EDIT_USER_ENDPOINT,
user.getAsUrlParam(), "",
true true
); );
} }
*/
private void sendBasicHttpRequest(String urlString, String urlParameters, boolean sendAuth) throws HttpRequestException { private String sendBasicHttpRequest(String urlString, String urlParameters, boolean sendAuth) throws HttpRequestException {
try { try {
Tuple<Integer, String> response = httpRequest.sendPostRequest( Tuple<Integer, String> response = httpRequest.sendPostRequest(
urlString, urlString,
@ -186,6 +196,8 @@ public class DataController {
if (response.getKey() != 200) { if (response.getKey() != 200) {
throw new HttpRequestException(response); throw new HttpRequestException(response);
} }
return response.getValue();
} catch (HttpRequestException e) { } catch (HttpRequestException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {

View File

@ -2,28 +2,28 @@ package res;
public class User { public class User {
private int id; private int userId;
private String loginName; private String login;
private String forename; private String forename;
private String name; private String name;
private boolean isAdmin;
public User(){ public User(){}
public int getUserId() {
return userId;
} }
public int getId() { public void setUserId(int userId) {
return id; this.userId = userId;
} }
public void setId(int id) { public String getLogin() {
this.id = id; return login;
} }
public String getLoginName() { public void setLogin(String login) {
return loginName; this.login = login;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
} }
public String getForename() { public String getForename() {
@ -41,4 +41,12 @@ public class User {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean admin) {
isAdmin = admin;
}
} }