Browse Source

teachers-done

master
esoe 7 months ago
parent
commit
8fc0a0fac7
  1. 6
      client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/config/WebConfig.java
  2. 153
      client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/controllers/TeachersController.java
  3. 49
      client-service-teachers/src/main/resources/templates/teacher-add.html
  4. 49
      client-service-teachers/src/main/resources/templates/teacher-edit.html
  5. 118
      client-service-teachers/src/main/resources/templates/teachers.html
  6. 2
      postgres-service/dockerfile

6
client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/config/WebConfig.java

@ -2,18 +2,14 @@ package ru.molokoin.clientserviceteachers.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
@Configuration @Configuration
public class WebConfig { public class WebConfig {
@Bean @Bean
public WebClient webClient() { WebClient webClient() {
WebClient webClient = WebClient.builder() WebClient webClient = WebClient.builder()
.baseUrl("http://resource-service-api:8181") .baseUrl("http://resource-service-api:8181")
// .defaultCookie("cookie-name", "cookie-value")
// .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.build(); .build();
return webClient; return webClient;
} }

153
client-service-teachers/src/main/java/ru/molokoin/clientserviceteachers/controllers/TeachersController.java

@ -1,18 +1,12 @@
package ru.molokoin.clientserviceteachers.controllers; package ru.molokoin.clientserviceteachers.controllers;
import java.net.URI;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -20,95 +14,65 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable; 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.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;
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.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import ru.molokoin.clientserviceteachers.entities.Teacher; 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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
@Controller @Controller
@RequestMapping(path = "/") @RequestMapping(path = "/")
public class TeachersController { public class TeachersController {
@Autowired @Autowired
WebClient client; private 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);
// ResponseEntity<String> response =
// restTemplate.exchange("http://resource-service-api:8181/teacher/list", HttpMethod.GET, httpEntity, String.class);
// return response.getBody();
// }
@GetMapping("/teachers") @GetMapping("/teachers")
public String listThy(Model model, @ModelAttribute("teachers") @Validated List<Teacher> teachers) throws JsonMappingException, JsonProcessingException{ public String teacherList(Model model) throws JsonMappingException, JsonProcessingException{
String json = client.method(HttpMethod.GET) // ModelAndView mav = new ModelAndView("teachers");
List<Teacher> teachers = client.method(HttpMethod.GET)
.uri("/teacher/list") .uri("/teacher/list")
.retrieve() .retrieve()
.bodyToMono(String.class) .bodyToMono(new ParameterizedTypeReference <List<Teacher>>(){})
.block(); .block();
System.out.println("############################################################################");
System.out.println("############################################################################"); model.addAttribute("teacher_list", teachers);
System.out.println("json :" + json); model.addAttribute("t", new Teacher());
// mav.addObject("teacher_list", teachers);
ObjectMapper objectMapper = new ObjectMapper(); // mav.addObject("t", new Teacher());
teachers = objectMapper.readValue(json, new TypeReference<List<Teacher>>() {}); // mav.addObject("teacher", new Teacher());
model.addAttribute("teachers", teachers);
System.out.println("TEACHERS:" + teachers.toString()); System.out.println("TEACHERS:" + teachers.toString());
return "teachers"; return "teachers";
// return mav;
} }
/** /**
* сохранение записи о новом преподавателе: * Вывод сведений о преподавателе
* - подготовка сущности записи * @param model
* - преобразование записи в json * @param id
* - отправка post запроса на сервис-ресурсов
*
* @param entity
* @return * @return
* @throws JsonProcessingException
*/ */
// @PostMapping(path = "/teacher/create", @GetMapping("/teacher-edit/{id}")
// consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}) public String teacherById(Model model, @PathVariable Long id) {
// public String postMethodName(@RequestBody Teacher teacher) throws JsonProcessingException { Teacher t = client.get()
// String url = "http://resource-service-api:8181/teacher/create"; .uri("/teacher/" + id)
// // model.addAttribute("teacher", teacher); .retrieve()
// RestTemplate restTemplate = new RestTemplate(); .bodyToMono(Teacher.class)
// // HttpHeaders headers = new HttpHeaders(); .timeout(Duration.ofSeconds(1))
// // headers.setContentType(MediaType.APPLICATION_JSON); .block();
// ObjectMapper objectMapper = new ObjectMapper(); model.addAttribute("teacher", t);
// String json = objectMapper.writeValueAsString(teacher); // System.out.println("TEACHERS:" + t.toString());
// System.out.println("СООБЩЕНИЕ ИЗ СЕРВИСА-КЛИЕНТОВ:"); return "teacher-edit";
// System.out.println(json); }
// // ResponseEntity<String> response =
// // restTemplate.postForEntity(url, teacher, Teacher.class);
// restTemplate.postForEntity(url, json, String.class);
// return "teachers";
// }
/** /**
* Добавление сведений о преподавателе: * Добавление сведений о преподавателе:
@ -119,6 +83,8 @@ public class TeachersController {
* - направление пользователя на основную страницу * - направление пользователя на основную страницу
* @param teacher * @param teacher
* @return * @return
* @throws JsonProcessingException
* @throws JsonMappingException
*/ */
@PostMapping( @PostMapping(
path = "/teacher/add", path = "/teacher/add",
@ -126,7 +92,7 @@ public class TeachersController {
produces = { produces = {
MediaType.APPLICATION_JSON_VALUE MediaType.APPLICATION_JSON_VALUE
}) })
public String postTeacher(@ModelAttribute("teacher") @Validated Teacher teacher){ public String addTeacher(@ModelAttribute("t") @Validated Teacher teacher) throws JsonMappingException, JsonProcessingException{
client.post() client.post()
.uri("/teacher/create") .uri("/teacher/create")
.body(Mono.just(teacher), Teacher.class) .body(Mono.just(teacher), Teacher.class)
@ -135,34 +101,48 @@ public class TeachersController {
.timeout(Duration.ofSeconds(1)) .timeout(Duration.ofSeconds(1))
.block(); .block();
return "redirect:/teachers"; return "redirect:/teachers";
// return teacherList();
} }
// @GetMapping(path = "/teacher/delete/{id}") @GetMapping(
// public String deleteTeacherByGet(@PathVariable Long id){ path = "/teacher/add"
// client.delete() )
// .uri("/teacher/delete/" + id) public String addTeacherFrame(Model model){
// .retrieve() Teacher teacher = new Teacher();
// // .toBodilessEntity() model.addAttribute("newTeacher", teacher);
// .bodyToMono(String.class) return "teacher-add";
// .timeout(Duration.ofSeconds(1)) }
// // .onErrorResume(e -> Mono.empty())
// .block();
// return "redirect:/teachers";
// }
@DeleteMapping(path = "/teacher/delete/{id}") /**
public String deleteTeacher(@PathVariable Long id){ * Удаление записи о преподавателе
* @param id
* @return
* @throws JsonProcessingException
* @throws JsonMappingException
*/
@GetMapping(path = "/teacher/delete/{id}")
public String deleteTeacher(@PathVariable Long id) throws JsonMappingException, JsonProcessingException{
client.delete() client.delete()
.uri("/teacher/delete/" + id) .uri("/teacher/delete/" + id)
.retrieve() .retrieve()
// .toBodilessEntity()
.bodyToMono(String.class) .bodyToMono(String.class)
.timeout(Duration.ofSeconds(1)) .timeout(Duration.ofSeconds(1))
// .onErrorResume(e -> Mono.empty())
.block(); .block();
return "redirect:/teachers"; return "redirect:/teachers";
// return teacherList();
} }
/**
* Обновление сведений о преподавателе
* - переделать post на put запрос
*
* @param id
* @param teacher
* @return
* @throws JsonProcessingException
* @throws JsonMappingException
*/
@PostMapping( @PostMapping(
path = "/teacher/update/{id}", path = "/teacher/update/{id}",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
@ -170,7 +150,7 @@ public class TeachersController {
MediaType.APPLICATION_JSON_VALUE MediaType.APPLICATION_JSON_VALUE
}) })
public String updateTeacher(@PathVariable Long id public String updateTeacher(@PathVariable Long id
, @ModelAttribute("teacher") @Validated Teacher teacher){ , @ModelAttribute("t") @Validated Teacher teacher) throws JsonMappingException, JsonProcessingException{
System.out.println(">>>>>>>>>>>>>> ####################### <<<<<<<<<<<<<<<<<<<<"); System.out.println(">>>>>>>>>>>>>> ####################### <<<<<<<<<<<<<<<<<<<<");
System.out.println(">>>>>>>>>>>>>> ####################### <<<<<<<<<<<<<<<<<<<<"); System.out.println(">>>>>>>>>>>>>> ####################### <<<<<<<<<<<<<<<<<<<<");
System.out.println(">>>>>>>>>>>>>>" + teacher.toString()); System.out.println(">>>>>>>>>>>>>>" + teacher.toString());
@ -182,5 +162,6 @@ public class TeachersController {
.timeout(Duration.ofSeconds(1)) .timeout(Duration.ofSeconds(1))
.block(); .block();
return "redirect:/teachers"; return "redirect:/teachers";
// return teacherList();
} }
} }

49
client-service-teachers/src/main/resources/templates/teacher-add.html

@ -0,0 +1,49 @@
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Teacher</title>
<script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs@2/webcomponents-loader.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@1/src/zero-md.min.js"></script>
</head>
<body>
<header>
edit : teacher
</header>
<main>
<h2>Добавление преподавателя:</h2>
<form th:action="@{/teacher/add}" method="post" th:object="${newTeacher}">
<div>
<label>id: </label>
<input type="text" th:field="${newTeacher.id}" placeholder="" readonly />
</div>
<div>
<label>Фамилия: </label>
<input type="text" th:field="${newTeacher.first_name}" placeholder="" />
</div>
<div>
<label>Имя: </label>
<input type="text" th:field="${newTeacher.second_name}" placeholder="" />
</div>
<div>
<label>Отчество: </label>
<input type="text" th:field="${newTeacher.last_name}" placeholder="" />
</div>
<div>
<label>Табельный номер</label>
<input type="text" th:field="${newTeacher.employee_id}" placeholder="" />
</div>
<div>
<label>СНИЛС</label>
<input type="text" th:field="${newTeacher.snils}" placeholder="" />
</div>
<br>
<input type="submit" value="СОХРАНИТЬ И ВЕРНУТЬСЯ"/>
</form>
<br>
<form th:action="@{/teachers}" th:method="get">
<input type="submit" value="ВЕРНУТЬСЯ"/>
</form>
</main>
</body>

49
client-service-teachers/src/main/resources/templates/teacher-edit.html

@ -0,0 +1,49 @@
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>Teacher</title>
<script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs@2/webcomponents-loader.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@1/src/zero-md.min.js"></script>
</head>
<body>
<header>
edit : teacher
</header>
<main>
<h2>Добавление преподавателя:</h2>
<form th:action="@{/teacher/add}" method="post" th:object="${teacher}">
<div>
<label>id: </label>
<input type="text" th:field="${teacher.id}" placeholder="" readonly />
</div>
<div>
<label>Фамилия: </label>
<input type="text" th:field="${teacher.first_name}" placeholder="" />
</div>
<div>
<label>Имя: </label>
<input type="text" th:field="${teacher.second_name}" placeholder="" />
</div>
<div>
<label>Отчество: </label>
<input type="text" th:field="${teacher.last_name}" placeholder="" />
</div>
<div>
<label>Табельный номер</label>
<input type="text" th:field="${teacher.employee_id}" placeholder="" />
</div>
<div>
<label>СНИЛС</label>
<input type="text" th:field="${teacher.snils}" placeholder="" />
</div>
<br>
<input type="submit" value="СОХРАНИТЬ И ВЕРНУТЬСЯ"/>
</form>
<br>
<form th:action="@{/teachers}" th:method="get">
<input type="submit" value="ВЕРНУТЬСЯ"/>
</form>
</main>
</body>

118
client-service-teachers/src/main/resources/templates/teachers.html

@ -3,7 +3,7 @@
xmlns:th="http://www.thymeleaf.org"> xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Teacher</title> <title>Teachers</title>
<script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs@2/webcomponents-loader.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@webcomponents/webcomponentsjs@2/webcomponents-loader.min.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@1/src/zero-md.min.js"></script> <script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@1/src/zero-md.min.js"></script>
</head> </head>
@ -13,73 +13,59 @@ xmlns:th="http://www.thymeleaf.org">
</header> </header>
<main> <main>
<div class="main-wraper"> <div class="main-wraper">
<zero-md th:src="@{/teachers.md}"></zero-md> <!-- Вывод сведений о приложении из *.md файла (client-service-teachers\src\main\resources\static\teachers.md) -->
<br> <!-- <zero-md th:src="@{/teachers.md}"></zero-md>
<h2>Добавление преподавателя:</h2> <br> -->
<form th:action="@{/teacher/add}" method="post" th:object="${teacher}">
<div>
<label>Фамилия: </label>
<input type="text" th:field="*{first_name}" placeholder="" />
</div>
<div>
<label>Имя: </label>
<input type="text" th:field="*{second_name}" placeholder="" />
</div>
<div>
<label>Отчество: </label>
<input type="text" th:field="*{last_name}" placeholder="" />
</div>
<div>
<label>Табельный номер</label>
<input type="text" th:field="*{employee_id}" placeholder="" />
</div>
<div>
<label>СНИЛС</label>
<input type="text" th:field="*{snils}" placeholder="" />
</div>
<input type="submit" value="ДОБАВИТЬ"/>
</form>
<br>
<h2>Перечень преподавателей:</h2> <h2>Перечень преподавателей:</h2>
<table> <form th:action="@{/teacher/list}" method="get" th:object="${t}"></form>
<tr> <table>
<th>id</th> <thead>
<th>firstname</th> <tr>
<th>secondname</th> <th>id</th>
<th>lastname</th> <th>firstname</th>
<th>Табельный номер</th> <th>secondname</th>
<th>СНИЛС</th> <th>lastname</th>
<th>delete</th> <th>Табельный номер</th>
</tr> <th>СНИЛС</th>
<tr th:each="teacher: ${teachers}" th:object="${teacher}"> <th>
<td> <form th:action="@{/teacher/add}" th:method="get">
<input type="text" th:field="${teacher.id}" placeholder="${teacher.id}"/> <input type="submit" value="ADD --|>"/>
</td> </form>
<td> </th>
<input type="text" th:field="${teacher.first_name}" placeholder="${teacher.first_name}"/> </tr>
</td> </thead>
<td> <tbody>
<input type="text" th:field="${teacher.second_name}" placeholder="string"/> <tr th:each="t: ${teacher_list}">
</td> <td>
<td> <input type="text" id="id" name="id" th:value="${t.id}" readonly />
<input type="text" th:field="${teacher.last_name}" placeholder="string"/> </td>
</td> <td>
<td> <input type="text" id="first_name" name="first_name" th:value="${t.first_name}" readonly />
<input type="text" th:field="${teacher.employee_id}" placeholder="string"/> </td>
</td> <td>
<td> <input type="text" id="second_name" name="second_name" th:value="${t.second_name}" readonly />
<input type="text" th:field="${teacher.snils}" placeholder="string"/> </td>
</td> <td>
<td> <input type="text" id="last_name" name="last_name" th:value="${t.last_name}" readonly />
<form th:action="@{teacher/update/{id}(id=${teacher.id})}" th:method="post"> </td>
<input type="submit" value="+"/> <td>
</form> <input type="text" id="employee_id" name="employee_id" th:value="${t.employee_id}" readonly />
<form th:action="@{teacher/delete/{id}(id=${teacher.id})}" th:method="delete"> </td>
<input type="submit" value="-"/> <td>
</form> <input type="text" id="snils" name="snils" th:value="${t.snils}" readonly />
</td> </td>
</tr> <td>
<form th:action="@{teacher-edit/{id}(id=${t.id})}" th:method="get">
<input type="submit" value="--|>"/>
</form>
<form th:action="@{/teacher/delete/{id}(id=${t.id})}" th:method="get">
<input type="submit" value="X"/>
</form>
</td>
</tr>
</tbody>
</table>
</table> </table>
</div> </div>
</main> </main>

2
postgres-service/dockerfile

@ -4,4 +4,4 @@ LABEL author="esoe"
LABEL description="postgres image for tech-services" LABEL description="postgres image for tech-services"
LABEL version="1.0" LABEL version="1.0"
COPY *.sql /docker-entrypoint-initdb.d/ # COPY *.sql /docker-entrypoint-initdb.d/

Loading…
Cancel
Save