From 345937630815ff3bf7a77330c25270452627b0c6 Mon Sep 17 00:00:00 2001 From: xmarek14 <xmarek14@fi.muni.cz> Date: Sun, 16 Apr 2023 10:56:51 +0200 Subject: [PATCH] Car attributes changed from objects to IDs --- .../cz/muni/pa165/car/data/model/Car.java | 29 ++--- .../cz/muni/pa165/car/mapper/CarMapper.java | 44 ++++++++ .../cz/muni/pa165/car/rest/CarController.java | 2 +- .../muni/pa165/car/restemplate/DbGetter.java | 46 ++++++++ .../car/service/CarComponentPairService.java | 2 - .../service/CarComponentPairServiceImpl.java | 50 +++------ .../car/service/CarDriverPairServiceImpl.java | 62 ++++------- .../cz/muni/pa165/car/service/CarService.java | 1 - .../pa165/car/service/CarServiceImpl.java | 102 +----------------- .../common_library/dtos/CarRequestDto.java | 8 +- .../common_library/dtos/CarResponseDto.java | 4 +- 11 files changed, 148 insertions(+), 202 deletions(-) create mode 100644 car/src/main/java/cz/muni/pa165/car/mapper/CarMapper.java create mode 100644 car/src/main/java/cz/muni/pa165/car/restemplate/DbGetter.java diff --git a/car/src/main/java/cz/muni/pa165/car/data/model/Car.java b/car/src/main/java/cz/muni/pa165/car/data/model/Car.java index 1d82c7f8..7dfd4f03 100644 --- a/car/src/main/java/cz/muni/pa165/car/data/model/Car.java +++ b/car/src/main/java/cz/muni/pa165/car/data/model/Car.java @@ -1,18 +1,7 @@ package cz.muni.pa165.car.data.model; -import cz.muni.pa165.component.data.model.CarComponent; -import cz.muni.pa165.driver.data.model.Driver; -import jakarta.persistence.CascadeType; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.OneToMany; -import jakarta.persistence.OneToOne; -import jakarta.persistence.Table; -import jakarta.validation.Valid; +import jakarta.persistence.*; + import java.io.Serializable; import java.util.List; import lombok.AllArgsConstructor; @@ -35,17 +24,13 @@ public class Car implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToMany(mappedBy = "id", - fetch = FetchType.EAGER) - private List<@Valid CarComponent> components; + @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER) + private List<Long> components; - @OneToMany(mappedBy = "id", - fetch = FetchType.EAGER) - private List<@Valid Driver> drivers; + @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER) + private List<Long> drivers; - @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) - @JoinColumn(name = "main_driver_id", referencedColumnName = "id") - private Driver mainDriver; + private Long mainDriverId; } diff --git a/car/src/main/java/cz/muni/pa165/car/mapper/CarMapper.java b/car/src/main/java/cz/muni/pa165/car/mapper/CarMapper.java new file mode 100644 index 00000000..44ea6e76 --- /dev/null +++ b/car/src/main/java/cz/muni/pa165/car/mapper/CarMapper.java @@ -0,0 +1,44 @@ +package cz.muni.pa165.car.mapper; + +import cz.muni.pa165.car.data.model.Car; +import cz.muni.pa165.common_library.dtos.CarRequestDto; +import cz.muni.pa165.common_library.dtos.CarResponseDto; + +/** + * Class for converting Car object and its Data Transfer Objects. + */ +public class CarMapper { + + /** + * Converts Dto object to Car object. + * + * @param carRequestDto Dto object of the car. + * @return Car object. + */ + public static Car carDtoConverter(CarRequestDto carRequestDto) { + + return Car.builder() + .id(null) + .components(carRequestDto.getComponentIds()) + .drivers(carRequestDto.getDriverIds()) + .mainDriverId(carRequestDto.getMainDriverId()) + .build(); + } + + /** + * Converts Car object to its Dto. + * + * @param car Car object. + * @return Dto object. + */ + public static CarResponseDto carConverterToDto(Car car) { + + return CarResponseDto.builder() + .id(car.getId()) + .componentIdsNames(car.getComponents()) + .driverIdsNames(car.getDrivers()) + .mainDriverId(car.getMainDriverId()) + .build(); + } + +} diff --git a/car/src/main/java/cz/muni/pa165/car/rest/CarController.java b/car/src/main/java/cz/muni/pa165/car/rest/CarController.java index 443c3f93..be67e6b1 100644 --- a/car/src/main/java/cz/muni/pa165/car/rest/CarController.java +++ b/car/src/main/java/cz/muni/pa165/car/rest/CarController.java @@ -80,7 +80,7 @@ public class CarController { */ @Operation(summary = "Get all cars") @GetMapping(path = "/all", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<List<CarResponseDto>> getAllSeasons() { + public ResponseEntity<List<CarResponseDto>> getAllCars() { return ResponseEntity.ok(carService.getAllCars()); } diff --git a/car/src/main/java/cz/muni/pa165/car/restemplate/DbGetter.java b/car/src/main/java/cz/muni/pa165/car/restemplate/DbGetter.java new file mode 100644 index 00000000..61a43c0b --- /dev/null +++ b/car/src/main/java/cz/muni/pa165/car/restemplate/DbGetter.java @@ -0,0 +1,46 @@ +package cz.muni.pa165.car.restemplate; + +import cz.muni.pa165.component.data.model.CarComponent; +import cz.muni.pa165.driver.data.model.Driver; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +/** + * Class for retrieving data from other modules using RestTemplate. + */ +@Component +public class DbGetter { + + @Autowired + private static RestTemplate client = new RestTemplate(); + + private static final String GET_DRIVER_URL = "http://localhost:8083/driver/get/id="; + private static final String GET_COMPONENT_URL = "http://localhost:8084/component?id="; + + /** + * Get a driver using RestTemplate client. + * + * @param id driver id + * @return Driver object + */ + public static Driver getDriverFromDb(Long id) { + String url = GET_DRIVER_URL + id; + ResponseEntity<Driver> response = client.getForEntity(url, Driver.class); + return response.getBody(); + } + + /** + * Get a component using RestTemplate client. + * + * @param id component id + * @return Component object + */ + public static CarComponent getComponentFromDb(Long id) { + String url = GET_COMPONENT_URL + id; + ResponseEntity<CarComponent> response = client.getForEntity(url, CarComponent.class); + return response.getBody(); + } + +} diff --git a/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java b/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java index 51d12c38..19e2cddd 100644 --- a/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java +++ b/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairService.java @@ -1,9 +1,7 @@ package cz.muni.pa165.car.service; import cz.muni.pa165.common_library.dtos.CarComponentDto; -import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarResponseDto; - import java.util.List; /** diff --git a/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairServiceImpl.java b/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairServiceImpl.java index bea04b8b..c55cf357 100644 --- a/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairServiceImpl.java +++ b/car/src/main/java/cz/muni/pa165/car/service/CarComponentPairServiceImpl.java @@ -1,18 +1,17 @@ package cz.muni.pa165.car.service; import cz.muni.pa165.car.data.repository.CarRepository; +import cz.muni.pa165.car.mapper.CarMapper; import cz.muni.pa165.common_library.dtos.CarComponentDto; -import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.component.data.model.CarComponent; import java.util.ArrayList; import java.util.List; import java.util.Objects; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; + +import static cz.muni.pa165.car.restemplate.DbGetter.getComponentFromDb; /** * Service for manipulation with car's components. @@ -20,9 +19,6 @@ import org.springframework.web.client.RestTemplate; @Service public class CarComponentPairServiceImpl implements CarComponentPairService { - @Autowired - private static RestTemplate client = new RestTemplate(); - private static final String GET_COMPONENT_URL = "http://localhost:8084/component?id="; CarRepository carRepository; /** @@ -38,27 +34,26 @@ public class CarComponentPairServiceImpl implements CarComponentPairService { public CarResponseDto addComponent(Long componentId, Long carId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - var component = getComponentById(componentId); var components = car.getComponents(); - components.add(component); + components.add(componentId); car.setComponents(components); carRepository.save(car); - return CarServiceImpl.carConverterToDto(car); + return CarMapper.carConverterToDto(car); } @Override public CarResponseDto removeComponent(Long componentId, Long carId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - var components = new ArrayList<CarComponent>(); - for (CarComponent d : car.getComponents()) { - if (!Objects.equals(d.getId(), componentId)) { - components.add(d); + var components = new ArrayList<Long>(); + for (Long id : car.getComponents()) { + if (!Objects.equals(id, componentId)) { + components.add(id); } } car.setComponents(components); carRepository.save(car); - return CarServiceImpl.carConverterToDto(car); + return CarMapper.carConverterToDto(car); } @Override @@ -66,30 +61,19 @@ public class CarComponentPairServiceImpl implements CarComponentPairService { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); var componentDtos = new ArrayList<CarComponentDto>(); - for (CarComponent c : car.getComponents()) { + for (Long id : car.getComponents()) { + CarComponent carComponent = getComponentFromDb(id); componentDtos.add( new CarComponentDto( - c.getId(), - c.getWeight(), - c.getPrice(), - c.getManufacturer(), - c.getName() + carComponent.getId(), + carComponent.getWeight(), + carComponent.getPrice(), + carComponent.getManufacturer(), + carComponent.getName() ) ); } return componentDtos; } - /** - * Get a component using RestTemplate client. - * - * @param id component id - * @return Component object - */ - public static CarComponent getComponentById(Long id) { - String url = GET_COMPONENT_URL + id; - ResponseEntity<CarComponent> response = client.getForEntity(url, CarComponent.class); - return response.getBody(); - } - } diff --git a/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairServiceImpl.java b/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairServiceImpl.java index 4f2dbbf8..3c3f8e28 100644 --- a/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairServiceImpl.java +++ b/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairServiceImpl.java @@ -1,18 +1,17 @@ package cz.muni.pa165.car.service; import cz.muni.pa165.car.data.repository.CarRepository; -import cz.muni.pa165.common_library.dtos.CarRequestDto; +import cz.muni.pa165.car.mapper.CarMapper; import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.dtos.DriverDto; import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.driver.data.model.Driver; - -import java.util.*; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; + +import static cz.muni.pa165.car.restemplate.DbGetter.getDriverFromDb; /** * Service for Driver Manager. @@ -20,10 +19,6 @@ import org.springframework.web.client.RestTemplate; @Service public class CarDriverPairServiceImpl implements CarDriverPairService { - @Autowired - private static RestTemplate client = new RestTemplate(); - - private static final String GET_DRIVER_URL = "http://localhost:8083/driver/get/id="; CarRepository carRepository; /** @@ -39,27 +34,26 @@ public class CarDriverPairServiceImpl implements CarDriverPairService { public CarResponseDto assignDriverToCar(Long driverId, Long carId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - var driver = getDriverById(driverId); var drivers = car.getDrivers(); - drivers.add(driver); + drivers.add(driverId); car.setDrivers(drivers); carRepository.save(car); - return CarServiceImpl.carConverterToDto(car); + return CarMapper.carConverterToDto(car); } @Override public CarResponseDto unassignDriverFromCar(Long driverId, Long carId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - var drivers = new ArrayList<Driver>(); - for (Driver d : car.getDrivers()) { - if (!Objects.equals(d.getId(), driverId)) { - drivers.add(d); + var drivers = new ArrayList<Long>(); + for (Long id : car.getDrivers()) { + if (!Objects.equals(id, driverId)) { + drivers.add(id); } } car.setDrivers(drivers); carRepository.save(car); - return CarServiceImpl.carConverterToDto(car); + return CarMapper.carConverterToDto(car); } @Override @@ -67,11 +61,12 @@ public class CarDriverPairServiceImpl implements CarDriverPairService { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); var driverDtos = new ArrayList<DriverDto>(); - for (Driver d : car.getDrivers()) { + for (Long id : car.getDrivers()) { + Driver driver = getDriverFromDb(id); driverDtos.add( - new DriverDto(d.getId(), - d.getName(), - d.getSurname()) + new DriverDto(driver.getId(), + driver.getName(), + driver.getSurname()) ); } return driverDtos; @@ -81,31 +76,18 @@ public class CarDriverPairServiceImpl implements CarDriverPairService { public CarResponseDto setMainDriver(Long carId, Long driverId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - var driver = getDriverById(driverId); - car.setMainDriver(driver); + car.setMainDriverId(driverId); carRepository.save(car); - return CarServiceImpl.carConverterToDto(car); + return CarMapper.carConverterToDto(car); } @Override public CarResponseDto removeMainDriver(Long carId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - car.setMainDriver(null); + car.setMainDriverId(null); carRepository.save(car); - return CarServiceImpl.carConverterToDto(car); - } - - /** - * Get a driver using RestTemplate client. - * - * @param id driver id - * @return Driver object - */ - public static Driver getDriverById(Long id) { - String url = GET_DRIVER_URL + id; - ResponseEntity<Driver> response = client.getForEntity(url, Driver.class); - return response.getBody(); + return CarMapper.carConverterToDto(car); } } diff --git a/car/src/main/java/cz/muni/pa165/car/service/CarService.java b/car/src/main/java/cz/muni/pa165/car/service/CarService.java index 167a8893..f3b923a0 100644 --- a/car/src/main/java/cz/muni/pa165/car/service/CarService.java +++ b/car/src/main/java/cz/muni/pa165/car/service/CarService.java @@ -2,7 +2,6 @@ package cz.muni.pa165.car.service; import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarResponseDto; - import java.util.List; /** diff --git a/car/src/main/java/cz/muni/pa165/car/service/CarServiceImpl.java b/car/src/main/java/cz/muni/pa165/car/service/CarServiceImpl.java index f2e2d1cf..c8119d00 100644 --- a/car/src/main/java/cz/muni/pa165/car/service/CarServiceImpl.java +++ b/car/src/main/java/cz/muni/pa165/car/service/CarServiceImpl.java @@ -1,19 +1,15 @@ package cz.muni.pa165.car.service; -import cz.muni.pa165.car.data.model.Car; import cz.muni.pa165.car.data.repository.CarRepository; +import cz.muni.pa165.car.mapper.CarMapper; import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.exception.DatabaseException; -import cz.muni.pa165.component.data.model.CarComponent; -import cz.muni.pa165.driver.data.model.Driver; -import java.util.ArrayList; import java.util.List; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.util.Pair; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; + +import static cz.muni.pa165.car.mapper.CarMapper.carConverterToDto; +import static cz.muni.pa165.car.mapper.CarMapper.carDtoConverter; /** * Service for car management. @@ -21,12 +17,6 @@ import org.springframework.web.client.RestTemplate; @Service public class CarServiceImpl implements CarService { - @Autowired - private static RestTemplate client = new RestTemplate(); - - private static final String GET_DRIVER_URL = "http://localhost:8083/driver/get/id="; - private static final String GET_COMPONENT_URL = "http://localhost:8084/component?id="; - CarRepository carRepository; /** @@ -57,7 +47,7 @@ public class CarServiceImpl implements CarService { return carRepository .findAll() .stream() - .map(CarServiceImpl::carConverterToDto) + .map(CarMapper::carConverterToDto) .toList(); } @@ -67,86 +57,4 @@ public class CarServiceImpl implements CarService { return "Car with id = " + carId + " deleted!"; } - /** - * Converts Dto object to Car object. - * TODO: Change to the correct version of converter. - * - * @param carRequestDto Dto object of the car. - * @return Car object. - */ - public static Car carDtoConverter(CarRequestDto carRequestDto) { - - var car = Car.builder().id(null).build(); - - var components = new ArrayList<CarComponent>(); - for (Pair<Long, String> componentPair : carRequestDto.getComponentIdsNames()) { - Long id = componentPair.getFirst(); - components.add(getComponentById(id)); - } - car.setComponents(components); - - var drivers = new ArrayList<Driver>(); - for (Pair<Long, String> driverPair : carRequestDto.getDriverIdsNames()) { - Long id = driverPair.getFirst(); - drivers.add(getDriverById(id)); - } - car.setDrivers(drivers); - - if (carRequestDto.getMainDriverId() != null) { - var mainDriver = getDriverById(carRequestDto.getMainDriverId()); - car.setMainDriver(mainDriver); - } - - return car; - } - - /** - * Converts Car object to its Dto. - * - * @param car Car object. - * @return Dto object. - */ - public static CarResponseDto carConverterToDto(Car car) { - Long id = null; - if (car.getMainDriver() != null) { - id = car.getMainDriver().getId(); - } - return CarResponseDto.builder() - .id(car.getId()) - .componentIdsNames(car.getComponents() - .stream() - .map(carComponent -> Pair.of(carComponent.getId(), carComponent.getName())) - .toList()) - .driverIdsNames(car.getDrivers() - .stream() - .map(driver -> Pair.of(driver.getId(), driver.getName())) - .toList()) - .mainDriverId(id) - .build(); - } - - /** - * Get a driver using RestTemplate client. - * - * @param id driver id - * @return Driver object - */ - public static Driver getDriverById(Long id) { - String url = GET_DRIVER_URL + id; - ResponseEntity<Driver> response = client.getForEntity(url, Driver.class); - return response.getBody(); - } - - /** - * Get a component using RestTemplate client. - * - * @param id component id - * @return Component object - */ - public static CarComponent getComponentById(Long id) { - String url = GET_COMPONENT_URL + id; - ResponseEntity<CarComponent> response = client.getForEntity(url, CarComponent.class); - return response.getBody(); - } - } diff --git a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarRequestDto.java b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarRequestDto.java index b52ce17f..7d4e4aa3 100644 --- a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarRequestDto.java +++ b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarRequestDto.java @@ -19,14 +19,14 @@ import org.springframework.data.util.Pair; public class CarRequestDto { @NotNull - @Schema(description = "car's components ids and names", + @Schema(description = "car's components ids", example = "[]") - List<Pair<Long, String>> componentIdsNames; + List<Long> componentIds; @NotNull - @Schema(description = "car's drivers ids and names", + @Schema(description = "car's drivers ids", example = "[]") - List<Pair<Long, String>> driverIdsNames; + List<Long> driverIds; @Schema(description = "id of car's main driver", example = "null") diff --git a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarResponseDto.java b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarResponseDto.java index 0ce66d5f..ddc8a224 100644 --- a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarResponseDto.java +++ b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarResponseDto.java @@ -27,12 +27,12 @@ public class CarResponseDto { @NotNull @Schema(description = "car's components ids and names", example = "[]") - List<Pair<Long, String>> componentIdsNames; + List<Long> componentIdsNames; @NotNull @Schema(description = "car's drivers ids and names", example = "[]") - List<Pair<Long, String>> driverIdsNames; + List<Long> driverIdsNames; @Schema(description = "id of car's main driver", example = "1") -- GitLab