diff --git a/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/config/WebConfig.java b/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/config/WebConfig.java index 64cd712..89f9300 100644 --- a/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/config/WebConfig.java +++ b/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/config/WebConfig.java @@ -13,7 +13,7 @@ public class WebConfig { WebClient webClient = WebClient.builder() .baseUrl("http://resource-service-api:8181") // .defaultCookie("cookie-name", "cookie-value") - .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + // .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .build(); return webClient; } diff --git a/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/controllers/TeachersController.java b/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/controllers/TeachersController.java index 554d375..dc473f4 100644 --- a/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/controllers/TeachersController.java +++ b/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/controllers/TeachersController.java @@ -16,10 +16,13 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @@ -40,6 +43,7 @@ import ru.molokoin.clientserviceteachers.entities.Teacher; import ru.molokoin.clientserviceteachers.services.TeacherService; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; @Controller @@ -48,32 +52,33 @@ public class TeachersController { @Autowired WebClient client; - @GetMapping("/teachers/resend") - public String infoResend(){ - RestTemplate restTemplate = new RestTemplate(); - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.TEXT_PLAIN); - HttpEntity httpEntity = new HttpEntity<>(headers); + // @GetMapping("/teachers/resend") + // public String infoResend(){ + // RestTemplate restTemplate = new RestTemplate(); + // HttpHeaders headers = new HttpHeaders(); + // headers.setContentType(MediaType.TEXT_PLAIN); + // HttpEntity httpEntity = new HttpEntity<>(headers); - ResponseEntity response = - restTemplate.exchange("http://resource-service-api:8181/teacher/list", HttpMethod.GET, httpEntity, String.class); - return response.getBody(); - } + // ResponseEntity response = + // restTemplate.exchange("http://resource-service-api:8181/teacher/list", HttpMethod.GET, httpEntity, String.class); + // return response.getBody(); + // } @GetMapping("/teachers") - public String listThy(Model model) throws JsonMappingException, JsonProcessingException{ - Teacher teacher = new Teacher( - 2, "first_name", "second_name", "last_name", "employee_id", "snils" - ); - model.addAttribute("teacher", teacher); + public String listThy(Model model, @ModelAttribute("teachers") @Validated List teachers) throws JsonMappingException, JsonProcessingException{ + String json = client.method(HttpMethod.GET) + .uri("/teacher/list") + .retrieve() + .bodyToMono(String.class) + .block(); + System.out.println("############################################################################"); + System.out.println("############################################################################"); + System.out.println("json :" + json); - RestTemplate restTemplate = new RestTemplate(); - ResponseEntity response = - restTemplate.exchange("http://resource-service-api:8181/teacher/list", HttpMethod.GET, null, String.class); - String json = response.getBody(); ObjectMapper objectMapper = new ObjectMapper(); - List teachers = objectMapper.readValue(json, new TypeReference>() {}); + teachers = objectMapper.readValue(json, new TypeReference>() {}); model.addAttribute("teachers", teachers); + System.out.println("TEACHERS:" + teachers.toString()); return "teachers"; } @@ -105,7 +110,16 @@ public class TeachersController { // return "teachers"; // } - // @PostMapping(path = "/teacher/add", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}) + /** + * Добавление сведений о преподавателе: + * - получение данных формы (в виде объекта teacher) + * - преобразование объекта teacher в json + * - отправка post запроса с данными json на сервис ресурсов + * - ожидание 1 секунду для уверенности, что запрос обработается сервером + * - направление пользователя на основную страницу + * @param teacher + * @return + */ @PostMapping( path = "/teacher/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, @@ -116,33 +130,57 @@ public class TeachersController { client.post() .uri("/teacher/create") .body(Mono.just(teacher), Teacher.class) - // .bodyValue(BodyInserters.fromValue(teacher)) .retrieve() .toBodilessEntity() - .subscribe( - responseEntity -> { - // Handle success response here - HttpStatusCode status = responseEntity.getStatusCode(); - URI location = responseEntity.getHeaders().getLocation(); - // handle response as necessary - }, - error -> { - // Handle the error here - if (error instanceof WebClientResponseException) { - WebClientResponseException ex = (WebClientResponseException) error; - HttpStatusCode status = ex.getStatusCode(); - System.out.println("!!!Error Status Code: " + status.value()); - //... - } else { - // Handle other types of errors - System.err.println("!!!An unexpected error occurred: " + error.getMessage()); - } - } - ); - // .bodyToMono(String.class) - // .timeout(Duration.ofSeconds(3)) // timeout - // .block(); + .timeout(Duration.ofSeconds(1)) + .block(); return "redirect:/teachers"; } - -} + + // @GetMapping(path = "/teacher/delete/{id}") + // public String deleteTeacherByGet(@PathVariable Long id){ + // client.delete() + // .uri("/teacher/delete/" + id) + // .retrieve() + // // .toBodilessEntity() + // .bodyToMono(String.class) + // .timeout(Duration.ofSeconds(1)) + // // .onErrorResume(e -> Mono.empty()) + // .block(); + // return "redirect:/teachers"; + // } + + @DeleteMapping(path = "/teacher/delete/{id}") + public String deleteTeacher(@PathVariable Long id){ + client.delete() + .uri("/teacher/delete/" + id) + .retrieve() + // .toBodilessEntity() + .bodyToMono(String.class) + .timeout(Duration.ofSeconds(1)) + // .onErrorResume(e -> Mono.empty()) + .block(); + return "redirect:/teachers"; + } + + @PostMapping( + path = "/teacher/update/{id}", + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, + produces = { + MediaType.APPLICATION_JSON_VALUE + }) + public String updateTeacher(@PathVariable Long id + , @ModelAttribute("teacher") @Validated Teacher teacher){ + System.out.println(">>>>>>>>>>>>>> ####################### <<<<<<<<<<<<<<<<<<<<"); + System.out.println(">>>>>>>>>>>>>> ####################### <<<<<<<<<<<<<<<<<<<<"); + System.out.println(">>>>>>>>>>>>>>" + teacher.toString()); + client.post() + .uri("/teacher/update/" + teacher.getId()) + .body(Mono.just(teacher), Teacher.class) + .retrieve() + .toBodilessEntity() + .timeout(Duration.ofSeconds(1)) + .block(); + return "redirect:/teachers"; + } +} \ No newline at end of file diff --git a/client-service-teachers/src/main/resources/application.yaml b/client-service-teachers/src/main/resources/application.yaml index 5274c6f..0abd80a 100644 --- a/client-service-teachers/src/main/resources/application.yaml +++ b/client-service-teachers/src/main/resources/application.yaml @@ -6,4 +6,12 @@ spring: thymeleaf: enabled: true encoding: UTF-8 + mode: LEGACYHTML5 + +# Разрешение на формирование скрытых запросов (DELETE) + mvc: + hiddenmethod: + filter: + enabled: true + diff --git a/client-service-teachers/src/main/resources/templates/index.html b/client-service-teachers/src/main/resources/templates/index.html deleted file mode 100644 index e69de29..0000000 diff --git a/client-service-teachers/src/main/resources/templates/teachers.html b/client-service-teachers/src/main/resources/templates/teachers.html index 8171380..cb3843f 100644 --- a/client-service-teachers/src/main/resources/templates/teachers.html +++ b/client-service-teachers/src/main/resources/templates/teachers.html @@ -17,31 +17,27 @@ xmlns:th="http://www.thymeleaf.org">

Добавление преподавателя:

-
- - + +
- - + +
- - + +
- +
- +
- +

@@ -54,18 +50,35 @@ xmlns:th="http://www.thymeleaf.org"> lastname Табельный номер СНИЛС - delete - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + +
+ +
+
+ +
+ diff --git a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/TeacherController.java b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/TeacherController.java index 2c3b52f..df58d2a 100644 --- a/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/TeacherController.java +++ b/resource-service-api/src/main/java/ru/molokoin/resourceserviceapi/controllers/TeacherController.java @@ -52,7 +52,7 @@ public class TeacherController { return new ResponseEntity<>(teacher, HttpStatus.CREATED); } - @PutMapping(path = "/teacher/update/{id}", + @PostMapping(path = "/teacher/update/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity updateTeacher(@PathVariable Integer id, @RequestBody Teacher teacher) { @@ -70,7 +70,7 @@ public class TeacherController { public ResponseEntity deleteTecher(@PathVariable Long id){ Teacher t = repo.findTeacherById(id); repo.delete(t); - return new ResponseEntity<>("Запись id#" + id + " удалена ... ", HttpStatus.NOT_FOUND); + return new ResponseEntity<>("Запись id#" + id + " удалена ... ", HttpStatus.OK); } }