diff --git a/src/main/java/com/example/autoservice/cars/api/CarsController.java b/src/main/java/com/example/autoservice/cars/api/CarsController.java index 98c1a91..dd3330a 100644 --- a/src/main/java/com/example/autoservice/cars/api/CarsController.java +++ b/src/main/java/com/example/autoservice/cars/api/CarsController.java @@ -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; @@ -48,30 +52,39 @@ public class CarsController { private CarsEntity toEntity(CarsDto dto) { return modelMapper.map(dto, CarsEntity.class); } + + private CarsEntity toEntity(CarsGroupedDto dto){ + return modelMapper.map(dto, CarsEntity.class); + } + + private List getClients(){ + return modelMapper.map(carsService.getAllClients(), new TypeToken>(){}.getType()); + } @GetMapping public String getAll(Model model){ List 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; } diff --git a/src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java b/src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java index 9b14f47..04c257a 100644 --- a/src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java +++ b/src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java @@ -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; } } diff --git a/src/main/java/com/example/autoservice/cars/model/CarsGrouped.java b/src/main/java/com/example/autoservice/cars/model/CarsGrouped.java index b88097a..c2116a0 100644 --- a/src/main/java/com/example/autoservice/cars/model/CarsGrouped.java +++ b/src/main/java/com/example/autoservice/cars/model/CarsGrouped.java @@ -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(); } diff --git a/src/main/java/com/example/autoservice/cars/repository/CarsRepository.java b/src/main/java/com/example/autoservice/cars/repository/CarsRepository.java index 81fc463..3aaaaa8 100644 --- a/src/main/java/com/example/autoservice/cars/repository/CarsRepository.java +++ b/src/main/java/com/example/autoservice/cars/repository/CarsRepository.java @@ -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 { - @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 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 findAllClients(); } diff --git a/src/main/java/com/example/autoservice/cars/service/CarsService.java b/src/main/java/com/example/autoservice/cars/service/CarsService.java index 07551e8..4607a76 100644 --- a/src/main/java/com/example/autoservice/cars/service/CarsService.java +++ b/src/main/java/com/example/autoservice/cars/service/CarsService.java @@ -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 getAllCarsWithClients(){ return repository.findAllCarsWithClients(); } + + @Transactional + public CarsGrouped getCarWithClient(Long carId){ + return repository.findCarWithClient(carId); + } + + @Transactional + public List getAllClients(){ + return repository.findAllClients(); + } } diff --git a/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java b/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java index 14f3c5f..802a260 100644 --- a/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java +++ b/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java @@ -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){ diff --git a/src/main/resources/templates/car-edit.html b/src/main/resources/templates/car-edit.html index fb07b75..c0c8cf3 100644 --- a/src/main/resources/templates/car-edit.html +++ b/src/main/resources/templates/car-edit.html @@ -2,7 +2,7 @@ - Редакторовать автомобиль + Редактировать автомобиль @@ -18,24 +18,32 @@
- +
- +
- +
- - + +
+
Отмена diff --git a/src/main/resources/templates/cars.html b/src/main/resources/templates/cars.html index 375faf0..6aa312e 100644 --- a/src/main/resources/templates/cars.html +++ b/src/main/resources/templates/cars.html @@ -2,7 +2,7 @@ - Cars + Автомобили @@ -14,12 +14,12 @@ - - - - - - + + + + + + diff --git a/src/main/resources/templates/client-edit.html b/src/main/resources/templates/client-edit.html index b5906f9..8b2ff61 100644 --- a/src/main/resources/templates/client-edit.html +++ b/src/main/resources/templates/client-edit.html @@ -2,7 +2,7 @@ - Редакторовать тип заказа + Редактировать клиента diff --git a/src/main/resources/templates/clients.html b/src/main/resources/templates/clients.html index 5f2c637..3f583c1 100644 --- a/src/main/resources/templates/clients.html +++ b/src/main/resources/templates/clients.html @@ -2,7 +2,7 @@ - Clients + Клиенты @@ -15,11 +15,11 @@ - - - - - + + + + +
IDMarkModelSerial NumberFirst NameLast NameIDМарка автомобиляМодельСерийный номерИмя владельцаФамилия владельца
IDFirst NameLast NameMiddle NameDate of BirthdayPhone NumberИмяФамилияОтчествоДата рожденияНомер телефона