diff --git a/src/main/java/ru/cft/task/restClient/RestActionsController.java b/src/main/java/ru/cft/task/restClient/RestActionsController.java index 8caf136..a64389f 100644 --- a/src/main/java/ru/cft/task/restClient/RestActionsController.java +++ b/src/main/java/ru/cft/task/restClient/RestActionsController.java @@ -5,13 +5,10 @@ import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.stage.Stage; -import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpStatusCodeException; -import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -56,25 +53,18 @@ public class RestActionsController { } } - public void cancelAction() { - Stage stage = (Stage) cancel.getScene().getWindow(); - stage.close(); - } - - public void createAction() { + private void createOrUpdate(HttpMethod method, MultiValueMap params) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - MultiValueMap map = new LinkedMultiValueMap(); - map.add("name", name.getText()); - map.add("email", email.getText()); - - HttpEntity> entity = new HttpEntity>(map, headers); + HttpEntity> entity = new HttpEntity>(params, headers); try { - ResponseEntity response = restTemplate.exchange(getRestServerUrl(), HttpMethod.POST, entity, EmailRecord.class); + ResponseEntity response = restTemplate.exchange(getRestServerUrl(), method, entity, EmailRecord.class); if (response.getStatusCode() == HttpStatus.OK) { EmailRecord emailRecord = response.getBody(); id.setText(String.valueOf(emailRecord.getId())); + name.setText(emailRecord.getName()); + email.setText(emailRecord.getEmail()); } } catch (HttpStatusCodeException ex) { try { @@ -87,6 +77,18 @@ public class RestActionsController { } } + public void cancelAction() { + Stage stage = (Stage) cancel.getScene().getWindow(); + stage.close(); + } + + public void createAction() { + MultiValueMap map = new LinkedMultiValueMap(); + map.add("name", name.getText()); + map.add("email", email.getText()); + createOrUpdate(HttpMethod.POST, map); + } + public void readAction() { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); @@ -121,10 +123,34 @@ public class RestActionsController { } public void updateAction() { - + MultiValueMap map = new LinkedMultiValueMap(); + map.add("id", id.getText()); + map.add("name", name.getText()); + map.add("email", email.getText()); + createOrUpdate(HttpMethod.PUT, map); } public void deleteAction() { + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + String delete_url = getRestServerUrl().concat("/delete/").concat(id.getText()); + HttpEntity entity = new HttpEntity<>(headers); + try { + ResponseEntity response = restTemplate.exchange(delete_url, HttpMethod.DELETE, entity, ErrorResponse.class); + if (response.getStatusCode() == HttpStatus.OK) { + ErrorResponse result = response.getBody(); + Utils.showAlert("info", result.getMessage()); + } + } catch (HttpStatusCodeException ex) { + try { + ObjectMapper objectMapper = new ObjectMapper(); + ErrorResponse err = objectMapper.readValue(ex.getResponseBodyAsString(), ErrorResponse.class); + Utils.showAlert("error", err.getMessage()); + } catch (IOException e) { + Utils.showAlert("error", e.getMessage()); + } + } } } diff --git a/src/main/resources/form_delete.fxml b/src/main/resources/form_delete.fxml index 4a733d0..c5a1e07 100644 --- a/src/main/resources/form_delete.fxml +++ b/src/main/resources/form_delete.fxml @@ -5,11 +5,11 @@ - +