Skip to content
Snippets Groups Projects
Commit c5bfc4ad authored by Andrej Šimurka's avatar Andrej Šimurka
Browse files

Finished driver module logic, Updated Car and Driver models

parent 26a0de8c
No related branches found
No related tags found
2 merge requests!60Docker,!2513 fixes by andrej
Pipeline #
Showing
with 236 additions and 209 deletions
......@@ -30,7 +30,6 @@ public class CarRepository {
driver.setName("Max");
driver.setSurname("Verstappen");
driver.setNationality("Dutch");
driver.setMain(true);
car.setDrivers(List.of(driver));
return Optional.of(car);
}
......
......@@ -9,6 +9,7 @@ import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
......@@ -43,6 +44,8 @@ public class Car implements Serializable {
cascade = {CascadeType.REMOVE, CascadeType.PERSIST, CascadeType.MERGE})
private List<@Valid Driver> drivers;
@NotNull
private Long mainDriverId;
}
......@@ -42,6 +42,6 @@ public class Driver implements Serializable {
private Set<Characteristic> characteristics;
@NotNull
private boolean isMain;
private Long carId;
}
package cz.muni.pa165.driver.api;
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;
/**
* Dto for Car entity.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CarDto {
@NotNull
@Schema(description = "car id", example = "1")
long id;
@NotNull
@Schema(description = "car's components ids and names",
example = "[]")
List<Pair<Long, String>> componentIdsNames;
@NotNull
@Schema(description = "car's drivers ids and names",
example = "[]")
List<Pair<Long, String>> driverIdsNames;
}
package cz.muni.pa165.driver.api;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
/**
* Dto for DriverCar entity.
* Dto for DriverCar response.
*/
@Data
@Builder
public class DriverCarDto {
@NotNull
@Schema(description = "driver id", example = "1")
Long driverId;
@NotNull
@Schema(description = "car id", example = "1")
Long carId;
@Schema(description = "driver name", example = "1")
String driverName;
public record DriverCarDto(Long driverId,
Long carId) {
}
package cz.muni.pa165.driver.api;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Dto for Driver entity.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DriverDto {
@NotNull
@Schema(description = "driver id", example = "1")
long id;
@NotNull
@Schema(description = "driver name", example = "Max")
String name;
@NotNull
@Schema(description = "driver surname", example = "Verstappen")
String surname;
}
package cz.muni.pa165.driver.api;
import cz.muni.pa165.common_library.racecomponents.Characteristic;
import java.util.Set;
/**
* Dto for Driver insight.
*/
public record DriverInsightDto(Long id,
String name,
String surname,
String nationality,
Set<Characteristic> characteristics,
Long carId) {
}
package cz.muni.pa165.driver.api;
/**
* Dto for driver response.
*/
public record DriverResponseDto(Long id) {
}
package cz.muni.pa165.driver.api;
import cz.muni.pa165.common_library.racecomponents.Characteristic;
import java.util.Set;
/**
* Dto for updating driver.
*/
public record DriverUpdateDto(String name,
String surname,
String nationality,
Set<Characteristic> characteristics) {
}
package cz.muni.pa165.driver.data.repository;
import cz.muni.pa165.common_library.racecomponents.CarComponent;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* Repository for car config.
*/
@Repository
public interface CarComponentRepository extends JpaRepository<CarComponent, Long> {
}
......@@ -2,6 +2,7 @@ package cz.muni.pa165.driver.data.repository;
import cz.muni.pa165.common_library.racecomponents.Car;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
......@@ -10,4 +11,6 @@ import org.springframework.stereotype.Repository;
@Repository
public interface CarRepository extends JpaRepository<Car, Long> {
@Query("UPDATE Car c SET c.mainDriverId = ?1 WHERE c.id = ?2")
int setMainDriverOfCar(Long driverId, Long carId);
}
package cz.muni.pa165.driver.data.repository;
import cz.muni.pa165.common_library.racecomponents.Driver;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
......@@ -12,14 +10,4 @@ import org.springframework.stereotype.Repository;
@Repository
public interface DriverRepository extends JpaRepository<Driver, Long> {
default List<Driver> getAllMainDrivers() {
return getAllDriversByRank(true);
}
default List<Driver> getAllTestDrivers() {
return getAllDriversByRank(false);
}
@Query("SELECT d FROM Driver d WHERE d.isMain = ?1")
List<Driver> getAllDriversByRank(boolean isMain);
}
package cz.muni.pa165.driver.mapper;
import cz.muni.pa165.common_library.racecomponents.Car;
import cz.muni.pa165.common_library.racecomponents.Driver;
import cz.muni.pa165.driver.api.DriverCarDto;
/**
* 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.common_library.racecomponents.Car;
import cz.muni.pa165.common_library.racecomponents.Driver;
import cz.muni.pa165.driver.api.DriverCarDto;
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());
}
}
package cz.muni.pa165.driver.mapper;
import cz.muni.pa165.common_library.racecomponents.Driver;
import cz.muni.pa165.driver.api.DriverInsightDto;
import cz.muni.pa165.driver.api.DriverResponseDto;
import cz.muni.pa165.driver.api.DriverUpdateDto;
/**
* Interface for driver mapper.
*/
public interface DriverMapper {
/**
* Maps driver class to driver response dto.
*
* @param driver driver class
* @return driver response dto
*/
DriverResponseDto convertToResponseDto(Driver driver);
/**
* Maps driver class to driver insight dto.
*
* @param driver driver class
* @return driver insight dto
*/
DriverInsightDto convertToInsightDto(Driver driver);
/**
* Maps driver dto to driver class.
*
* @param driverUpdateDto driver dto
* @return driver class
*/
Driver convertToDriver(DriverUpdateDto driverUpdateDto);
}
package cz.muni.pa165.driver.mapper;
import cz.muni.pa165.common_library.racecomponents.Driver;
import cz.muni.pa165.driver.api.DriverInsightDto;
import cz.muni.pa165.driver.api.DriverResponseDto;
import cz.muni.pa165.driver.api.DriverUpdateDto;
import org.springframework.stereotype.Component;
/**
* Implementation of driver mapper.
*/
@Component
public class DriverMapperImpl implements DriverMapper {
/**
* Maps driver class to driver response dto.
*
* @param driver driver class
* @return driver response dto
*/
@Override
public DriverResponseDto convertToResponseDto(Driver driver) {
return new DriverResponseDto(driver.getId());
}
/**
* Maps driver class to driver insight dto.
*
* @param driver driver class
* @return driver insight dto
*/
@Override
public DriverInsightDto convertToInsightDto(Driver driver) {
return new DriverInsightDto(
driver.getId(),
driver.getName(),
driver.getSurname(),
driver.getNationality(),
driver.getCharacteristics(),
driver.getCarId());
}
/**
* Maps driver dto to driver class.
*
* @param driverUpdateDto driver dto
* @return driver class
*/
@Override
public Driver convertToDriver(DriverUpdateDto driverUpdateDto) {
return Driver.builder()
.name(driverUpdateDto.name())
.surname(driverUpdateDto.surname())
.nationality(driverUpdateDto.nationality())
.characteristics(driverUpdateDto.characteristics())
.build();
}
}
package cz.muni.pa165.driver.rest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Rest controller for car.
*/
@RestController
@RequestMapping(path = "/car")
@Validated
public class CarController {
}
package cz.muni.pa165.driver.rest;
import cz.muni.pa165.common_library.racecomponents.Driver;
import cz.muni.pa165.driver.api.DriverDto;
import cz.muni.pa165.driver.api.DriverCarDto;
import cz.muni.pa165.driver.api.DriverInsightDto;
import cz.muni.pa165.driver.api.DriverResponseDto;
import cz.muni.pa165.driver.api.DriverUpdateDto;
import cz.muni.pa165.driver.service.CarService;
import cz.muni.pa165.driver.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
......@@ -28,46 +31,54 @@ import org.springframework.web.bind.annotation.RestController;
public class DriverController {
private final DriverService driverService;
private final CarService carService;
@Autowired
public DriverController(DriverService driverService) {
public DriverController(DriverService driverService, CarService carService) {
this.driverService = driverService;
this.carService = carService;
}
@Operation(summary = "Sign a specific driver to the team")
@PostMapping(path = "/add", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Driver> addDriver(@Valid @RequestBody DriverDto driver) {
return ResponseEntity.ok(driverService.addDriver(driver));
public ResponseEntity<DriverResponseDto> addDriver(
@Valid @RequestBody DriverUpdateDto driverUpdateDto) {
return ResponseEntity.ok(driverService.addDriver(driverUpdateDto));
}
@Operation(summary = "Updates a driver specified by his id")
@PutMapping(path = "/update/id={id}", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Driver> updateDriverById(@PathVariable("id") Long id,
@Valid @RequestBody DriverDto driver) {
return ResponseEntity.ok(driverService.updateDriverById(id, driver));
public ResponseEntity<DriverResponseDto> updateDriverById(
@PathVariable("id") Long id, @Valid @RequestBody DriverUpdateDto 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 = "Dismiss a driver specified by his id from the team")
@DeleteMapping(path = "/remove/id={id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Driver> removeDriverById(@PathVariable("id") Long id) {
public ResponseEntity<DriverResponseDto> removeDriverById(@PathVariable("id") Long id) {
return ResponseEntity.ok(driverService.removeDriverById(id));
}
@Operation(summary = "Get all the signed driver of the team")
@GetMapping(path = "/get/all", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<DriverDto>> getAllDrivers() {
public ResponseEntity<List<DriverInsightDto>> getAllDrivers() {
return ResponseEntity.ok(driverService.getAllDrivers());
}
@Operation(summary = "Get a signed driver specified by his id")
@GetMapping(path = "/get/id={id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<DriverDto> getDriverById(@PathVariable("id") Long id) {
public ResponseEntity<DriverInsightDto> getDriverById(@PathVariable("id") Long id) {
return ResponseEntity.ok(driverService.getDriverById(id));
}
}
package cz.muni.pa165.driver.service;
import cz.muni.pa165.common_library.racecomponents.Car;
import cz.muni.pa165.driver.api.CarDto;
import cz.muni.pa165.driver.api.DriverCarDto;
/**
* Car service interface.
......@@ -9,18 +8,12 @@ import cz.muni.pa165.driver.api.CarDto;
public interface CarService {
/**
* Converts a car dto into car class.
* Sets given driver as main driver of his corresponding car.
*
* @param carDto car dto object
* @return car class object
* @param driverId driver id
* @return dto of driver and car
*/
Car convertDtoToCar(CarDto carDto);
/**
* Converts a car class object to car dto object.
*
* @param car car class object
* @return car dto object
*/
CarDto convertCarToDto(Car car);
DriverCarDto setAsMainDriver(Long driverId);
}
package cz.muni.pa165.driver.service;
import cz.muni.pa165.common_library.exceptions.DatabaseException;
import cz.muni.pa165.common_library.racecomponents.Car;
import cz.muni.pa165.common_library.racecomponents.CarComponent;
import cz.muni.pa165.common_library.racecomponents.Driver;
import cz.muni.pa165.driver.api.CarDto;
import cz.muni.pa165.driver.data.repository.CarComponentRepository;
import cz.muni.pa165.common_library.exceptions.ResourceNotFoundException;
import cz.muni.pa165.driver.api.DriverCarDto;
import cz.muni.pa165.driver.data.repository.CarRepository;
import cz.muni.pa165.driver.data.repository.DriverRepository;
import java.util.ArrayList;
import cz.muni.pa165.driver.mapper.DriverCarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;
/**
......@@ -21,64 +16,33 @@ public class CarServiceImpl implements CarService {
private final DriverRepository driverRepository;
private final CarRepository carRepository;
private final CarComponentRepository carComponentRepository;
private final DriverCarMapper driverCarMapper;
@Autowired
CarServiceImpl(DriverRepository driverRepository, CarRepository carRepository,
CarComponentRepository carComponentRepository) {
DriverCarMapper driverCarMapper) {
this.driverRepository = driverRepository;
this.carRepository = carRepository;
this.carComponentRepository = carComponentRepository;
this.driverCarMapper = driverCarMapper;
}
/**
* Converts a car dto into car class.
* Sets given driver as main driver of his corresponding car.
*
* @param carDto car dto object
* @return car class object
* @param driverId driver id
* @return dto of driver and car
*/
public Car convertDtoToCar(CarDto carDto) {
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(carComponentRepository.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")
));
@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.getCarId())
.orElseThrow(() -> new ResourceNotFoundException("Car with given id not found."));
var rowsUpdated = carRepository.setMainDriverOfCar(driverId, carDb.getId());
if (rowsUpdated == 1) {
return driverCarMapper.convertToDto(driverDb, carDb);
} else {
throw new ResourceNotFoundException("Database integrity violation.");
}
car.setDrivers(drivers);
return car;
}
/**
* Converts a car class object to car dto object.
*
* @param car car class object
* @return car dto object
*/
public CarDto convertCarToDto(Car car) {
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())
.build();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment