|
|
|
@ -16,10 +16,13 @@ import org.springframework.http.ResponseEntity;
@@ -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;
@@ -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 {
@@ -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<String> httpEntity = new HttpEntity<>(headers); |
|
|
|
|
// @GetMapping("/teachers/resend")
|
|
|
|
|
// public String infoResend(){
|
|
|
|
|
// RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
// HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
// headers.setContentType(MediaType.TEXT_PLAIN);
|
|
|
|
|
// HttpEntity<String> httpEntity = new HttpEntity<>(headers);
|
|
|
|
|
|
|
|
|
|
ResponseEntity<String> response = |
|
|
|
|
restTemplate.exchange("http://resource-service-api:8181/teacher/list", HttpMethod.GET, httpEntity, String.class); |
|
|
|
|
return response.getBody(); |
|
|
|
|
} |
|
|
|
|
// ResponseEntity<String> 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); |
|
|
|
|
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate(); |
|
|
|
|
ResponseEntity<String> response = |
|
|
|
|
restTemplate.exchange("http://resource-service-api:8181/teacher/list", HttpMethod.GET, null, String.class); |
|
|
|
|
String json = response.getBody(); |
|
|
|
|
public String listThy(Model model, @ModelAttribute("teachers") @Validated List<Teacher> 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); |
|
|
|
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
|
List<Teacher> teachers = objectMapper.readValue(json, new TypeReference<List<Teacher>>() {}); |
|
|
|
|
teachers = objectMapper.readValue(json, new TypeReference<List<Teacher>>() {}); |
|
|
|
|
model.addAttribute("teachers", teachers); |
|
|
|
|
System.out.println("TEACHERS:" + teachers.toString()); |
|
|
|
|
return "teachers"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -105,7 +110,16 @@ public class TeachersController {
@@ -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 {
@@ -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"; |
|
|
|
|
} |
|
|
|
|
} |