diff --git a/car/pom.xml b/car/pom.xml index 91c37dc076ca0fde137a06281d1b7a961e2b8624..108906b91dc88f29a16498d97bfb96d11cdf4082 100644 --- a/car/pom.xml +++ b/car/pom.xml @@ -70,4 +70,37 @@ <maven.compiler.target>17</maven.compiler.target> </properties> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>3.2.0</version> + <configuration> + <archive> + <manifest> + <mainClass>cz.muni.pa165.car.App</mainClass> + </manifest> + </archive> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-shade-plugin</artifactId> + <version>3.2.4</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>shade</goal> + </goals> + <configuration> + <createDependencyReducedPom>false</createDependencyReducedPom> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </project> \ No newline at end of file diff --git a/car/src/main/java/cz/muni/pa165/car/App.java b/car/src/main/java/cz/muni/pa165/car/App.java index 036de8b87844ffaff8dd7cb9eef6bbe2aa0096ed..acf19d92fe628aadaa00a9cfbab48f4bcba05208 100644 --- a/car/src/main/java/cz/muni/pa165/car/App.java +++ b/car/src/main/java/cz/muni/pa165/car/App.java @@ -1,25 +1,37 @@ package cz.muni.pa165.car; import cz.muni.pa165.car.data.model.Car; +import cz.muni.pa165.common_library.client.ClientConfig; import cz.muni.pa165.common_library.exception.RestExceptionHandler; import cz.muni.pa165.component.data.model.CarComponent; import cz.muni.pa165.driver.data.model.Driver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.web.client.RestTemplate; /** * Main app. */ @SpringBootApplication @EnableJpaRepositories(basePackages = {"cz.muni.pa165.car.data.repository"}) +@EnableTransactionManagement @EntityScan(basePackageClasses = {Car.class, Driver.class, CarComponent.class}) +//@Import({RestExceptionHandler.class, ClientConfig.class}) @Import(RestExceptionHandler.class) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } + } 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 1d82c7f881e541db5ef1bbb5e00bbfeb4a5fea16..75d61eed69f2b7274cb105df5fe2a7f16c852360 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,12 @@ 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.ElementCollection; 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 java.io.Serializable; import java.util.List; import lombok.AllArgsConstructor; @@ -35,17 +29,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/data/repository/CarComponentRepository.java b/car/src/main/java/cz/muni/pa165/car/data/repository/CarComponentRepository.java deleted file mode 100644 index 7d8c61e4d21fc3f4bf2f66162d439f3c1a589ffa..0000000000000000000000000000000000000000 --- a/car/src/main/java/cz/muni/pa165/car/data/repository/CarComponentRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package cz.muni.pa165.car.data.repository; - -import cz.muni.pa165.component.data.model.CarComponent; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -/** - * Repository for Car Component. - */ -@Repository -public interface CarComponentRepository extends JpaRepository<CarComponent, Long> {} diff --git a/car/src/main/java/cz/muni/pa165/car/data/repository/DriverRepository.java b/car/src/main/java/cz/muni/pa165/car/data/repository/DriverRepository.java deleted file mode 100644 index cb2bfe5179a78213caed2d8d2b6cdd53d6b3d06d..0000000000000000000000000000000000000000 --- a/car/src/main/java/cz/muni/pa165/car/data/repository/DriverRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package cz.muni.pa165.car.data.repository; - -import cz.muni.pa165.driver.data.model.Driver; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -/** - * Repository for driver. - */ -@Repository -public interface DriverRepository extends JpaRepository<Driver, Long> {} 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 0000000000000000000000000000000000000000..44ea6e762f75ad7b8c9d366f46c64dc6441858e8 --- /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/CarComponentPairController.java b/car/src/main/java/cz/muni/pa165/car/rest/CarComponentPairController.java index a36ff9c478bd281801c6c6afc42c0fbb71c47b2a..8cbf5a412fee36051c429724beaf427698a88b8f 100644 --- a/car/src/main/java/cz/muni/pa165/car/rest/CarComponentPairController.java +++ b/car/src/main/java/cz/muni/pa165/car/rest/CarComponentPairController.java @@ -2,7 +2,8 @@ package cz.muni.pa165.car.rest; import cz.muni.pa165.car.service.CarComponentPairService; import cz.muni.pa165.common_library.dtos.CarComponentDto; -import cz.muni.pa165.common_library.dtos.CarDto; +import cz.muni.pa165.common_library.dtos.CarRequestDto; +import cz.muni.pa165.common_library.dtos.CarResponseDto; import io.swagger.v3.oas.annotations.Operation; import java.util.List; import org.springframework.http.MediaType; @@ -37,8 +38,8 @@ public class CarComponentPairController { @Operation(summary = "Add component to a car") @PutMapping(path = "/addcomponent", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<CarDto> addComponent(@RequestParam Long componentId, - @RequestParam Long carId) { + public ResponseEntity<CarResponseDto> addComponent(@RequestParam Long componentId, + @RequestParam Long carId) { return ResponseEntity.ok(carComponentService.addComponent(componentId, carId)); } @@ -52,8 +53,8 @@ public class CarComponentPairController { @Operation(summary = "Remove component from a car") @PutMapping(path = "/removecomponent", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<CarDto> removeComponent(@RequestParam Long componentId, - @RequestParam Long carId) { + public ResponseEntity<CarResponseDto> removeComponent(@RequestParam Long componentId, + @RequestParam Long carId) { return ResponseEntity.ok(carComponentService.removeComponent(componentId, carId)); } 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 893170fd6bd088c974d2bbb69d14bb7f352d0242..be67e6b110a459cd961524b33e54fa758bd64d2e 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 @@ -1,7 +1,8 @@ package cz.muni.pa165.car.rest; import cz.muni.pa165.car.service.CarService; -import cz.muni.pa165.common_library.dtos.CarDto; +import cz.muni.pa165.common_library.dtos.CarRequestDto; +import cz.muni.pa165.common_library.dtos.CarResponseDto; import io.swagger.v3.oas.annotations.Operation; import jakarta.validation.Valid; import java.util.List; @@ -10,7 +11,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -39,14 +39,14 @@ public class CarController { /** * Creates a new car. * - * @param carDto the DTO representing the car to be created + * @param carRequestDto the DTO representing the car to be created * @return a ResponseEntity containing the DTO of the created car */ @Operation(summary = "Create a car") @PostMapping(path = "/", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<CarDto> createCar(@Valid @RequestBody CarDto carDto) { - return ResponseEntity.ok(carService.postCar(carDto)); + public ResponseEntity<CarResponseDto> createCar(@Valid @RequestBody CarRequestDto carRequestDto) { + return ResponseEntity.ok(carService.postCar(carRequestDto)); } /** @@ -56,7 +56,7 @@ public class CarController { * @return a ResponseEntity containing a message indicating whether the car was deleted or not */ @Operation(summary = "Delete a car") - @DeleteMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + @DeleteMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> deleteCar(@RequestParam Long carId) { return ResponseEntity.ok(carService.deleteById(carId)); } @@ -64,13 +64,13 @@ public class CarController { /** * Retrieves a car by its ID. * - * @param id the ID of the car to retrieve + * @param carId the ID of the car to retrieve * @return a ResponseEntity containing the DTO of the retrieved car */ @Operation(summary = "Get a car") - @GetMapping(path = "/id={id}", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<CarDto> getCar(@PathVariable("id") Long id) { - return ResponseEntity.ok(carService.getCarById(id)); + @GetMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<CarResponseDto> getCar(@RequestParam Long carId) { + return ResponseEntity.ok(carService.getCarById(carId)); } /** @@ -79,8 +79,8 @@ public class CarController { * @return a ResponseEntity containing a list of DTOs representing all cars */ @Operation(summary = "Get all cars") - @GetMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<List<CarDto>> getAllSeasons() { + @GetMapping(path = "/all", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<List<CarResponseDto>> getAllCars() { return ResponseEntity.ok(carService.getAllCars()); } diff --git a/car/src/main/java/cz/muni/pa165/car/rest/CarDriverPairController.java b/car/src/main/java/cz/muni/pa165/car/rest/CarDriverPairController.java index 88631333bdac050fb34f5f198982d3c53b91a96e..3c1344dc984b8451e4c9ecc1bb78650d03a5aacd 100644 --- a/car/src/main/java/cz/muni/pa165/car/rest/CarDriverPairController.java +++ b/car/src/main/java/cz/muni/pa165/car/rest/CarDriverPairController.java @@ -1,7 +1,8 @@ package cz.muni.pa165.car.rest; import cz.muni.pa165.car.service.CarDriverPairService; -import cz.muni.pa165.common_library.dtos.CarDto; +import cz.muni.pa165.common_library.dtos.CarRequestDto; +import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.dtos.DriverDto; import io.swagger.v3.oas.annotations.Operation; import java.util.List; @@ -21,10 +22,10 @@ import org.springframework.web.bind.annotation.RestController; @Validated public class CarDriverPairController { - CarDriverPairService carDriverService; + CarDriverPairService carDriverPairService; public CarDriverPairController(CarDriverPairService driverManagerService) { - this.carDriverService = driverManagerService; + this.carDriverPairService = driverManagerService; } /** @@ -37,9 +38,9 @@ public class CarDriverPairController { @Operation(summary = "Assign driver to a car") @PutMapping(path = "/assign", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<CarDto> assignDriverToCar(@RequestParam Long driverId, - @RequestParam Long carId) { - return ResponseEntity.ok(carDriverService.assignDriverToCar(driverId, carId)); + public ResponseEntity<CarResponseDto> assignDriverToCar(@RequestParam Long driverId, + @RequestParam Long carId) { + return ResponseEntity.ok(carDriverPairService.assignDriverToCar(driverId, carId)); } /** @@ -52,9 +53,9 @@ public class CarDriverPairController { @Operation(summary = "Unassign driver from a car") @PutMapping(path = "/unassign", produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity<CarDto> unassignDriverFromCar(@RequestParam Long driverId, - @RequestParam Long carId) { - return ResponseEntity.ok(carDriverService.unassignDriverFromCar(driverId, carId)); + public ResponseEntity<CarResponseDto> unassignDriverFromCar(@RequestParam Long driverId, + @RequestParam Long carId) { + return ResponseEntity.ok(carDriverPairService.unassignDriverFromCar(driverId, carId)); } /** @@ -67,6 +68,36 @@ public class CarDriverPairController { @PutMapping(path = "/alldrivers", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<List<DriverDto>> getAllDriversOfCar(@RequestParam Long carId) { - return ResponseEntity.ok(carDriverService.getAllDriversOfCar(carId)); + return ResponseEntity.ok(carDriverPairService.getAllDriversOfCar(carId)); } + + /** + * Sets the main driver of the car identified by the given ID. + * + * @param carId The ID of the car to set the main driver for. + * @param driverId The ID of the driver to set as the main driver. + * @return A ResponseEntity containing a CarDto object + * representing the updated car. + */ + @Operation(summary = "Set main driver for the car") + @PutMapping(path = "/setmain", + produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<CarResponseDto> setMainDriver(@RequestParam Long carId, Long driverId) { + return ResponseEntity.ok(carDriverPairService.setMainDriver(carId, driverId)); + } + + /** + * Removes the main driver from the car identified by the given ID. + * + * @param carId The ID of the car to remove the main driver from. + * @return A ResponseEntity containing a CarDto object + * representing the updated car. + */ + @Operation(summary = "Set main driver to null value") + @PutMapping(path = "/removemain", + produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<CarResponseDto> removeMainDriver(@RequestParam Long carId) { + return ResponseEntity.ok(carDriverPairService.removeMainDriver(carId)); + } + } 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 0000000000000000000000000000000000000000..61a43c0bd756c1dee35f2ba555a601233821d26d --- /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 5bd94c3db8f658b9be98321a7d7efb5f2a5a3750..19e2cdddc5bc605e7d33486faa56ad3b83ac7a41 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,7 +1,7 @@ package cz.muni.pa165.car.service; import cz.muni.pa165.common_library.dtos.CarComponentDto; -import cz.muni.pa165.common_library.dtos.CarDto; +import cz.muni.pa165.common_library.dtos.CarResponseDto; import java.util.List; /** @@ -15,7 +15,7 @@ public interface CarComponentPairService { * @param carId Id of the car. * @return Car with added component. */ - CarDto addComponent(Long componentId, Long carId); + CarResponseDto addComponent(Long componentId, Long carId); /** * Calls repository to remove a component from a car. @@ -24,7 +24,7 @@ public interface CarComponentPairService { * @param carId Id of the car. * @return Car with removed component. */ - CarDto removeComponent(Long componentId, Long carId); + CarResponseDto removeComponent(Long componentId, Long carId); /** * Calls repository ond returns oll components of specific car. 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 bf79e3d1d062ab71320aa6c2e90664a7f709fdb0..f0afe6ccf9ef3677214f516a2c6e32136946a6b8 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,10 +1,11 @@ package cz.muni.pa165.car.service; -import cz.muni.pa165.car.data.repository.CarComponentPairRepository; -import cz.muni.pa165.car.data.repository.CarComponentRepository; +import static cz.muni.pa165.car.restemplate.DbGetter.getComponentFromDb; + 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.CarDto; +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; @@ -18,51 +19,41 @@ import org.springframework.stereotype.Service; @Service public class CarComponentPairServiceImpl implements CarComponentPairService { - CarComponentPairRepository carComponentPairRepository; - CarComponentRepository carComponentRepository; CarRepository carRepository; /** * Constructor for Car - Car Component Service. * - * @param carComponentPairRepository Car - Car Component repository - * @param carComponentRepository Car Component repository * @param carRepository Car repository */ - public CarComponentPairServiceImpl(CarComponentPairRepository carComponentPairRepository, - CarComponentRepository carComponentRepository, - CarRepository carRepository) { - this.carComponentPairRepository = carComponentPairRepository; - this.carComponentRepository = carComponentRepository; + public CarComponentPairServiceImpl(CarRepository carRepository) { this.carRepository = carRepository; } @Override - public CarDto addComponent(Long componentId, Long carId) { + public CarResponseDto addComponent(Long componentId, Long carId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - var component = carComponentRepository.findById(componentId).orElseThrow( - () -> new DatabaseException("Component not found")); 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 CarDto removeComponent(Long componentId, Long carId) { + 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 @@ -70,14 +61,15 @@ 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() ) ); } diff --git a/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairService.java b/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairService.java index edc598223d253a83c62269fa2b3a04ead082ab46..0131343725de5ed3805c64c1bfdafcea7e97b40a 100644 --- a/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairService.java +++ b/car/src/main/java/cz/muni/pa165/car/service/CarDriverPairService.java @@ -1,6 +1,7 @@ package cz.muni.pa165.car.service; -import cz.muni.pa165.common_library.dtos.CarDto; +import cz.muni.pa165.common_library.dtos.CarRequestDto; +import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.dtos.DriverDto; import java.util.List; @@ -15,7 +16,7 @@ public interface CarDriverPairService { * @param carId Id of the car. * @return Car object that the driver was assigned to. */ - CarDto assignDriverToCar(Long driverId, Long carId); + CarResponseDto assignDriverToCar(Long driverId, Long carId); /** * Calls repository to unassign a driver from a car, if unsuccessful, throws exception. @@ -24,7 +25,7 @@ public interface CarDriverPairService { * @param carId Id of the car. * @return Car object that the driver was unassigned from. */ - CarDto unassignDriverFromCar(Long driverId, Long carId); + CarResponseDto unassignDriverFromCar(Long driverId, Long carId); /** * Calls repository to return list of all drivers of specific car. @@ -33,4 +34,21 @@ public interface CarDriverPairService { * @return List of all drivers assigned to the car. */ List<DriverDto> getAllDriversOfCar(Long carId); + + /** + * Sets the main driver of the car identified by the given ID. + * + * @param carId The ID of the car to set the main driver for. + * @param driverId The ID of the driver to set as the main driver. + * @return A CarDto object representing the updated car. + */ + CarResponseDto setMainDriver(Long carId, Long driverId); + + /** + * Removes the main driver from the car identified by the given ID. + * + * @param carId The ID of the car to remove the main driver from. + * @return A CarDto object representing the updated car. + */ + CarResponseDto removeMainDriver(Long carId); } 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 15ef76773a8ce8a6d762777fac33d19fc8322d00..dabba4a173a72b33faefd6aed346f495597243d5 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,9 +1,10 @@ package cz.muni.pa165.car.service; -import cz.muni.pa165.car.data.repository.CarDriverPairRepository; +import static cz.muni.pa165.car.restemplate.DbGetter.getDriverFromDb; + import cz.muni.pa165.car.data.repository.CarRepository; -import cz.muni.pa165.car.data.repository.DriverRepository; -import cz.muni.pa165.common_library.dtos.CarDto; +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; @@ -18,51 +19,41 @@ import org.springframework.stereotype.Service; @Service public class CarDriverPairServiceImpl implements CarDriverPairService { - CarDriverPairRepository carDriverPairRepository; - DriverRepository driverRepository; CarRepository carRepository; /** * Constructor for Car - Driver Service. * - * @param carDriverPairRepository Car - Driver repository - * @param driverRepository Driver repository * @param carRepository Car repository */ - public CarDriverPairServiceImpl(CarDriverPairRepository carDriverPairRepository, - DriverRepository driverRepository, - CarRepository carRepository) { - this.carDriverPairRepository = carDriverPairRepository; - this.driverRepository = driverRepository; + public CarDriverPairServiceImpl(CarRepository carRepository) { this.carRepository = carRepository; } @Override - public CarDto assignDriverToCar(Long driverId, Long carId) { + public CarResponseDto assignDriverToCar(Long driverId, Long carId) { var car = carRepository.findById(carId).orElseThrow( () -> new DatabaseException("Car not found")); - var driver = driverRepository.findById(driverId).orElseThrow( - () -> new DatabaseException("Driver not found")); 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 CarDto unassignDriverFromCar(Long driverId, Long carId) { + 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 @@ -70,14 +61,33 @@ 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; } + @Override + public CarResponseDto setMainDriver(Long carId, Long driverId) { + var car = carRepository.findById(carId).orElseThrow( + () -> new DatabaseException("Car not found")); + car.setMainDriverId(driverId); + carRepository.save(car); + return CarMapper.carConverterToDto(car); + } + + @Override + public CarResponseDto removeMainDriver(Long carId) { + var car = carRepository.findById(carId).orElseThrow( + () -> new DatabaseException("Car not found")); + car.setMainDriverId(null); + carRepository.save(car); + 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 7fe1dbf0aed56f0e32d8d667c36727b59721fc4f..f3b923a0ac0ff620324e657fe874c2dc7a357b45 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 @@ -1,6 +1,7 @@ package cz.muni.pa165.car.service; -import cz.muni.pa165.common_library.dtos.CarDto; +import cz.muni.pa165.common_library.dtos.CarRequestDto; +import cz.muni.pa165.common_library.dtos.CarResponseDto; import java.util.List; /** @@ -10,10 +11,10 @@ public interface CarService { /** * Calls repository to insert a car into the database. * - * @param carDto Dto object of the car. + * @param carRequestDto Dto object of the car. * @return Dto object of inserted car. */ - CarDto postCar(CarDto carDto); + CarResponseDto postCar(CarRequestDto carRequestDto); /** * Calls repository to get a specific car from the database. @@ -21,14 +22,14 @@ public interface CarService { * @param carId car id * @return Dto object of the car */ - CarDto getCarById(Long carId); + CarResponseDto getCarById(Long carId); /** * Calls repository to get all cars in the database. * * @return list of Dto objects */ - List<CarDto> getAllCars(); + List<CarResponseDto> getAllCars(); /** * Calls repository to delete a car in the database. 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 6171479b2d48ed525c241224835be03c22bc900c..874dbf4a907219b91a5d232c17e6f5dd64113cd9 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,16 +1,17 @@ package cz.muni.pa165.car.service; -import cz.muni.pa165.car.data.model.Car; -import cz.muni.pa165.car.data.repository.CarComponentRepository; +import static cz.muni.pa165.car.mapper.CarMapper.carConverterToDto; +import static cz.muni.pa165.car.mapper.CarMapper.carDtoConverter; + import cz.muni.pa165.car.data.repository.CarRepository; -import cz.muni.pa165.car.data.repository.DriverRepository; -import cz.muni.pa165.common_library.dtos.CarDto; +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 java.util.ArrayList; import java.util.List; -import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; + /** * Service for car management. */ @@ -18,31 +19,23 @@ import org.springframework.stereotype.Service; public class CarServiceImpl implements CarService { CarRepository carRepository; - CarComponentRepository componentRepository; - DriverRepository driverRepository; /** * Constructor for Car Service. * - * @param carRepository Car repository - * @param componentRepository Component repository - * @param driverRepository Driver repository + * @param carRepository Car repository */ - public CarServiceImpl(CarRepository carRepository, - CarComponentRepository componentRepository, - DriverRepository driverRepository) { + public CarServiceImpl(CarRepository carRepository) { this.carRepository = carRepository; - this.componentRepository = componentRepository; - this.driverRepository = driverRepository; } @Override - public CarDto postCar(CarDto carDto) { - return carConverterToDto(carRepository.save(carDtoConverter(carDto))); + public CarResponseDto postCar(CarRequestDto carRequestDto) { + return carConverterToDto(carRepository.save(carDtoConverter(carRequestDto))); } @Override - public CarDto getCarById(Long carId) { + public CarResponseDto getCarById(Long carId) { return carConverterToDto(carRepository.findById(carId).orElseThrow( () -> new DatabaseException( "Finding car with id=" + carId + " was unsuccessful" @@ -51,11 +44,11 @@ public class CarServiceImpl implements CarService { } @Override - public List<CarDto> getAllCars() { + public List<CarResponseDto> getAllCars() { return carRepository .findAll() .stream() - .map(CarServiceImpl::carConverterToDto) + .map(CarMapper::carConverterToDto) .toList(); } @@ -65,72 +58,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 carDto Dto object of the car. - * @return Car object. - */ - public static Car carDtoConverter(CarDto carDto) { - - // TEST VERSION WHEN COMPONENTS AND DRIVERS DO NOT EXIST - return Car.builder() - .id(carDto.getId()) - .components(new ArrayList<>()) - .drivers(new ArrayList<>()) - .mainDriver(null) - .build(); - - // TO BE USED IN FINAL VERSION - /* - var car = Car.builder() - .id(carDto.getId()) - .build(); - - var components = new ArrayList<CarComponent>(); - for (Pair<Long, String> componentPair : carDto.getComponentIdsNames()) { - Long id = componentPair.getFirst(); - components.add(componentRepository.findById(id).orElseThrow( - () -> new DatabaseException("Component not found") - )); - } - car.setComponents(components); - var drivers = new ArrayList<Driver>(); - for (Pair<Long, String> driverPair : carDto.getDriverIdsNames()) { - Long id = driverPair.getFirst(); - drivers.add(driverRepository.findById(id).orElseThrow( - () -> new DatabaseException("Driver not found") - )); - } - car.setDrivers(drivers); - return car; - */ - } - - /** - * Converts Car object to its Dto. - * - * @param car Car object. - * @return Dto object. - */ - public static CarDto carConverterToDto(Car car) { - Long id = null; - if (car.getMainDriver() != null) { - id = car.getMainDriver().getId(); - } - return CarDto.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(); - } - } diff --git a/common_library/src/main/java/cz/muni/pa165/common_library/client/ClientConfig.java b/common_library/src/main/java/cz/muni/pa165/common_library/client/ClientConfig.java index 34368b71643175fec54c053f69bb221afae34ec2..9cd037e7e21e5cbc37b002ae83095f49db5ea8ed 100644 --- a/common_library/src/main/java/cz/muni/pa165/common_library/client/ClientConfig.java +++ b/common_library/src/main/java/cz/muni/pa165/common_library/client/ClientConfig.java @@ -7,7 +7,7 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; /** - * Client configuration. + * Custom Rest Template class. */ @Configuration public class ClientConfig { 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 new file mode 100644 index 0000000000000000000000000000000000000000..7d4e4aa3c014b848a0aad1484f74f05ce62df19c --- /dev/null +++ b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarRequestDto.java @@ -0,0 +1,35 @@ +package cz.muni.pa165.common_library.dtos; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.util.Pair; + +/** + * Data Transfer object for Car class. + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CarRequestDto { + + @NotNull + @Schema(description = "car's components ids", + example = "[]") + List<Long> componentIds; + + @NotNull + @Schema(description = "car's drivers ids", + example = "[]") + List<Long> driverIds; + + @Schema(description = "id of car's main driver", + example = "null") + Long mainDriverId; + +} diff --git a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarDto.java b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarResponseDto.java similarity index 80% rename from common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarDto.java rename to common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarResponseDto.java index 1b9ce2fa9cefca649e1f5a0afd32558746565503..93a5a8b57fdd65328ed7f3e7484b929bb6212a4a 100644 --- a/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarDto.java +++ b/common_library/src/main/java/cz/muni/pa165/common_library/dtos/CarResponseDto.java @@ -7,7 +7,6 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import org.springframework.data.util.Pair; /** * Data Transfer object for Car class. @@ -16,22 +15,22 @@ import org.springframework.data.util.Pair; @Builder @NoArgsConstructor @AllArgsConstructor -public class CarDto { +public class CarResponseDto { @NotNull @Schema(description = "car id", - example = "1") + example = "1") Long id; @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")