Сущность cars
This commit is contained in:
parent
cb8c9949a6
commit
aa2d317abb
@ -6,6 +6,7 @@ import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.modelmapper.TypeToken;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
@ -18,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import com.example.autoservice.cars.model.CarsEntity;
|
||||
import com.example.autoservice.cars.model.CarsGrouped;
|
||||
import com.example.autoservice.cars.service.CarsService;
|
||||
import com.example.autoservice.clients.api.ClientsDto;
|
||||
import com.example.autoservice.clients.model.ClientsEntity;
|
||||
import com.example.autoservice.core.configuration.Constants;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
@ -26,9 +29,10 @@ import jakarta.validation.Valid;
|
||||
@RequestMapping(CarsController.URL)
|
||||
public class CarsController {
|
||||
public static final String URL = "/cars";
|
||||
private static final String CLIENTS_VIEW = "cars";
|
||||
private static final String CLIENTS_ATTRIBUTE = "cars";
|
||||
private static final String CLIENTS_EDIT_VIEW = "car-edit";
|
||||
private static final String CARS_VIEW = "cars";
|
||||
private static final String CARS_ATTRIBUTE = "cars";
|
||||
private static final String CLIENTS_ATTRIBUTE = "clients";
|
||||
private static final String CARS_EDIT_VIEW = "car-edit";
|
||||
private final CarsService carsService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
@ -49,29 +53,38 @@ public class CarsController {
|
||||
return modelMapper.map(dto, CarsEntity.class);
|
||||
}
|
||||
|
||||
private CarsEntity toEntity(CarsGroupedDto dto){
|
||||
return modelMapper.map(dto, CarsEntity.class);
|
||||
}
|
||||
|
||||
private List<ClientsDto> getClients(){
|
||||
return modelMapper.map(carsService.getAllClients(), new TypeToken<List<ClientsDto>>(){}.getType());
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getAll(Model model){
|
||||
List<CarsGroupedDto> cars = carsService.getAllCarsWithClients().stream().map(this::toGroupedDto).toList();
|
||||
model.addAttribute("cars",
|
||||
cars);
|
||||
return CLIENTS_VIEW;
|
||||
return CARS_VIEW;
|
||||
}
|
||||
|
||||
@GetMapping("/edit/")
|
||||
public String create(Model model){
|
||||
model.addAttribute(CLIENTS_ATTRIBUTE, new CarsDto());
|
||||
return CLIENTS_EDIT_VIEW;
|
||||
model.addAttribute(CARS_ATTRIBUTE, new CarsGroupedDto());
|
||||
model.addAttribute(CLIENTS_ATTRIBUTE, getClients());
|
||||
return CARS_EDIT_VIEW;
|
||||
}
|
||||
|
||||
@PostMapping("/edit/")
|
||||
public String create(
|
||||
@ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid CarsDto client,
|
||||
@ModelAttribute(name = CARS_ATTRIBUTE) @Valid CarsGroupedDto car,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return CLIENTS_EDIT_VIEW;
|
||||
return CARS_EDIT_VIEW;
|
||||
}
|
||||
carsService.create(toEntity(client));
|
||||
carsService.create(toEntity(car));
|
||||
return Constants.REDIRECT_VIEW + URL;
|
||||
}
|
||||
|
||||
@ -82,23 +95,24 @@ public class CarsController {
|
||||
if (id <= 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
model.addAttribute(CLIENTS_ATTRIBUTE, toDto(carsService.get(id)));
|
||||
return CLIENTS_EDIT_VIEW;
|
||||
model.addAttribute(CARS_ATTRIBUTE, toGroupedDto(carsService.getCarWithClient(id)));
|
||||
model.addAttribute(CLIENTS_ATTRIBUTE, getClients());
|
||||
return CARS_EDIT_VIEW;
|
||||
}
|
||||
|
||||
@PostMapping("/edit/{id}")
|
||||
public String update(
|
||||
@PathVariable(name = "id") Long id,
|
||||
@ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid CarsDto client,
|
||||
@ModelAttribute(name = CARS_ATTRIBUTE) @Valid CarsGroupedDto car,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return CLIENTS_EDIT_VIEW;
|
||||
return CARS_EDIT_VIEW;
|
||||
}
|
||||
if (id <= 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
carsService.update(id, toEntity(client));
|
||||
carsService.update(id, toEntity(car));
|
||||
return Constants.REDIRECT_VIEW + URL;
|
||||
}
|
||||
|
||||
|
@ -1,49 +1,67 @@
|
||||
package com.example.autoservice.cars.api;
|
||||
|
||||
public class CarsGroupedDto {
|
||||
private String Mark;
|
||||
private String Model;
|
||||
private String SerialNumber;
|
||||
private String FirstName;
|
||||
private String LastName;
|
||||
private Long id;
|
||||
private Long clientId;
|
||||
private String mark;
|
||||
private String model;
|
||||
private String serial_number;
|
||||
private String first_name;
|
||||
private String last_name;
|
||||
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getClientId(){
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setClientId(Long clientId){
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public String getMark(){
|
||||
return Mark;
|
||||
return mark;
|
||||
}
|
||||
|
||||
public void setMark(String mark){
|
||||
this.Mark = mark;
|
||||
this.mark = mark;
|
||||
}
|
||||
|
||||
public String getModel(){
|
||||
return Model;
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model){
|
||||
this.Model = model;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getSerial_Number(){
|
||||
return SerialNumber;
|
||||
public String getSerial_number(){
|
||||
return serial_number;
|
||||
}
|
||||
|
||||
public void setSerial_Number(String serial_number){
|
||||
this.SerialNumber = serial_number;
|
||||
public void setSerial_number(String serial_number){
|
||||
this.serial_number = serial_number;
|
||||
}
|
||||
|
||||
public String getFirst_Name(){
|
||||
return FirstName;
|
||||
public String getFirst_name(){
|
||||
return first_name;
|
||||
}
|
||||
|
||||
public void setFirst_Name(String first_name){
|
||||
this.FirstName = first_name;
|
||||
public void setFirst_name(String first_name){
|
||||
this.first_name = first_name;
|
||||
}
|
||||
|
||||
public String getLast_Name(){
|
||||
return LastName;
|
||||
public String getLast_name(){
|
||||
return last_name;
|
||||
}
|
||||
|
||||
public void setLast_Name(String last_name){
|
||||
this.LastName = last_name;
|
||||
public void setLast_name(String last_name){
|
||||
this.last_name = last_name;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package com.example.autoservice.cars.model;
|
||||
|
||||
public interface CarsGrouped {
|
||||
Long getId();
|
||||
Long getClientId();
|
||||
String getMark();
|
||||
String getModel();
|
||||
String getSerialNumber();
|
||||
String getFirstName();
|
||||
String getLastName();
|
||||
String getSerial_number();
|
||||
String getFirst_name();
|
||||
String getLast_name();
|
||||
}
|
||||
|
@ -7,11 +7,21 @@ import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.example.autoservice.cars.model.CarsEntity;
|
||||
import com.example.autoservice.cars.model.CarsGrouped;
|
||||
import com.example.autoservice.clients.model.ClientsEntity;
|
||||
|
||||
public interface CarsRepository extends CrudRepository<CarsEntity, Long> {
|
||||
@Query("select c.mark as mark, c.model as model, c.serial_number as serialNumber, " +
|
||||
"cl.first_name as firstName, cl.last_name as lastName " +
|
||||
@Query("select c.id as id, cl.id as clientId, c.mark as mark, c.model as model, c.serial_number as serial_number, " +
|
||||
"cl.first_name as first_name, cl.last_name as last_name " +
|
||||
"From CarsEntity c " +
|
||||
"join ClientsEntity cl on c.client.id = cl.id")
|
||||
public List<CarsGrouped> findAllCarsWithClients();
|
||||
@Query("select c.id as id, cl.id as clientId, c.mark as mark, c.model as model, c.serial_number as serial_number, " +
|
||||
"cl.first_name as first_name, cl.last_name as last_name " +
|
||||
"From CarsEntity c " +
|
||||
"join ClientsEntity cl on c.client.id = cl.id " +
|
||||
"where c.id = :carId")
|
||||
public CarsGrouped findCarWithClient(Long carId);
|
||||
@Query("select cl as client " +
|
||||
"from ClientsEntity cl")
|
||||
public List<ClientsEntity> findAllClients();
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import com.example.autoservice.cars.model.CarsEntity;
|
||||
import com.example.autoservice.cars.model.CarsGrouped;
|
||||
import com.example.autoservice.cars.repository.CarsRepository;
|
||||
import com.example.autoservice.clients.model.ClientsEntity;
|
||||
import com.example.autoservice.core.error.NotFoundException;
|
||||
|
||||
@Service
|
||||
@ -59,4 +60,14 @@ public class CarsService {
|
||||
public List<CarsGrouped> getAllCarsWithClients(){
|
||||
return repository.findAllCarsWithClients();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public CarsGrouped getCarWithClient(Long carId){
|
||||
return repository.findCarWithClient(carId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<ClientsEntity> getAllClients(){
|
||||
return repository.findAllClients();
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ public class ClientsEntity extends BaseEntity{
|
||||
private String phone_number;
|
||||
|
||||
public ClientsEntity(){
|
||||
|
||||
}
|
||||
|
||||
public ClientsEntity(String first_name, String last_name, String middle_name, String date_birthday, String phone_number){
|
||||
|
@ -2,7 +2,7 @@
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<title>Редакторовать автомобиль</title>
|
||||
<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" />
|
||||
@ -18,24 +18,32 @@
|
||||
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="mark" class="form-label">Имя</label>
|
||||
<label for="mark" class="form-label">Марка</label>
|
||||
<input type="text" th:field="*{mark}" id="mark" class="form-control">
|
||||
<div th:if="${#fields.hasErrors('mark')}" th:errors="*{mark}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="model" class="form-label">Фамилия</label>
|
||||
<label for="model" class="form-label">Модель</label>
|
||||
<input type="text" th:field="*{model}" id="model" class="form-control">
|
||||
<div th:if="${#fields.hasErrors('model')}" th:errors="*{model}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="serial_number" class="form-label">Отчество</label>
|
||||
<label for="serial_number" class="form-label">Серийный номер</label>
|
||||
<input type="text" th:field="*{serial_number}" id="serial_number" class="form-control">
|
||||
<div th:if="${#fields.hasErrors('serial_number')}" th:errors="*{serial_number}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="clientId" class="form-label">Дата рождения</label>
|
||||
<input type="date" th:field="*{clientId}" id="clientId" class="form-control">
|
||||
<label for="clientId" class="form-label">clientId</label>
|
||||
<select th:field="*{clientId}" id="clientId" class="form-control">
|
||||
<option th:if="*{clientId} == null" value="">Выберите клиента</option>
|
||||
<option th:unless="*{clientId} == null" th:value="*{clientId}"
|
||||
th:text="*{first_name} + ' ' + *{last_name}"></option>
|
||||
<option th:each="client : ${clients}" th:value="${client.id}" th:text="${client.first_name} + ' ' + ${client.last_name}"
|
||||
th:selected="*{clientId} == ${client.id}">
|
||||
</option>
|
||||
</select>
|
||||
<div th:if="${#fields.hasErrors('clientId')}" th:errors="*{clientId}" 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="/cars">Отмена</a>
|
||||
|
@ -2,7 +2,7 @@
|
||||
<html lang="ru"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Cars</title>
|
||||
<title>Автомобили</title>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
</head>
|
||||
@ -14,12 +14,12 @@
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope = "col" class="w-10">ID</th>
|
||||
<th scope = "col" class="w-10">Mark</th>
|
||||
<th scope = "col" class="w-10">Model</th>
|
||||
<th scope = "col" class="w-10">Serial Number</th>
|
||||
<th scope = "col" class="w-10">First Name</th>
|
||||
<th scope = "col" class="w-10">Last Name</th>
|
||||
<th scope = "col" class="w-25">ID</th>
|
||||
<th scope = "col" class="w-25">Марка автомобиля</th>
|
||||
<th scope = "col" class="w-10">Модель</th>
|
||||
<th scope = "col" class="w-10">Серийный номер</th>
|
||||
<th scope = "col" class="w-10">Имя владельца</th>
|
||||
<th scope = "col" class="w-10">Фамилия владельца</th>
|
||||
<th scope = "col"></th>
|
||||
<th scope = "col"></th>
|
||||
</tr>
|
||||
|
@ -2,7 +2,7 @@
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<title>Редакторовать тип заказа</title>
|
||||
<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" />
|
||||
|
@ -2,7 +2,7 @@
|
||||
<html lang="ru"
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Clients</title>
|
||||
<title>Клиенты</title>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
</head>
|
||||
@ -15,11 +15,11 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope = "col" class="w-10">ID</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">Middle Name</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">Имя</th>
|
||||
<th scope = "col" class="w-10">Фамилия</th>
|
||||
<th scope = "col" class="w-10">Отчество</th>
|
||||
<th scope = "col" class="w-10">Дата рождения</th>
|
||||
<th scope = "col" class="w-10">Номер телефона</th>
|
||||
<th scope = "col"></th>
|
||||
<th scope = "col"></th>
|
||||
</tr>
|
||||
|
Loading…
Reference in New Issue
Block a user