diff --git a/client-service-teachers/pom.xml b/client-service-teachers/pom.xml
index 6e8318e..7695685 100644
--- a/client-service-teachers/pom.xml
+++ b/client-service-teachers/pom.xml
@@ -17,6 +17,10 @@
17
+
+ org.springframework.boot
+ spring-boot-starter-webflux
+
org.springframework.boot
spring-boot-starter-thymeleaf
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
new file mode 100644
index 0000000..64cd712
--- /dev/null
+++ b/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/config/WebConfig.java
@@ -0,0 +1,20 @@
+package ru.molokoin.clientserviceteachers.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.web.reactive.function.client.WebClient;
+
+@Configuration
+public class WebConfig {
+ @Bean
+ public WebClient webClient() {
+ WebClient webClient = WebClient.builder()
+ .baseUrl("http://resource-service-api:8181")
+ // .defaultCookie("cookie-name", "cookie-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 bc78991..554d375 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
@@ -1,22 +1,33 @@
package ru.molokoin.clientserviceteachers.controllers;
+import java.net.URI;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
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.GetMapping;
+import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.client.ClientRequest;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
@@ -24,11 +35,19 @@ import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
+import reactor.core.publisher.Mono;
import ru.molokoin.clientserviceteachers.entities.Teacher;
+import ru.molokoin.clientserviceteachers.services.TeacherService;
+
+import org.springframework.web.bind.annotation.PostMapping;
+
@Controller
@RequestMapping(path = "/")
public class TeachersController {
+ @Autowired
+ WebClient client;
+
@GetMapping("/teachers/resend")
public String infoResend(){
RestTemplate restTemplate = new RestTemplate();
@@ -57,4 +76,73 @@ public class TeachersController {
model.addAttribute("teachers", teachers);
return "teachers";
}
+
+ /**
+ * сохранение записи о новом преподавателе:
+ * - подготовка сущности записи
+ * - преобразование записи в json
+ * - отправка post запроса на сервис-ресурсов
+ *
+ * @param entity
+ * @return
+ * @throws JsonProcessingException
+ */
+ // @PostMapping(path = "/teacher/create",
+ // consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
+ // public String postMethodName(@RequestBody Teacher teacher) throws JsonProcessingException {
+ // String url = "http://resource-service-api:8181/teacher/create";
+ // // model.addAttribute("teacher", teacher);
+ // RestTemplate restTemplate = new RestTemplate();
+ // // HttpHeaders headers = new HttpHeaders();
+ // // headers.setContentType(MediaType.APPLICATION_JSON);
+ // ObjectMapper objectMapper = new ObjectMapper();
+ // String json = objectMapper.writeValueAsString(teacher);
+ // System.out.println("СООБЩЕНИЕ ИЗ СЕРВИСА-КЛИЕНТОВ:");
+ // System.out.println(json);
+ // // ResponseEntity response =
+ // // restTemplate.postForEntity(url, teacher, Teacher.class);
+ // restTemplate.postForEntity(url, json, String.class);
+ // return "teachers";
+ // }
+
+ // @PostMapping(path = "/teacher/add", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
+ @PostMapping(
+ path = "/teacher/add",
+ consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
+ produces = {
+ MediaType.APPLICATION_JSON_VALUE
+ })
+ public String postTeacher(@ModelAttribute("teacher") @Validated Teacher teacher){
+ 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();
+ return "redirect:/teachers";
+ }
+
}
diff --git a/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/services/TeacherService.java b/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/services/TeacherService.java
new file mode 100644
index 0000000..c28c15b
--- /dev/null
+++ b/client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/services/TeacherService.java
@@ -0,0 +1,22 @@
+package ru.molokoin.clientserviceteachers.services;
+
+import org.springframework.stereotype.Service;
+import org.springframework.web.reactive.function.client.WebClient;
+
+import lombok.AllArgsConstructor;
+import reactor.core.publisher.Mono;
+import ru.molokoin.clientserviceteachers.entities.Teacher;
+
+@Service
+@AllArgsConstructor
+public class TeacherService {
+ private final WebClient webClient;
+
+ public Mono getTeacherByIdAsync(final String id){
+ return webClient
+ .get()
+ .uri(String.join("", "/teacher/", id))
+ .retrieve()
+ .bodyToMono(Teacher.class);
+ }
+}
diff --git a/client-service-teachers/src/main/resources/application.yaml b/client-service-teachers/src/main/resources/application.yaml
index 144628c..5274c6f 100644
--- a/client-service-teachers/src/main/resources/application.yaml
+++ b/client-service-teachers/src/main/resources/application.yaml
@@ -3,3 +3,7 @@ server:
spring:
application:
name: client-service-teachers
+ thymeleaf:
+ enabled: true
+ encoding: UTF-8
+
diff --git a/client-service-teachers/src/main/resources/templates/teachers.html b/client-service-teachers/src/main/resources/templates/teachers.html
index 3bb9d05..8171380 100644
--- a/client-service-teachers/src/main/resources/templates/teachers.html
+++ b/client-service-teachers/src/main/resources/templates/teachers.html
@@ -8,17 +8,67 @@ xmlns:th="http://www.thymeleaf.org">
-
-
-
+
+
+
+
+
+
Добавление преподавателя:
+
-
-
- |
- |
-
-
+
+
Перечень преподавателей:
+
+
+ id |
+ firstname |
+ secondname |
+ lastname |
+ Табельный номер |
+ СНИЛС |
+
+ delete |
+
+
+ |
+ |
+ |
+ |
+ |
+ |
+
+ - |
+
+
+
+