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. 66
      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; @@ -2,18 +2,14 @@ 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 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;
}

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

@ -1,18 +1,12 @@ @@ -1,18 +1,12 @@
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.core.ParameterizedTypeReference;
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;
@ -20,95 +14,65 @@ import org.springframework.web.bind.annotation.DeleteMapping; @@ -20,95 +14,65 @@ 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;
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;
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;
import org.springframework.web.bind.annotation.PutMapping;
@Controller
@RequestMapping(path = "/")
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);
// ResponseEntity<String> response =
// restTemplate.exchange("http://resource-service-api:8181/teacher/list", HttpMethod.GET, httpEntity, String.class);
// return response.getBody();
// }
private WebClient client;
@GetMapping("/teachers")
public String listThy(Model model, @ModelAttribute("teachers") @Validated List<Teacher> teachers) throws JsonMappingException, JsonProcessingException{
String json = client.method(HttpMethod.GET)
public String teacherList(Model model) throws JsonMappingException, JsonProcessingException{
// ModelAndView mav = new ModelAndView("teachers");
List<Teacher> teachers = client.method(HttpMethod.GET)
.uri("/teacher/list")
.retrieve()
.bodyToMono(String.class)
.bodyToMono(new ParameterizedTypeReference <List<Teacher>>(){})
.block();
System.out.println("############################################################################");
System.out.println("############################################################################");
System.out.println("json :" + json);
ObjectMapper objectMapper = new ObjectMapper();
teachers = objectMapper.readValue(json, new TypeReference<List<Teacher>>() {});
model.addAttribute("teachers", teachers);
model.addAttribute("teacher_list", teachers);
model.addAttribute("t", new Teacher());
// mav.addObject("teacher_list", teachers);
// mav.addObject("t", new Teacher());
// mav.addObject("teacher", new Teacher());
System.out.println("TEACHERS:" + teachers.toString());
return "teachers";
// return mav;
}
/**
* сохранение записи о новом преподавателе:
* - подготовка сущности записи
* - преобразование записи в json
* - отправка post запроса на сервис-ресурсов
*
* @param entity
* Вывод сведений о преподавателе
* @param model
* @param id
* @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<String> response =
// // restTemplate.postForEntity(url, teacher, Teacher.class);
// restTemplate.postForEntity(url, json, String.class);
// return "teachers";
// }
@GetMapping("/teacher-edit/{id}")
public String teacherById(Model model, @PathVariable Long id) {
Teacher t = client.get()
.uri("/teacher/" + id)
.retrieve()
.bodyToMono(Teacher.class)
.timeout(Duration.ofSeconds(1))
.block();
model.addAttribute("teacher", t);
// System.out.println("TEACHERS:" + t.toString());
return "teacher-edit";
}
/**
* Добавление сведений о преподавателе:
@ -119,6 +83,8 @@ public class TeachersController { @@ -119,6 +83,8 @@ public class TeachersController {
* - направление пользователя на основную страницу
* @param teacher
* @return
* @throws JsonProcessingException
* @throws JsonMappingException
*/
@PostMapping(
path = "/teacher/add",
@ -126,7 +92,7 @@ public class TeachersController { @@ -126,7 +92,7 @@ public class TeachersController {
produces = {
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()
.uri("/teacher/create")
.body(Mono.just(teacher), Teacher.class)
@ -135,34 +101,48 @@ public class TeachersController { @@ -135,34 +101,48 @@ public class TeachersController {
.timeout(Duration.ofSeconds(1))
.block();
return "redirect:/teachers";
// return teacherList();
}
@GetMapping(
path = "/teacher/add"
)
public String addTeacherFrame(Model model){
Teacher teacher = new Teacher();
model.addAttribute("newTeacher", teacher);
return "teacher-add";
}
// @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){
/**
* Удаление записи о преподавателе
* @param id
* @return
* @throws JsonProcessingException
* @throws JsonMappingException
*/
@GetMapping(path = "/teacher/delete/{id}")
public String deleteTeacher(@PathVariable Long id) throws JsonMappingException, JsonProcessingException{
client.delete()
.uri("/teacher/delete/" + id)
.retrieve()
// .toBodilessEntity()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(1))
// .onErrorResume(e -> Mono.empty())
.block();
return "redirect:/teachers";
// return teacherList();
}
/**
* Обновление сведений о преподавателе
* - переделать post на put запрос
*
* @param id
* @param teacher
* @return
* @throws JsonProcessingException
* @throws JsonMappingException
*/
@PostMapping(
path = "/teacher/update/{id}",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
@ -170,7 +150,7 @@ public class TeachersController { @@ -170,7 +150,7 @@ public class TeachersController {
MediaType.APPLICATION_JSON_VALUE
})
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(">>>>>>>>>>>>>>" + teacher.toString());
@ -182,5 +162,6 @@ public class TeachersController { @@ -182,5 +162,6 @@ public class TeachersController {
.timeout(Duration.ofSeconds(1))
.block();
return "redirect:/teachers";
// return teacherList();
}
}

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

@ -0,0 +1,49 @@ @@ -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 @@ @@ -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>

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

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

2
postgres-service/dockerfile

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

Loading…
Cancel
Save