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

Merge branch '13_fixes_by_andrej' into 'milestone_2'

13 fixes by andrej

See merge request !37
parents 8b9622df 4b96a79c
No related branches found
No related tags found
2 merge requests!60Docker,!3713 fixes by andrej
Pipeline #
Showing
with 30 additions and 205 deletions
package cz.muni.pa165.car.data.model; package cz.muni.pa165.car.data.model;
import cz.muni.pa165.driver.data.model.Driver;
import cz.muni.pa165.component.data.model.CarComponent; import cz.muni.pa165.component.data.model.CarComponent;
import cz.muni.pa165.driver.data.model.Driver;
import jakarta.persistence.CascadeType; import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.FetchType; import jakarta.persistence.FetchType;
...@@ -36,11 +36,11 @@ public class Car implements Serializable { ...@@ -36,11 +36,11 @@ public class Car implements Serializable {
private Long id; private Long id;
@OneToMany(mappedBy = "id", @OneToMany(mappedBy = "id",
fetch = FetchType.EAGER) fetch = FetchType.EAGER)
private List<@Valid CarComponent> components; private List<@Valid CarComponent> components;
@OneToMany(mappedBy = "id", @OneToMany(mappedBy = "id",
fetch = FetchType.EAGER) fetch = FetchType.EAGER)
private List<@Valid Driver> drivers; private List<@Valid Driver> drivers;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
......
package cz.muni.pa165.car.service; package cz.muni.pa165.car.service;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.car.data.repository.CarComponentRepository; import cz.muni.pa165.car.data.repository.CarComponentRepository;
import cz.muni.pa165.car.data.repository.CarRepository; import cz.muni.pa165.car.data.repository.CarRepository;
import cz.muni.pa165.car.data.repository.DriverRepository; import cz.muni.pa165.car.data.repository.DriverRepository;
import cz.muni.pa165.common_library.dtos.CarDto; import cz.muni.pa165.common_library.dtos.CarDto;
import cz.muni.pa165.common_library.exception.DatabaseException; import cz.muni.pa165.common_library.exception.DatabaseException;
import cz.muni.pa165.car.data.model.Car;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.data.util.Pair; import org.springframework.data.util.Pair;
...@@ -24,9 +24,9 @@ public class CarServiceImpl implements CarService { ...@@ -24,9 +24,9 @@ public class CarServiceImpl implements CarService {
/** /**
* Constructor for Car Service. * Constructor for Car Service.
* *
* @param carRepository Car repository * @param carRepository Car repository
* @param componentRepository Component repository * @param componentRepository Component repository
* @param driverRepository Driver repository * @param driverRepository Driver repository
*/ */
public CarServiceImpl(CarRepository carRepository, public CarServiceImpl(CarRepository carRepository,
CarComponentRepository componentRepository, CarComponentRepository componentRepository,
...@@ -66,12 +66,12 @@ public class CarServiceImpl implements CarService { ...@@ -66,12 +66,12 @@ public class CarServiceImpl implements CarService {
} }
/** /**
* Converts Dto object to Car object. * Converts Dto object to Car object.
* TODO: Change to the correct version of converter. * TODO: Change to the correct version of converter.
* *
* @param carDto Dto object of the car. * @param carDto Dto object of the car.
* @return Car object. * @return Car object.
*/ */
public static Car carDtoConverter(CarDto carDto) { public static Car carDtoConverter(CarDto carDto) {
// TEST VERSION WHEN COMPONENTS AND DRIVERS DO NOT EXIST // TEST VERSION WHEN COMPONENTS AND DRIVERS DO NOT EXIST
...@@ -109,11 +109,11 @@ public class CarServiceImpl implements CarService { ...@@ -109,11 +109,11 @@ public class CarServiceImpl implements CarService {
} }
/** /**
* Converts Car object to its Dto. * Converts Car object to its Dto.
* *
* @param car Car object. * @param car Car object.
* @return Dto object. * @return Dto object.
*/ */
public static CarDto carConverterToDto(Car car) { public static CarDto carConverterToDto(Car car) {
Long id = null; Long id = null;
if (car.getMainDriver() != null) { if (car.getMainDriver() != null) {
......
...@@ -18,7 +18,6 @@ public record DriverInsightDto(@NotNull Long id, ...@@ -18,7 +18,6 @@ public record DriverInsightDto(@NotNull Long id,
"Aggressiveness", "Aggressiveness",
"Consistency", "Consistency",
"Racecraft"}) "Racecraft"})
Map<String, Integer> characteristics, Map<String, Integer> characteristics) {
@Nullable Long carId) {
} }
package cz.muni.pa165.common_library.dtos; package cz.muni.pa165.common_library.dtos;
import cz.muni.pa165.common_library.racecomponents.Location;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
......
No preview for this file type
package cz.muni.pa165.driver.data.model; package cz.muni.pa165.driver.data.model;
import cz.muni.pa165.car.data.model.Car;
import jakarta.persistence.CascadeType;
import jakarta.persistence.CollectionTable; import jakarta.persistence.CollectionTable;
import jakarta.persistence.ElementCollection; import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
...@@ -9,8 +7,6 @@ import jakarta.persistence.FetchType; ...@@ -9,8 +7,6 @@ import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapKeyColumn; import jakarta.persistence.MapKeyColumn;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Max;
......
package cz.muni.pa165.driver.data.repository;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.driver.data.model.Driver;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
* Repository for car.
*/
@Repository
public interface CarRepository extends JpaRepository<Car, Long> {
@Query("UPDATE Car c SET c.mainDriver = ?1 WHERE c.id = ?2")
int setMainDriverOfCar(Driver driver, Long carId);
}
package cz.muni.pa165.driver.mapper;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.common_library.dtos.DriverCarDto;
import cz.muni.pa165.driver.data.model.Driver;
/**
* Interface of driver car mapper.
*/
public interface DriverCarMapper {
/**
* Maps driver and car objects to driver car response dto.
*
* @param driver driver class
* @param car car class
* @return driver car response dto
*/
DriverCarDto convertToDto(Driver driver, Car car);
}
package cz.muni.pa165.driver.mapper;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.common_library.dtos.DriverCarDto;
import cz.muni.pa165.driver.data.model.Driver;
import org.springframework.stereotype.Component;
/**
* Implementation of driver car mapper.
*/
@Component
public class DriverCarMapperImpl implements DriverCarMapper {
/**
* Maps driver and car objects to driver car response dto.
*
* @param driver driver class
* @param car car class
* @return driver car response dto
*/
@Override
public DriverCarDto convertToDto(Driver driver, Car car) {
return new DriverCarDto(
driver.getId(),
car.getId());
}
}
...@@ -36,8 +36,7 @@ public class DriverMapperImpl implements DriverMapper { ...@@ -36,8 +36,7 @@ public class DriverMapperImpl implements DriverMapper {
driver.getName(), driver.getName(),
driver.getSurname(), driver.getSurname(),
driver.getNationality(), driver.getNationality(),
driver.getCharacteristics(), driver.getCharacteristics());
driver.getCar() == null ? null : driver.getCar().getId());
} }
/** /**
......
package cz.muni.pa165.driver.rest; package cz.muni.pa165.driver.rest;
import cz.muni.pa165.common_library.dtos.DriverAddDto; import cz.muni.pa165.common_library.dtos.DriverAddDto;
import cz.muni.pa165.common_library.dtos.DriverCarDto;
import cz.muni.pa165.common_library.dtos.DriverInsightDto; import cz.muni.pa165.common_library.dtos.DriverInsightDto;
import cz.muni.pa165.common_library.dtos.DriverResponseDto; import cz.muni.pa165.common_library.dtos.DriverResponseDto;
import cz.muni.pa165.common_library.dtos.DriverUpdateDto; import cz.muni.pa165.common_library.dtos.DriverUpdateDto;
import cz.muni.pa165.driver.service.CarService;
import cz.muni.pa165.driver.service.DriverService; import cz.muni.pa165.driver.service.DriverService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -32,19 +31,25 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -32,19 +31,25 @@ import org.springframework.web.bind.annotation.RestController;
public class DriverController { public class DriverController {
private final DriverService driverService; private final DriverService driverService;
private final CarService carService;
@Autowired @Autowired
public DriverController(DriverService driverService, CarService carService) { public DriverController(DriverService driverService) {
this.driverService = driverService; this.driverService = driverService;
this.carService = carService;
} }
@Operation(summary = "Add a specific driver to the team") @Operation(summary = "Add a specific driver to the team")
@PostMapping(path = "/add", consumes = MediaType.APPLICATION_JSON_VALUE, @PostMapping(path = "/add", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE) produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DriverResponseDto> addDriver( public ResponseEntity<DriverResponseDto> addDriver(
@Valid @RequestBody DriverAddDto driverAddDto) { @Valid
@RequestBody
@Schema(example = "{\"name\":\"Fernando\", "
+ "\"surname\": \"Alonso\", "
+ "\"nationality\": \"Spanish\", "
+ "\"characteristics\" : {\"Experience\" : 1,"
+ " \"Aggressiveness\" : 1,"
+ " \"Consistency\" : 1,"
+ " \"Racecraft\" : 1 }}") DriverAddDto driverAddDto) {
return ResponseEntity.ok(driverService.addDriver(driverAddDto)); return ResponseEntity.ok(driverService.addDriver(driverAddDto));
} }
...@@ -56,20 +61,6 @@ public class DriverController { ...@@ -56,20 +61,6 @@ public class DriverController {
return ResponseEntity.ok(driverService.updateDriverById(id, driverUpdateDto)); return ResponseEntity.ok(driverService.updateDriverById(id, driverUpdateDto));
} }
@Operation(summary = "Makes a driver specified by his id a main driver of its car")
@PutMapping(path = "/set_main/id={id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DriverCarDto> setAsMainDriver(@PathVariable("id") Long id) {
return ResponseEntity.ok(carService.setAsMainDriver(id));
}
@Operation(summary = "Assign driver to a car.")
@PutMapping(path = "/assign/driverId={did}/carId={cid}",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DriverCarDto> assignDriverToCar(@PathVariable("did") Long did,
@PathVariable("cid") Long cid) {
return ResponseEntity.ok(carService.assignDriverToCar(did, cid));
}
@Operation(summary = "Dismiss a driver specified by his id from the team") @Operation(summary = "Dismiss a driver specified by his id from the team")
@DeleteMapping(path = "/remove/id={id}", produces = MediaType.APPLICATION_JSON_VALUE) @DeleteMapping(path = "/remove/id={id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DriverResponseDto> removeDriverById(@PathVariable("id") Long id) { public ResponseEntity<DriverResponseDto> removeDriverById(@PathVariable("id") Long id) {
......
package cz.muni.pa165.driver.service;
import cz.muni.pa165.common_library.dtos.DriverCarDto;
/**
* Car service interface.
*/
public interface CarService {
/**
* Sets given driver as main driver of his corresponding car.
*
* @param driverId driver id
* @return dto of driver and car
*/
DriverCarDto setAsMainDriver(Long driverId);
/**
* Assign driver to a car.
*
* @param driverId driver id
* @param carId car id
* @return driver car dto response.
*/
DriverCarDto assignDriverToCar(Long driverId, Long carId);
}
package cz.muni.pa165.driver.service;
import cz.muni.pa165.car.data.model.Car;
import cz.muni.pa165.common_library.dtos.DriverCarDto;
import cz.muni.pa165.common_library.exception.ResourceNotFoundException;
import cz.muni.pa165.driver.data.repository.CarRepository;
import cz.muni.pa165.driver.data.repository.DriverRepository;
import cz.muni.pa165.driver.mapper.DriverCarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Implementation of car service.
*/
@Service
public class CarServiceImpl implements CarService {
private final DriverRepository driverRepository;
private final CarRepository carRepository;
private final DriverCarMapper driverCarMapper;
@Autowired
CarServiceImpl(DriverRepository driverRepository, CarRepository carRepository,
DriverCarMapper driverCarMapper) {
this.driverRepository = driverRepository;
this.carRepository = carRepository;
this.driverCarMapper = driverCarMapper;
}
/**
* Sets given driver as main driver of his corresponding car.
*
* @param driverId driver id
* @return dto of driver and car
*/
@Override
public DriverCarDto setAsMainDriver(Long driverId) {
var driverDb = driverRepository.findById(driverId)
.orElseThrow(() -> new ResourceNotFoundException("Driver with given id not found."));
var carDb = carRepository.findById(driverDb.getCar().getId())
.orElseThrow(() -> new ResourceNotFoundException("Car with given id not found."));
var rowsUpdated = carRepository.setMainDriverOfCar(driverDb, carDb.getId());
if (rowsUpdated == 1) {
return driverCarMapper.convertToDto(driverDb, carDb);
} else {
throw new ResourceNotFoundException("Database integrity violation.");
}
}
/**
* Assign driver to a car.
*
* @param driverId driver id
* @param carId car id
* @return driver car dto response.
*/
@Override
public DriverCarDto assignDriverToCar(Long driverId, Long carId) {
var car = carRepository.findById(carId)
.orElseThrow(() -> new ResourceNotFoundException("Car not found."));
var driver = driverRepository.findById(driverId)
.orElseThrow(() -> new ResourceNotFoundException("Driver not found."));
car.getDrivers().add(driver);
carRepository.save(car);
return driverCarMapper.convertToDto(driver, car);
}
}
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