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;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
......@@ -30,10 +30,10 @@ public class Car implements Serializable {
private Long id;
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
private List<Long> components;
private Set<Long> components;
@ElementCollection(targetClass = Long.class, fetch = FetchType.EAGER)
private List<Long> drivers;
private Set<Long> drivers;
private Long mainDriverId;
......
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.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.HashSet;
/**
* Class for converting Car object and its Data Transfer Objects.
......@@ -19,8 +25,8 @@ public class CarMapper {
return Car.builder()
.id(null)
.components(carRequestDto.getComponentIds())
.drivers(carRequestDto.getDriverIds())
.components(new HashSet<>(carRequestDto.getComponentIds()))
.drivers(new HashSet<>(carRequestDto.getDriverIds()))
.mainDriverId(carRequestDto.getMainDriverId())
.build();
}
......@@ -35,10 +41,11 @@ public class CarMapper {
return CarResponseDto.builder()
.id(car.getId())
.componentIdsNames(car.getComponents())
.driverIdsNames(car.getDrivers())
.componentIdsNames(new ArrayList<>(car.getComponents()))
.driverIdsNames(new ArrayList<>(car.getDrivers()))
.mainDriverId(car.getMainDriverId())
.build();
}
}
......@@ -13,11 +13,10 @@ import org.springframework.web.client.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=";
private static final String GET_COMPONENT_URL = "http://localhost:8084/component/id?componentId=";
/**
* Get a driver using RestTemplate client.
......
......@@ -2,6 +2,7 @@ package cz.muni.pa165.car.service;
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.mapper.CarMapper;
import cz.muni.pa165.common_library.dtos.CarComponentDto;
......@@ -9,6 +10,7 @@ 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;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Service;
......@@ -34,8 +36,13 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
public CarResponseDto addComponent(Long componentId, Long carId) {
var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found"));
if (isComponentInUse(componentId, carId)) {
throw new DatabaseException("Component with id " + componentId + " already in use");
}
var components = car.getComponents();
components.add(componentId);
CarComponent carComponent = getComponentFromDb(componentId);
components.add(carComponent.getId());
car.setComponents(components);
carRepository.save(car);
return CarMapper.carConverterToDto(car);
......@@ -45,7 +52,7 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
public CarResponseDto removeComponent(Long componentId, Long carId) {
var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found"));
var components = new ArrayList<Long>();
var components = new HashSet<Long>();
for (Long id : car.getComponents()) {
if (!Objects.equals(id, componentId)) {
components.add(id);
......@@ -76,4 +83,15 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
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;
import cz.muni.pa165.common_library.exception.DatabaseException;
import cz.muni.pa165.driver.data.model.Driver;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Service;
......@@ -34,9 +35,12 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
public CarResponseDto assignDriverToCar(Long driverId, Long carId) {
var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found"));
var drivers = car.getDrivers();
drivers.add(driverId);
var savedDriver = getDriverFromDb(driverId);
drivers.add(savedDriver.getId());
car.setDrivers(drivers);
carRepository.save(car);
return CarMapper.carConverterToDto(car);
}
......@@ -45,7 +49,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
public CarResponseDto unassignDriverFromCar(Long driverId, Long carId) {
var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found"));
var drivers = new ArrayList<Long>();
var drivers = new HashSet<Long>();
for (Long id : car.getDrivers()) {
if (!Objects.equals(id, driverId)) {
drivers.add(id);
......@@ -76,7 +80,9 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
public CarResponseDto setMainDriver(Long carId, Long driverId) {
var car = carRepository.findById(carId).orElseThrow(
() -> new DatabaseException("Car not found"));
car.setMainDriverId(driverId);
var savedDriver = getDriverFromDb(driverId);
car.setMainDriverId(savedDriver.getId());
carRepository.save(car);
return CarMapper.carConverterToDto(car);
}
......
......@@ -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.carDtoConverter;
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.car.restemplate.DbGetter;
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.List;
import java.util.Objects;
import org.springframework.stereotype.Service;
......@@ -31,6 +34,14 @@ public class CarServiceImpl implements CarService {
@Override
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)));
}
......@@ -58,4 +69,15 @@ public class CarServiceImpl implements CarService {
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