Почти завершенные круды clients
This commit is contained in:
parent
afd48943a0
commit
8ba82c3ff6
@ -5,8 +5,10 @@ import java.util.List;
|
|||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
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.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
@ -14,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
import com.example.autoservice.clients.service.ClientsService;
|
import com.example.autoservice.clients.service.ClientsService;
|
||||||
|
import com.example.autoservice.core.configuration.Constants;
|
||||||
import com.example.autoservice.clients.model.ClientsEntity;
|
import com.example.autoservice.clients.model.ClientsEntity;
|
||||||
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
@ -23,6 +26,8 @@ import jakarta.validation.Valid;
|
|||||||
public class ClientsController {
|
public class ClientsController {
|
||||||
public static final String URL = "/clients";
|
public static final String URL = "/clients";
|
||||||
private static final String CLIENTS_VIEW = "clients";
|
private static final String CLIENTS_VIEW = "clients";
|
||||||
|
private static final String CLIENTS_ATTRIBUTE = "clients";
|
||||||
|
private static final String CLIENTS_EDIT_VIEW = "client-edit";
|
||||||
private final ClientsService clientsService;
|
private final ClientsService clientsService;
|
||||||
private final ModelMapper modelMapper;
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
@ -38,7 +43,7 @@ public class ClientsController {
|
|||||||
private ClientsEntity toEntity(ClientsDto dto) {
|
private ClientsEntity toEntity(ClientsDto dto) {
|
||||||
return modelMapper.map(dto, ClientsEntity.class);
|
return modelMapper.map(dto, ClientsEntity.class);
|
||||||
}
|
}
|
||||||
@GetMapping()
|
@GetMapping
|
||||||
public String getAll(Model model){
|
public String getAll(Model model){
|
||||||
List<ClientsDto> lines = clientsService.getAll().stream()
|
List<ClientsDto> lines = clientsService.getAll().stream()
|
||||||
.map(this::toDto)
|
.map(this::toDto)
|
||||||
@ -48,6 +53,51 @@ public class ClientsController {
|
|||||||
return CLIENTS_VIEW;
|
return CLIENTS_VIEW;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/")
|
||||||
|
public String create(Model model){
|
||||||
|
model.addAttribute(CLIENTS_ATTRIBUTE, new ClientsDto());
|
||||||
|
return CLIENTS_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid ClientsDto client,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
return CLIENTS_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
clientsService.create(toEntity(client));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
Model model) {
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
model.addAttribute(CLIENTS_ATTRIBUTE, toDto(clientsService.get(id)));
|
||||||
|
return CLIENTS_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid ClientsDto client,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
return CLIENTS_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
clientsService.update(id, toEntity(client));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
// @GetMapping
|
// @GetMapping
|
||||||
// public List<ClientsDto> getAll() {
|
// public List<ClientsDto> getAll() {
|
||||||
// return clientsService.getAll().stream().map(this::toDto).toList();
|
// return clientsService.getAll().stream().map(this::toDto).toList();
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.example.autoservice.clients.api;
|
package com.example.autoservice.clients.api;
|
||||||
|
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
@ -14,7 +15,7 @@ public class ClientsDto{
|
|||||||
@NotNull
|
@NotNull
|
||||||
private String last_name;
|
private String last_name;
|
||||||
private String middle_name;
|
private String middle_name;
|
||||||
private Date date_birthday;
|
private String date_birthday;
|
||||||
@NotNull
|
@NotNull
|
||||||
private String phone_number;
|
private String phone_number;
|
||||||
|
|
||||||
@ -50,12 +51,13 @@ public class ClientsDto{
|
|||||||
this.middle_name = middlename;
|
this.middle_name = middlename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date getDate_birthday(){
|
public String getDate_birthday(){
|
||||||
return date_birthday;
|
return date_birthday;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDate_birthday(Date datebirthday){
|
public void setDate_birthday(Date datebirthday){
|
||||||
this.date_birthday = datebirthday;
|
this.date_birthday = datebirthday.toInstant().
|
||||||
|
atZone(ZoneId.systemDefault()).toLocalDate().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPhone_number(){
|
public String getPhone_number(){
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package com.example.autoservice.clients.model;
|
package com.example.autoservice.clients.model;
|
||||||
|
|
||||||
import com.example.autoservice.core.model.BaseEntity;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
|
import com.example.autoservice.core.model.BaseEntity;
|
||||||
import jakarta.persistence.Column;
|
import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.Table;
|
||||||
@ -13,7 +14,7 @@ public class ClientsEntity extends BaseEntity{
|
|||||||
private String first_name;
|
private String first_name;
|
||||||
@Column(nullable = false, unique = false, length = 25)
|
@Column(nullable = false, unique = false, length = 25)
|
||||||
private String last_name;
|
private String last_name;
|
||||||
@Column(nullable = false, unique = false, length = 10)
|
@Column(nullable = true, unique = false, length = 10)
|
||||||
private String middle_name;
|
private String middle_name;
|
||||||
@Column(nullable = true, unique = false)
|
@Column(nullable = true, unique = false)
|
||||||
private Date date_birthday;
|
private Date date_birthday;
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package com.example.autoservice.clients.repository;
|
package com.example.autoservice.clients.repository;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
import com.example.autoservice.clients.model.ClientsEntity;
|
import com.example.autoservice.clients.model.ClientsEntity;
|
||||||
|
@ -3,6 +3,8 @@ package com.example.autoservice.core.configuration;
|
|||||||
public class Constants {
|
public class Constants {
|
||||||
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
||||||
|
|
||||||
|
public static final String REDIRECT_VIEW = "redirect:";
|
||||||
|
|
||||||
public static final String API_URL = "/api/1.0";
|
public static final String API_URL = "/api/1.0";
|
||||||
|
|
||||||
private Constants() {
|
private Constants() {
|
||||||
|
52
src/main/resources/templates/client-edit.html
Normal file
52
src/main/resources/templates/client-edit.html
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html lang="ru" xmlns:th="http://www.thymeleaf.org"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||||
|
<head>
|
||||||
|
<title>Редакторовать тип заказа</title>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
|
||||||
|
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
|
||||||
|
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
|
||||||
|
<link rel="stylesheet" href="/css/style.css" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<main>
|
||||||
|
<form action="#" th:action="@{/clients/edit/{id}(id=${clients.id})}" th:object="${clients}" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id" class="form-label">ID</label>
|
||||||
|
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="first_name" class="form-label">Имя</label>
|
||||||
|
<input type="text" th:field="*{first_name}" id="first_name" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('first_name')}" th:errors="*{first_name}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="last_name" class="form-label">Фамилия</label>
|
||||||
|
<input type="text" th:field="*{last_name}" id="last_name" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('last_name')}" th:errors="*{last_name}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="middle_name" class="form-label">Отчество</label>
|
||||||
|
<input type="text" th:field="*{middle_name}" id="middle_name" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('middle_name')}" th:errors="*{middle_name}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="date_birthday" class="form-label">Дата рождения</label>
|
||||||
|
<input type="date" th:field="*{date_birthday}" id="date_birthday" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('date_birthday')}" th:errors="*{date_birthday}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="phone_number" class="form-label">Номер телефона</label>
|
||||||
|
<input type="text" th:field="*{phone_number}" id="phone_number" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('phone_number')}" th:errors="*{phone_number}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 d-flex flex-row">
|
||||||
|
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
|
||||||
|
<a class="btn btn-secondary button-fixed-width" href="/clients">Отмена</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,33 +1,48 @@
|
|||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
<html xmlns:th="http://www.thymeleaf.org"
|
<html lang="ru"
|
||||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
|
|
||||||
<head>
|
<head>
|
||||||
<title>Getting Started: Serving Web Content</title>
|
<title>Clients</title>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main class="container-fluid p-2">
|
<main layout:fragment="content">
|
||||||
<table>
|
<form th:action="@{/clients/edit/}" method="get" class="mb-3">
|
||||||
|
<button type="submit" class="btn btn-primary">Добавить клиента</button>
|
||||||
|
</form>
|
||||||
|
<table class="table">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th scope = "col" class="w-10">ID</th>
|
<th scope = "col" class="w-10">ID</th>
|
||||||
<th scope = "col" class="w-10">First Name</th>
|
<th scope = "col" class="w-10">First Name</th>
|
||||||
<th scope = "col" class="w-10">Last Name</th>
|
<th scope = "col" class="w-10">Last Name</th>
|
||||||
<th scope = "col" class="w-10">Middle Name</th>
|
<th scope = "col" class="w-10">Middle Name</th>
|
||||||
<th scope = "col" class="w-10">Date of Birth</th>
|
<th scope = "col" class="w-10">Date of Birthday</th>
|
||||||
<th scope = "col" class="w-10">Phone Number</th>
|
<th scope = "col" class="w-10">Phone Number</th>
|
||||||
|
<th scope = "col"></th>
|
||||||
|
<th scope = "col"></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr th:each="c : ${clients}">
|
<tr th:each="client : ${clients}">
|
||||||
<th scope="row" th:text="${c.id}"></th>
|
<th scope="row" th:text="${client.id}"></th>
|
||||||
<td th:text="${c.first_name}"></td>
|
<td th:text="${client.first_name}"></td>
|
||||||
<td th:text="${c.last_name}"></td>
|
<td th:text="${client.last_name}"></td>
|
||||||
<td th:text="${c.middle_name}"></td>
|
<td th:text="${client.middle_name}"></td>
|
||||||
<td th:text="${c.date_birthday}"></td>
|
<td th:text="${client.date_birthday}"></td>
|
||||||
<td th:text="${c.phone_number}"></td>
|
<td th:text="${client.phone_number}"></td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/clients/edit/{id}(id=${client.id})}" method = "get">
|
||||||
|
<button type="submit" class="btn btn-link button-link">Редактировать</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/clients/delete/{id}(id=${client.id})}" method="post">
|
||||||
|
<button type="submit" class="btn btn-link button-link"
|
||||||
|
onclick="return confirm('Вы уверены?')">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
40
src/main/resources/templates/default.html
Normal file
40
src/main/resources/templates/default.html
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" data-bs-theme="dark" xmlns:th="http://www.thymeleaf.org"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">autoservice</title>
|
||||||
|
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
|
||||||
|
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
|
||||||
|
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
|
||||||
|
<link rel="stylesheet" href="/css/style.css"/>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body class="h-100 d-flex flex-column">
|
||||||
|
<nav class="navbar navbar-expand-md my-navbar" data-bs-theme="dark">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<a class="navbar-brand" href="/">
|
||||||
|
<i class="bi bi-cart2 d-inline-block align-top me-1 logo"></i>
|
||||||
|
autoservice
|
||||||
|
</a>
|
||||||
|
<div class="collapse navbar-collapse" id="main-navbar">
|
||||||
|
<ul class="navbar-nav me-auto link" th:with="activeLink=${#objects.nullSafe(servletPath, '')}">
|
||||||
|
<a class="nav-link" href="/clients">
|
||||||
|
Клиенты
|
||||||
|
</a>
|
||||||
|
<a class="nav-link" href="/h2-console/" target="_blank">Консоль H2</a>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
<main class="container-fluid p-2" layout:fragment="content">
|
||||||
|
</main>
|
||||||
|
<footer class="my-footer mt-auto d-flex flex-shrink-0 justify-content-center align-items-center">
|
||||||
|
Автор, [[${#dates.year(#dates.createNow())}]]
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user