From cb8c9949a63557110a4b20c306fbf00ca46b94e0 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Fri, 10 May 2024 20:53:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D1=82?= =?UTF-8?q?=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=8B=20cars?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../autoservice/cars/api/CarsController.java | 111 ++++++++++++++++++ .../example/autoservice/cars/api/CarsDto.java | 53 +++++++++ .../autoservice/cars/api/CarsGroupedDto.java | 49 ++++++++ .../autoservice/cars/model/CarsEntity.java | 64 ++++++++++ .../autoservice/cars/model/CarsGrouped.java | 9 ++ .../cars/repository/CarsRepository.java | 17 +++ .../autoservice/cars/service/CarsService.java | 62 ++++++++++ .../autoservice/clients/api/ClientsDto.java | 2 - src/main/resources/templates/car-edit.html | 46 ++++++++ src/main/resources/templates/cars.html | 51 ++++++++ src/main/resources/templates/default.html | 3 + 11 files changed, 465 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/autoservice/cars/api/CarsController.java create mode 100644 src/main/java/com/example/autoservice/cars/api/CarsDto.java create mode 100644 src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java create mode 100644 src/main/java/com/example/autoservice/cars/model/CarsEntity.java create mode 100644 src/main/java/com/example/autoservice/cars/model/CarsGrouped.java create mode 100644 src/main/java/com/example/autoservice/cars/repository/CarsRepository.java create mode 100644 src/main/java/com/example/autoservice/cars/service/CarsService.java create mode 100644 src/main/resources/templates/car-edit.html create mode 100644 src/main/resources/templates/cars.html diff --git a/src/main/java/com/example/autoservice/cars/api/CarsController.java b/src/main/java/com/example/autoservice/cars/api/CarsController.java new file mode 100644 index 0000000..98c1a91 --- /dev/null +++ b/src/main/java/com/example/autoservice/cars/api/CarsController.java @@ -0,0 +1,111 @@ +package com.example.autoservice.cars.api; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.modelmapper.ModelMapper; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +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.PostMapping; +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.core.configuration.Constants; + +import jakarta.validation.Valid; + +@Controller +@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 final CarsService carsService; + private final ModelMapper modelMapper; + + public CarsController(CarsService carsService, ModelMapper modelMapper) { + this.carsService = carsService; + this.modelMapper = modelMapper; + } + + private CarsDto toDto(CarsEntity entity) { + return modelMapper.map(entity, CarsDto.class); + } + + private CarsGroupedDto toGroupedDto(CarsGrouped entity){ + return modelMapper.map(entity, CarsGroupedDto.class); + } + + private CarsEntity toEntity(CarsDto dto) { + return modelMapper.map(dto, CarsEntity.class); + } + + @GetMapping + public String getAll(Model model){ + List cars = carsService.getAllCarsWithClients().stream().map(this::toGroupedDto).toList(); + model.addAttribute("cars", + cars); + return CLIENTS_VIEW; + } + + @GetMapping("/edit/") + public String create(Model model){ + model.addAttribute(CLIENTS_ATTRIBUTE, new CarsDto()); + return CLIENTS_EDIT_VIEW; + } + + @PostMapping("/edit/") + public String create( + @ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid CarsDto client, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return CLIENTS_EDIT_VIEW; + } + carsService.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(carsService.get(id))); + return CLIENTS_EDIT_VIEW; + } + + @PostMapping("/edit/{id}") + public String update( + @PathVariable(name = "id") Long id, + @ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid CarsDto client, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return CLIENTS_EDIT_VIEW; + } + if (id <= 0) { + throw new IllegalArgumentException(); + } + carsService.update(id, toEntity(client)); + return Constants.REDIRECT_VIEW + URL; + } + + @PostMapping("/delete/{id}") + public String delete( + @PathVariable(name = "id") Long id) { + carsService.delete(id); + return Constants.REDIRECT_VIEW + URL; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/autoservice/cars/api/CarsDto.java b/src/main/java/com/example/autoservice/cars/api/CarsDto.java new file mode 100644 index 0000000..75fa88e --- /dev/null +++ b/src/main/java/com/example/autoservice/cars/api/CarsDto.java @@ -0,0 +1,53 @@ +package com.example.autoservice.cars.api; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; + + +public class CarsDto { + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + private Long id; + @NotNull + private String mark; + @NotNull + private String model; + @NotNull + private String serial_number; + @NotNull + @Min(1) + private Long clientId; + + public String getMark(){ + return mark; + } + + public void setMark(String mark){ + this.mark = mark; + } + + public String getModel(){ + return model; + } + + public void setModel(String model){ + this.model = model; + } + + public String getSerial_Number(){ + return serial_number; + } + + public void setSerial_Number(String serial_number){ + this.serial_number = serial_number; + } + + public Long getClientId(){ + return clientId; + } + + public void setClientId(Long clientId){ + this.clientId = clientId; + } +} diff --git a/src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java b/src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java new file mode 100644 index 0000000..9b14f47 --- /dev/null +++ b/src/main/java/com/example/autoservice/cars/api/CarsGroupedDto.java @@ -0,0 +1,49 @@ +package com.example.autoservice.cars.api; + +public class CarsGroupedDto { + private String Mark; + private String Model; + private String SerialNumber; + private String FirstName; + private String LastName; + + public String getMark(){ + return Mark; + } + + public void setMark(String mark){ + this.Mark = mark; + } + + public String getModel(){ + return Model; + } + + public void setModel(String model){ + this.Model = model; + } + + public String getSerial_Number(){ + return SerialNumber; + } + + public void setSerial_Number(String serial_number){ + this.SerialNumber = serial_number; + } + + public String getFirst_Name(){ + return FirstName; + } + + public void setFirst_Name(String first_name){ + this.FirstName = first_name; + } + + public String getLast_Name(){ + return LastName; + } + + public void setLast_Name(String last_name){ + this.LastName = last_name; + } +} diff --git a/src/main/java/com/example/autoservice/cars/model/CarsEntity.java b/src/main/java/com/example/autoservice/cars/model/CarsEntity.java new file mode 100644 index 0000000..04ab31a --- /dev/null +++ b/src/main/java/com/example/autoservice/cars/model/CarsEntity.java @@ -0,0 +1,64 @@ +package com.example.autoservice.cars.model; + +import com.example.autoservice.clients.model.ClientsEntity; +import com.example.autoservice.core.model.BaseEntity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +@Entity +@Table(name = "cars") +public class CarsEntity extends BaseEntity{ + @Column(nullable = false) + private String mark; + @Column(nullable = false) + private String model; + @Column(nullable = false) + private String serial_number; + @ManyToOne + @JoinColumn(name = "clientsid", nullable = false) + private ClientsEntity client; + + public CarsEntity(){} + + public CarsEntity(String mark, String model, String serial_number, ClientsEntity client){ + this.mark = mark; + this.model = model; + this.serial_number = serial_number; + this.client = client; + } + + public String getMark(){ + return mark; + } + + public void setMark(String mark){ + this.mark = mark; + } + + public String getModel(){ + return model; + } + + public void setModel(String model){ + this.model = model; + } + + public String getSerial_Number(){ + return serial_number; + } + + public void setSerial_Number(String serial_number){ + this.serial_number = serial_number; + } + + public ClientsEntity getClient(){ + return client; + } + + public void setClient(ClientsEntity client){ + this.client = client; + } +} diff --git a/src/main/java/com/example/autoservice/cars/model/CarsGrouped.java b/src/main/java/com/example/autoservice/cars/model/CarsGrouped.java new file mode 100644 index 0000000..b88097a --- /dev/null +++ b/src/main/java/com/example/autoservice/cars/model/CarsGrouped.java @@ -0,0 +1,9 @@ +package com.example.autoservice.cars.model; + +public interface CarsGrouped { + String getMark(); + String getModel(); + String getSerialNumber(); + String getFirstName(); + String getLastName(); +} diff --git a/src/main/java/com/example/autoservice/cars/repository/CarsRepository.java b/src/main/java/com/example/autoservice/cars/repository/CarsRepository.java new file mode 100644 index 0000000..81fc463 --- /dev/null +++ b/src/main/java/com/example/autoservice/cars/repository/CarsRepository.java @@ -0,0 +1,17 @@ +package com.example.autoservice.cars.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; + +import com.example.autoservice.cars.model.CarsEntity; +import com.example.autoservice.cars.model.CarsGrouped; + +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 " + + "From CarsEntity c " + + "join ClientsEntity cl on c.client.id = cl.id") + public List findAllCarsWithClients(); +} diff --git a/src/main/java/com/example/autoservice/cars/service/CarsService.java b/src/main/java/com/example/autoservice/cars/service/CarsService.java new file mode 100644 index 0000000..07551e8 --- /dev/null +++ b/src/main/java/com/example/autoservice/cars/service/CarsService.java @@ -0,0 +1,62 @@ +package com.example.autoservice.cars.service; + +import java.util.List; +import java.util.stream.StreamSupport; + +import org.springframework.stereotype.Service; +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.core.error.NotFoundException; + +@Service +public class CarsService { + private final CarsRepository repository; + + public CarsService(CarsRepository repository) { + this.repository = repository; + } + + @Transactional(readOnly = true) + public List getAll() { + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); + } + + @Transactional(readOnly = true) + public CarsEntity get(Long id) { + return repository.findById(id) + .orElseThrow(() -> new NotFoundException(CarsEntity.class, id)); + } + + @Transactional + public CarsEntity create(CarsEntity entity) { + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + return repository.save(entity); + } + + @Transactional + public CarsEntity update(Long id, CarsEntity entity) { + final CarsEntity existsEntity = get(id); + existsEntity.setMark(entity.getMark()); + existsEntity.setModel(entity.getModel()); + existsEntity.setSerial_Number(entity.getSerial_Number()); + existsEntity.setClient(entity.getClient()); + return repository.save(existsEntity); + } + + @Transactional + public CarsEntity delete(Long id) { + final CarsEntity existsEntity = get(id); + repository.delete(existsEntity); + return existsEntity; + } + + @Transactional + public List getAllCarsWithClients(){ + return repository.findAllCarsWithClients(); + } +} diff --git a/src/main/java/com/example/autoservice/clients/api/ClientsDto.java b/src/main/java/com/example/autoservice/clients/api/ClientsDto.java index 72147e3..57eef98 100644 --- a/src/main/java/com/example/autoservice/clients/api/ClientsDto.java +++ b/src/main/java/com/example/autoservice/clients/api/ClientsDto.java @@ -1,7 +1,5 @@ package com.example.autoservice.clients.api; -import java.util.Date; - import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotNull; diff --git a/src/main/resources/templates/car-edit.html b/src/main/resources/templates/car-edit.html new file mode 100644 index 0000000..fb07b75 --- /dev/null +++ b/src/main/resources/templates/car-edit.html @@ -0,0 +1,46 @@ + + + + Редакторовать автомобиль + + + + + + + + +
+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+ + Отмена +
+ +
+ + diff --git a/src/main/resources/templates/cars.html b/src/main/resources/templates/cars.html new file mode 100644 index 0000000..375faf0 --- /dev/null +++ b/src/main/resources/templates/cars.html @@ -0,0 +1,51 @@ + + + + Cars + + + + +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
IDMarkModelSerial NumberFirst NameLast Name
+
+ +
+
+
+ +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 1082fbe..b93a521 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -25,6 +25,9 @@ Клиенты + + Автомобили + Консоль H2