Сущность cars

This commit is contained in:
DyCTaTOR 2024-05-13 00:35:35 +04:00
parent cb8c9949a6
commit aa2d317abb
10 changed files with 123 additions and 61 deletions

View File

@ -6,6 +6,7 @@ import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
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.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.CarsEntity;
import com.example.autoservice.cars.model.CarsGrouped; import com.example.autoservice.cars.model.CarsGrouped;
import com.example.autoservice.cars.service.CarsService; 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 com.example.autoservice.core.configuration.Constants;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -26,9 +29,10 @@ import jakarta.validation.Valid;
@RequestMapping(CarsController.URL) @RequestMapping(CarsController.URL)
public class CarsController { public class CarsController {
public static final String URL = "/cars"; public static final String URL = "/cars";
private static final String CLIENTS_VIEW = "cars"; private static final String CARS_VIEW = "cars";
private static final String CLIENTS_ATTRIBUTE = "cars"; private static final String CARS_ATTRIBUTE = "cars";
private static final String CLIENTS_EDIT_VIEW = "car-edit"; private static final String CLIENTS_ATTRIBUTE = "clients";
private static final String CARS_EDIT_VIEW = "car-edit";
private final CarsService carsService; private final CarsService carsService;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
@ -49,29 +53,38 @@ public class CarsController {
return modelMapper.map(dto, CarsEntity.class); 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 @GetMapping
public String getAll(Model model){ public String getAll(Model model){
List<CarsGroupedDto> cars = carsService.getAllCarsWithClients().stream().map(this::toGroupedDto).toList(); List<CarsGroupedDto> cars = carsService.getAllCarsWithClients().stream().map(this::toGroupedDto).toList();
model.addAttribute("cars", model.addAttribute("cars",
cars); cars);
return CLIENTS_VIEW; return CARS_VIEW;
} }
@GetMapping("/edit/") @GetMapping("/edit/")
public String create(Model model){ public String create(Model model){
model.addAttribute(CLIENTS_ATTRIBUTE, new CarsDto()); model.addAttribute(CARS_ATTRIBUTE, new CarsGroupedDto());
return CLIENTS_EDIT_VIEW; model.addAttribute(CLIENTS_ATTRIBUTE, getClients());
return CARS_EDIT_VIEW;
} }
@PostMapping("/edit/") @PostMapping("/edit/")
public String create( public String create(
@ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid CarsDto client, @ModelAttribute(name = CARS_ATTRIBUTE) @Valid CarsGroupedDto car,
BindingResult bindingResult, BindingResult bindingResult,
Model model) { Model model) {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
return CLIENTS_EDIT_VIEW; return CARS_EDIT_VIEW;
} }
carsService.create(toEntity(client)); carsService.create(toEntity(car));
return Constants.REDIRECT_VIEW + URL; return Constants.REDIRECT_VIEW + URL;
} }
@ -82,23 +95,24 @@ public class CarsController {
if (id <= 0) { if (id <= 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
model.addAttribute(CLIENTS_ATTRIBUTE, toDto(carsService.get(id))); model.addAttribute(CARS_ATTRIBUTE, toGroupedDto(carsService.getCarWithClient(id)));
return CLIENTS_EDIT_VIEW; model.addAttribute(CLIENTS_ATTRIBUTE, getClients());
return CARS_EDIT_VIEW;
} }
@PostMapping("/edit/{id}") @PostMapping("/edit/{id}")
public String update( public String update(
@PathVariable(name = "id") Long id, @PathVariable(name = "id") Long id,
@ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid CarsDto client, @ModelAttribute(name = CARS_ATTRIBUTE) @Valid CarsGroupedDto car,
BindingResult bindingResult, BindingResult bindingResult,
Model model) { Model model) {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
return CLIENTS_EDIT_VIEW; return CARS_EDIT_VIEW;
} }
if (id <= 0) { if (id <= 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
carsService.update(id, toEntity(client)); carsService.update(id, toEntity(car));
return Constants.REDIRECT_VIEW + URL; return Constants.REDIRECT_VIEW + URL;
} }

View File

@ -1,49 +1,67 @@
package com.example.autoservice.cars.api; package com.example.autoservice.cars.api;
public class CarsGroupedDto { public class CarsGroupedDto {
private String Mark; private Long id;
private String Model; private Long clientId;
private String SerialNumber; private String mark;
private String FirstName; private String model;
private String LastName; 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(){ public String getMark(){
return Mark; return mark;
} }
public void setMark(String mark){ public void setMark(String mark){
this.Mark = mark; this.mark = mark;
} }
public String getModel(){ public String getModel(){
return Model; return model;
} }
public void setModel(String model){ public void setModel(String model){
this.Model = model; this.model = model;
} }
public String getSerial_Number(){ public String getSerial_number(){
return SerialNumber; return serial_number;
} }
public void setSerial_Number(String serial_number){ public void setSerial_number(String serial_number){
this.SerialNumber = serial_number; this.serial_number = serial_number;
} }
public String getFirst_Name(){ public String getFirst_name(){
return FirstName; return first_name;
} }
public void setFirst_Name(String first_name){ public void setFirst_name(String first_name){
this.FirstName = first_name; this.first_name = first_name;
} }
public String getLast_Name(){ public String getLast_name(){
return LastName; return last_name;
} }
public void setLast_Name(String last_name){ public void setLast_name(String last_name){
this.LastName = last_name; this.last_name = last_name;
} }
} }

View File

@ -1,9 +1,11 @@
package com.example.autoservice.cars.model; package com.example.autoservice.cars.model;
public interface CarsGrouped { public interface CarsGrouped {
Long getId();
Long getClientId();
String getMark(); String getMark();
String getModel(); String getModel();
String getSerialNumber(); String getSerial_number();
String getFirstName(); String getFirst_name();
String getLastName(); String getLast_name();
} }

View File

@ -7,11 +7,21 @@ import org.springframework.data.repository.CrudRepository;
import com.example.autoservice.cars.model.CarsEntity; import com.example.autoservice.cars.model.CarsEntity;
import com.example.autoservice.cars.model.CarsGrouped; import com.example.autoservice.cars.model.CarsGrouped;
import com.example.autoservice.clients.model.ClientsEntity;
public interface CarsRepository extends CrudRepository<CarsEntity, Long> { public interface CarsRepository extends CrudRepository<CarsEntity, Long> {
@Query("select c.mark as mark, c.model as model, c.serial_number as serialNumber, " + @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 firstName, cl.last_name as lastName " + "cl.first_name as first_name, cl.last_name as last_name " +
"From CarsEntity c " + "From CarsEntity c " +
"join ClientsEntity cl on c.client.id = cl.id") "join ClientsEntity cl on c.client.id = cl.id")
public List<CarsGrouped> findAllCarsWithClients(); 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();
} }

View File

@ -9,6 +9,7 @@ import org.springframework.transaction.annotation.Transactional;
import com.example.autoservice.cars.model.CarsEntity; import com.example.autoservice.cars.model.CarsEntity;
import com.example.autoservice.cars.model.CarsGrouped; import com.example.autoservice.cars.model.CarsGrouped;
import com.example.autoservice.cars.repository.CarsRepository; import com.example.autoservice.cars.repository.CarsRepository;
import com.example.autoservice.clients.model.ClientsEntity;
import com.example.autoservice.core.error.NotFoundException; import com.example.autoservice.core.error.NotFoundException;
@Service @Service
@ -59,4 +60,14 @@ public class CarsService {
public List<CarsGrouped> getAllCarsWithClients(){ public List<CarsGrouped> getAllCarsWithClients(){
return repository.findAllCarsWithClients(); return repository.findAllCarsWithClients();
} }
@Transactional
public CarsGrouped getCarWithClient(Long carId){
return repository.findCarWithClient(carId);
}
@Transactional
public List<ClientsEntity> getAllClients(){
return repository.findAllClients();
}
} }

View File

@ -20,7 +20,6 @@ public class ClientsEntity extends BaseEntity{
private String phone_number; private String phone_number;
public ClientsEntity(){ public ClientsEntity(){
} }
public ClientsEntity(String first_name, String last_name, String middle_name, String date_birthday, String phone_number){ public ClientsEntity(String first_name, String last_name, String middle_name, String date_birthday, String phone_number){

View File

@ -2,7 +2,7 @@
<html lang="ru" xmlns:th="http://www.thymeleaf.org" <html lang="ru" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head> <head>
<title>Редакторовать автомобиль</title> <title>Редактировать автомобиль</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" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> <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> <input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
</div> </div>
<div class="mb-3"> <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"> <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 th:if="${#fields.hasErrors('mark')}" th:errors="*{mark}" class="invalid-feedback"></div>
</div> </div>
<div class="mb-3"> <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"> <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 th:if="${#fields.hasErrors('model')}" th:errors="*{model}" class="invalid-feedback"></div>
</div> </div>
<div class="mb-3"> <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"> <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 th:if="${#fields.hasErrors('serial_number')}" th:errors="*{serial_number}" class="invalid-feedback"></div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="clientId" class="form-label">Дата рождения</label> <label for="clientId" class="form-label">clientId</label>
<input type="date" th:field="*{clientId}" id="clientId" class="form-control"> <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 th:if="${#fields.hasErrors('clientId')}" th:errors="*{clientId}" class="invalid-feedback"></div>
</div>
<div class="mb-3 d-flex flex-row"> <div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button> <button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
<a class="btn btn-secondary button-fixed-width" href="/cars">Отмена</a> <a class="btn btn-secondary button-fixed-width" href="/cars">Отмена</a>

View File

@ -2,7 +2,7 @@
<html lang="ru" <html lang="ru"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}"> xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head> <head>
<title>Cars</title> <title>Автомобили</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>
@ -14,12 +14,12 @@
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
<th scope = "col" class="w-10">ID</th> <th scope = "col" class="w-25">ID</th>
<th scope = "col" class="w-10">Mark</th> <th scope = "col" class="w-25">Марка автомобиля</th>
<th scope = "col" class="w-10">Model</th> <th scope = "col" class="w-10">Модель</th>
<th scope = "col" class="w-10">Serial Number</th> <th scope = "col" class="w-10">Серийный номер</th>
<th scope = "col" class="w-10">First Name</th> <th scope = "col" class="w-10">Имя владельца</th>
<th scope = "col" class="w-10">Last Name</th> <th scope = "col" class="w-10">Фамилия владельца</th>
<th scope = "col"></th> <th scope = "col"></th>
<th scope = "col"></th> <th scope = "col"></th>
</tr> </tr>

View File

@ -2,7 +2,7 @@
<html lang="ru" xmlns:th="http://www.thymeleaf.org" <html lang="ru" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head> <head>
<title>Редакторовать тип заказа</title> <title>Редактировать клиента</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" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" /> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />

View File

@ -2,7 +2,7 @@
<html lang="ru" <html lang="ru"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}"> xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head> <head>
<title>Clients</title> <title>Клиенты</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>
@ -15,11 +15,11 @@
<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">Имя</th>
<th scope = "col" class="w-10">Last Name</th> <th scope = "col" class="w-10">Фамилия</th>
<th scope = "col" class="w-10">Middle Name</th> <th scope = "col" class="w-10">Отчество</th>
<th scope = "col" class="w-10">Date of Birthday</th> <th scope = "col" class="w-10">Дата рождения</th>
<th scope = "col" class="w-10">Phone Number</th> <th scope = "col" class="w-10">Номер телефона</th>
<th scope = "col"></th> <th scope = "col"></th>
<th scope = "col"></th> <th scope = "col"></th>
</tr> </tr>