Skip to content
Snippets Groups Projects
Commit b0d743cf authored by Diana Gulčíková's avatar Diana Gulčíková
Browse files

small fixes

parent b2baa582
No related branches found
No related tags found
2 merge requests!60Docker,!40Small fixes on car module
Pipeline #
...@@ -8,7 +8,7 @@ import jakarta.persistence.GenerationType; ...@@ -8,7 +8,7 @@ import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.Set;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
...@@ -30,10 +30,10 @@ public class Car implements Serializable { ...@@ -30,10 +30,10 @@ public class Car implements Serializable {
private Long id; private Long id;
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER) @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
private List<Long> components; private Set<Long> components;
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER) @ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
private List<Long> drivers; private Set<Long> drivers;
private Long mainDriverId; private Long mainDriverId;
......
package cz.muni.pa165.car.mapper; package cz.muni.pa165.car.mapper;
import static cz.muni.pa165.car.restemplate.DbGetter.getComponentFromDb;
import cz.muni.pa165.car.data.model.Car; import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarRequestDto;
import cz.muni.pa165.common_library.dtos.CarResponseDto; 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.HashSet;
/** /**
* Class for converting Car object and its Data Transfer Objects. * Class for converting Car object and its Data Transfer Objects.
...@@ -19,8 +25,8 @@ public class CarMapper { ...@@ -19,8 +25,8 @@ public class CarMapper {
return Car.builder() return Car.builder()
.id(null) .id(null)
.components(carRequestDto.getComponentIds()) .components(new HashSet<>(carRequestDto.getComponentIds()))
.drivers(carRequestDto.getDriverIds()) .drivers(new HashSet<>(carRequestDto.getDriverIds()))
.mainDriverId(carRequestDto.getMainDriverId()) .mainDriverId(carRequestDto.getMainDriverId())
.build(); .build();
} }
...@@ -35,10 +41,11 @@ public class CarMapper { ...@@ -35,10 +41,11 @@ public class CarMapper {
return CarResponseDto.builder() return CarResponseDto.builder()
.id(car.getId()) .id(car.getId())
.componentIdsNames(car.getComponents()) .componentIdsNames(new ArrayList<>(car.getComponents()))
.driverIdsNames(car.getDrivers()) .driverIdsNames(new ArrayList<>(car.getDrivers()))
.mainDriverId(car.getMainDriverId()) .mainDriverId(car.getMainDriverId())
.build(); .build();
} }
} }
...@@ -13,11 +13,10 @@ import org.springframework.web.client.RestTemplate; ...@@ -13,11 +13,10 @@ import org.springframework.web.client.RestTemplate;
@Component @Component
public class DbGetter { public class DbGetter {
@Autowired
private static RestTemplate client = new RestTemplate(); private static RestTemplate client = new RestTemplate();
private static final String GET_DRIVER_URL = "http://localhost:8083/driver/get/id="; 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="; private static final String GET_COMPONENT_URL = "http://localhost:8084/component/id?componentId=";
/** /**
* Get a driver using RestTemplate client. * Get a driver using RestTemplate client.
......
...@@ -2,6 +2,7 @@ package cz.muni.pa165.car.service; ...@@ -2,6 +2,7 @@ package cz.muni.pa165.car.service;
import static cz.muni.pa165.car.restemplate.DbGetter.getComponentFromDb; import static cz.muni.pa165.car.restemplate.DbGetter.getComponentFromDb;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.car.data.repository.CarRepository; import cz.muni.pa165.car.data.repository.CarRepository;
import cz.muni.pa165.car.mapper.CarMapper; import cz.muni.pa165.car.mapper.CarMapper;
import cz.muni.pa165.common_library.dtos.CarComponentDto; import cz.muni.pa165.common_library.dtos.CarComponentDto;
...@@ -9,6 +10,7 @@ import cz.muni.pa165.common_library.dtos.CarResponseDto; ...@@ -9,6 +10,7 @@ import cz.muni.pa165.common_library.dtos.CarResponseDto;
import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.common_library.exception.DatabaseException;
import cz.muni.pa165.component.data.model.CarComponent; import cz.muni.pa165.component.data.model.CarComponent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -34,8 +36,13 @@ public class CarComponentPairServiceImpl implements CarComponentPairService { ...@@ -34,8 +36,13 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
public CarResponseDto addComponent(Long componentId, Long carId) { public CarResponseDto addComponent(Long componentId, Long carId) {
var car = carRepository.findById(carId).orElseThrow( var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found")); () -> new DatabaseException("Car not found"));
if (isComponentInUse(componentId, carId)) {
throw new DatabaseException("Component with id " + componentId + " already in use");
}
var components = car.getComponents(); var components = car.getComponents();
components.add(componentId); CarComponent carComponent = getComponentFromDb(componentId);
components.add(carComponent.getId());
car.setComponents(components); car.setComponents(components);
carRepository.save(car); carRepository.save(car);
return CarMapper.carConverterToDto(car); return CarMapper.carConverterToDto(car);
...@@ -45,7 +52,7 @@ public class CarComponentPairServiceImpl implements CarComponentPairService { ...@@ -45,7 +52,7 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
public CarResponseDto removeComponent(Long componentId, Long carId) { public CarResponseDto removeComponent(Long componentId, Long carId) {
var car = carRepository.findById(carId).orElseThrow( var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found")); () -> new DatabaseException("Car not found"));
var components = new ArrayList<Long>(); var components = new HashSet<Long>();
for (Long id : car.getComponents()) { for (Long id : car.getComponents()) {
if (!Objects.equals(id, componentId)) { if (!Objects.equals(id, componentId)) {
components.add(id); components.add(id);
...@@ -76,4 +83,15 @@ public class CarComponentPairServiceImpl implements CarComponentPairService { ...@@ -76,4 +83,15 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
return componentDtos; return componentDtos;
} }
private boolean isComponentInUse(Long componentId, Long carId) {
var cars = carRepository.findAll();
for (Car car: cars) {
if (car.getComponents().stream()
.anyMatch(x -> x.longValue() == componentId && !Objects.equals(car.getId(), carId))){
return true;
}
}
return false;
}
} }
...@@ -9,6 +9,7 @@ import cz.muni.pa165.common_library.dtos.DriverDto; ...@@ -9,6 +9,7 @@ import cz.muni.pa165.common_library.dtos.DriverDto;
import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.common_library.exception.DatabaseException;
import cz.muni.pa165.driver.data.model.Driver; import cz.muni.pa165.driver.data.model.Driver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -34,9 +35,12 @@ public class CarDriverPairServiceImpl implements CarDriverPairService { ...@@ -34,9 +35,12 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
public CarResponseDto assignDriverToCar(Long driverId, Long carId) { public CarResponseDto assignDriverToCar(Long driverId, Long carId) {
var car = carRepository.findById(carId).orElseThrow( var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found")); () -> new DatabaseException("Car not found"));
var drivers = car.getDrivers(); var drivers = car.getDrivers();
drivers.add(driverId); var savedDriver = getDriverFromDb(driverId);
drivers.add(savedDriver.getId());
car.setDrivers(drivers); car.setDrivers(drivers);
carRepository.save(car); carRepository.save(car);
return CarMapper.carConverterToDto(car); return CarMapper.carConverterToDto(car);
} }
...@@ -45,7 +49,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService { ...@@ -45,7 +49,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
public CarResponseDto unassignDriverFromCar(Long driverId, Long carId) { public CarResponseDto unassignDriverFromCar(Long driverId, Long carId) {
var car = carRepository.findById(carId).orElseThrow( var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found")); () -> new DatabaseException("Car not found"));
var drivers = new ArrayList<Long>(); var drivers = new HashSet<Long>();
for (Long id : car.getDrivers()) { for (Long id : car.getDrivers()) {
if (!Objects.equals(id, driverId)) { if (!Objects.equals(id, driverId)) {
drivers.add(id); drivers.add(id);
...@@ -76,7 +80,9 @@ public class CarDriverPairServiceImpl implements CarDriverPairService { ...@@ -76,7 +80,9 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
public CarResponseDto setMainDriver(Long carId, Long driverId) { public CarResponseDto setMainDriver(Long carId, Long driverId) {
var car = carRepository.findById(carId).orElseThrow( var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found")); () -> new DatabaseException("Car not found"));
car.setMainDriverId(driverId);
var savedDriver = getDriverFromDb(driverId);
car.setMainDriverId(savedDriver.getId());
carRepository.save(car); carRepository.save(car);
return CarMapper.carConverterToDto(car); return CarMapper.carConverterToDto(car);
} }
......
...@@ -3,12 +3,15 @@ package cz.muni.pa165.car.service; ...@@ -3,12 +3,15 @@ package cz.muni.pa165.car.service;
import static cz.muni.pa165.car.mapper.CarMapper.carConverterToDto; import static cz.muni.pa165.car.mapper.CarMapper.carConverterToDto;
import static cz.muni.pa165.car.mapper.CarMapper.carDtoConverter; import static cz.muni.pa165.car.mapper.CarMapper.carDtoConverter;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.car.data.repository.CarRepository; import cz.muni.pa165.car.data.repository.CarRepository;
import cz.muni.pa165.car.mapper.CarMapper; import cz.muni.pa165.car.mapper.CarMapper;
import cz.muni.pa165.car.restemplate.DbGetter;
import cz.muni.pa165.common_library.dtos.CarRequestDto; import cz.muni.pa165.common_library.dtos.CarRequestDto;
import cz.muni.pa165.common_library.dtos.CarResponseDto; import cz.muni.pa165.common_library.dtos.CarResponseDto;
import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.common_library.exception.DatabaseException;
import java.util.List; import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -31,6 +34,14 @@ public class CarServiceImpl implements CarService { ...@@ -31,6 +34,14 @@ public class CarServiceImpl implements CarService {
@Override @Override
public CarResponseDto postCar(CarRequestDto carRequestDto) { public CarResponseDto postCar(CarRequestDto carRequestDto) {
var componentIds = carRequestDto.getComponentIds();
for (Long componentId: componentIds) {
if (isComponentInUse(componentId)) {
throw new DatabaseException("Component with id " + componentId + " already in use");
}
DbGetter.getComponentFromDb(componentId);
}
return carConverterToDto(carRepository.save(carDtoConverter(carRequestDto))); return carConverterToDto(carRepository.save(carDtoConverter(carRequestDto)));
} }
...@@ -58,4 +69,15 @@ public class CarServiceImpl implements CarService { ...@@ -58,4 +69,15 @@ public class CarServiceImpl implements CarService {
return "Car with id = " + carId + " deleted!"; return "Car with id = " + carId + " deleted!";
} }
private boolean isComponentInUse(Long componentId) {
var cars = carRepository.findAll();
for (Car car: cars) {
if (car.getComponents().stream()
.anyMatch(x -> x.longValue() == componentId)){
return true;
}
}
return false;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment