From 05b5854ca4d6b27f98bdbd43d0bd0d240fa73fa0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Diana=20Gul=C4=8D=C3=ADkov=C3=A1?= <xgulcik@fi.muni.cz>
Date: Sun, 16 Apr 2023 22:12:37 +0200
Subject: [PATCH] Added Transactional

---
 .../car/service/CarComponentPairServiceImpl.java     |  4 ++++
 .../pa165/car/service/CarDriverPairServiceImpl.java  |  6 ++++++
 .../cz/muni/pa165/car/service/CarServiceImpl.java    |  5 +++++
 .../pa165/component/service/ComponentService.java    |  4 ++++
 .../muni/pa165/driver/service/DriverServiceImpl.java |  7 +++++++
 .../java/cz/muni/pa165/race/rest/RaceController.java |  2 +-
 .../java/cz/muni/pa165/race/service/RaceService.java | 12 +++++++++++-
 .../cz/muni/pa165/race/service/SeasonService.java    |  6 ++++++
 8 files changed, 44 insertions(+), 2 deletions(-)

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 6bfab5d0..7f3c6f15 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
@@ -13,6 +13,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * Service for manipulation with car's components.
@@ -32,6 +33,7 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
   }
 
   @Override
+  @Transactional
   public CarResponseDto addComponent(Long componentId, Long carId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
@@ -48,6 +50,7 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
   }
 
   @Override
+  @Transactional
   public CarResponseDto removeComponent(Long componentId, Long carId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
@@ -63,6 +66,7 @@ public class CarComponentPairServiceImpl implements CarComponentPairService {
   }
 
   @Override
+  @Transactional(readOnly = true)
   public List<CarComponentResponseDto> getAllComponentsOfCar(Long carId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
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 543d9dc4..69a346e7 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
@@ -13,6 +13,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Objects;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * Service for Driver Manager.
@@ -32,6 +33,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
   }
 
   @Override
+  @Transactional
   public CarResponseDto assignDriverToCar(Long driverId, Long carId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
@@ -46,6 +48,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
   }
 
   @Override
+  @Transactional
   public CarResponseDto unassignDriverFromCar(Long driverId, Long carId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
@@ -61,6 +64,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
   }
 
   @Override
+  @Transactional(readOnly = true)
   public List<DriverDto> getAllDriversOfCar(Long carId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
@@ -77,6 +81,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
   }
 
   @Override
+  @Transactional
   public CarResponseDto setMainDriver(Long carId, Long driverId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
@@ -92,6 +97,7 @@ public class CarDriverPairServiceImpl implements CarDriverPairService {
   }
 
   @Override
+  @Transactional
   public CarResponseDto removeMainDriver(Long carId) {
     var car = carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException("Car not found"));
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 38f8d647..e9d2921a 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
@@ -13,6 +13,7 @@ import cz.muni.pa165.common_library.exception.DatabaseException;
 import java.util.List;
 import java.util.Objects;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 
 /**
@@ -33,6 +34,7 @@ public class CarServiceImpl implements CarService {
   }
 
   @Override
+  @Transactional
   public CarResponseDto postCar(CarRequestDto carRequestDto) {
 
     var componentIds = carRequestDto.getComponentIds();
@@ -46,6 +48,7 @@ public class CarServiceImpl implements CarService {
   }
 
   @Override
+  @Transactional(readOnly = true)
   public CarResponseDto getCarById(Long carId) {
     return carConverterToDto(carRepository.findById(carId).orElseThrow(
         () -> new DatabaseException(
@@ -55,6 +58,7 @@ public class CarServiceImpl implements CarService {
   }
 
   @Override
+  @Transactional(readOnly = true)
   public List<CarResponseDto> getAllCars() {
     return carRepository
         .findAll()
@@ -64,6 +68,7 @@ public class CarServiceImpl implements CarService {
   }
 
   @Override
+  @Transactional
   public String deleteById(Long carId) {
     carRepository.deleteById(carId);
     return "Car with id = " + carId + " deleted!";
diff --git a/component/src/main/java/cz/muni/pa165/component/service/ComponentService.java b/component/src/main/java/cz/muni/pa165/component/service/ComponentService.java
index bc08b230..58042fa2 100644
--- a/component/src/main/java/cz/muni/pa165/component/service/ComponentService.java
+++ b/component/src/main/java/cz/muni/pa165/component/service/ComponentService.java
@@ -8,6 +8,7 @@ import cz.muni.pa165.component.data.repository.ComponentRepositoryInterface;
 import java.util.List;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 
 /**
@@ -34,6 +35,7 @@ public class ComponentService implements ComponentServiceInterface {
    * @param carComponentId id of the component.
    * @return found car component.
    */
+  @Transactional(readOnly = true)
   public CarComponentResponseDto getCarComponentById(Long carComponentId) {
     return carComponentConverter(componentRepository.findById(carComponentId).orElseThrow(
         () -> new DatabaseException("Something went wrong when finding car component with id: "
@@ -45,6 +47,7 @@ public class ComponentService implements ComponentServiceInterface {
    *
    * @return list of stored car components.
    */
+  @Transactional(readOnly = true)
   public List<CarComponentResponseDto> getAllCarComponents() {
     return componentRepository.findAll()
         .stream()
@@ -57,6 +60,7 @@ public class ComponentService implements ComponentServiceInterface {
    *
    * @param carComponentId of the car component for removal.
    */
+  @Transactional
   public String deleteById(Long carComponentId) {
     componentRepository.deleteById(carComponentId);
     return "Car component with id: " + carComponentId + "was successfully deleted";
diff --git a/driver/src/main/java/cz/muni/pa165/driver/service/DriverServiceImpl.java b/driver/src/main/java/cz/muni/pa165/driver/service/DriverServiceImpl.java
index db4a956e..3aa0f458 100644
--- a/driver/src/main/java/cz/muni/pa165/driver/service/DriverServiceImpl.java
+++ b/driver/src/main/java/cz/muni/pa165/driver/service/DriverServiceImpl.java
@@ -12,6 +12,7 @@ import java.util.List;
 import java.util.stream.Collectors;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * Implementation of driver service.
@@ -34,6 +35,7 @@ public class DriverServiceImpl implements DriverService {
    * @param driverAddDto driver dto object
    * @return dto of added driver
    */
+  @Transactional
   public DriverResponseDto addDriver(DriverAddDto driverAddDto) {
     var driver = driverMapper.convertToDriver(driverAddDto);
     return driverMapper.convertToResponseDto(driverRepository.save(driver));
@@ -46,6 +48,7 @@ public class DriverServiceImpl implements DriverService {
    * @param driverUpdateDto data to be updated
    * @return dto of updated driver
    */
+  @Transactional
   public DriverResponseDto updateDriverById(Long id, DriverUpdateDto driverUpdateDto) {
     var driver = driverRepository.findById(id)
         .orElseThrow(() -> new ResourceNotFoundException("Driver with given id not found."));
@@ -59,6 +62,7 @@ public class DriverServiceImpl implements DriverService {
    * @param driver          existing driver
    * @param driverUpdateDto driver update dto
    */
+  @Transactional
   private static void updateDriverAttributes(Driver driver, DriverUpdateDto driverUpdateDto) {
     if (driverUpdateDto.name() != null) {
       driver.setName(driverUpdateDto.name());
@@ -80,6 +84,7 @@ public class DriverServiceImpl implements DriverService {
    * @param id driver id
    * @return dto of removed driver
    */
+  @Transactional
   public DriverResponseDto removeDriverById(Long id) {
     var found = driverRepository.findById(id)
         .orElseThrow(() -> new ResourceNotFoundException(
@@ -93,6 +98,7 @@ public class DriverServiceImpl implements DriverService {
    *
    * @return all stored drivers dto list
    */
+  @Transactional(readOnly = true)
   public List<DriverInsightDto> getAllDrivers() {
     System.out.println();
     return driverRepository.findAll()
@@ -107,6 +113,7 @@ public class DriverServiceImpl implements DriverService {
    * @param id driver id
    * @return found driver dto if successful
    */
+  @Transactional(readOnly = true)
   public DriverInsightDto getDriverById(Long id) {
     return driverMapper.convertToInsightDto(driverRepository.findById(id)
         .orElseThrow(() -> new ResourceNotFoundException(
diff --git a/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java b/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java
index 907c1066..d9ef0981 100644
--- a/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java
+++ b/race/src/main/java/cz/muni/pa165/race/rest/RaceController.java
@@ -88,7 +88,7 @@ public class RaceController {
     return ResponseEntity.ok(raceService.assignPositionForDriverTwo(raceId, position));
   }
 
-  @Operation(summary = "Assign position for driver two")
+  @Operation(summary = "Assign position for driver one")
   @PatchMapping(path = "/assignPointsDriverOne",
       produces = MediaType.APPLICATION_JSON_VALUE)
   public ResponseEntity<RaceDto> assignPositionDriverOne(@RequestParam Long raceId,
diff --git a/race/src/main/java/cz/muni/pa165/race/service/RaceService.java b/race/src/main/java/cz/muni/pa165/race/service/RaceService.java
index 48797427..ad0ff086 100644
--- a/race/src/main/java/cz/muni/pa165/race/service/RaceService.java
+++ b/race/src/main/java/cz/muni/pa165/race/service/RaceService.java
@@ -18,6 +18,7 @@ import java.util.Objects;
 import java.util.Set;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.client.RestTemplate;
 
 /**
@@ -44,6 +45,7 @@ public class RaceService implements RaceServiceI {
    * @param raceDto race to insert.
    * @return inserted race.
    */
+  @Transactional
   public RaceDto postRace(RaceDto raceDto) {
     raceDto.setId(null);
     return convertRace(raceRepository.save(convertRaceDto(raceDto)));
@@ -54,6 +56,7 @@ public class RaceService implements RaceServiceI {
    *
    * @param raceId race id
    */
+  @Transactional
   public String deleteRace(Long raceId) {
     raceRepository.deleteById(raceId);
     return "Race with id: " + raceId + "was succesfully deleted";
@@ -62,6 +65,7 @@ public class RaceService implements RaceServiceI {
   /**
    * Assigns driver one.
    */
+  @Transactional
   public RaceDto assignDriverOne(Long driverId, Long raceId, Long carId) {
     var race = raceRepository.findById(raceId)
         .orElseThrow(() -> new DatabaseException("Race not found"));
@@ -83,6 +87,7 @@ public class RaceService implements RaceServiceI {
   /**
    * Assigns driver two.
    */
+  @Transactional
   public RaceDto assignDriverTwo(Long driverId, Long raceId, Long carId) {
     var race = raceRepository.findById(raceId)
         .orElseThrow(() -> new DatabaseException("Race not found"));
@@ -108,6 +113,7 @@ public class RaceService implements RaceServiceI {
    * @param raceId id of race.
    * @return found race.
    */
+  @Transactional(readOnly = true)
   public RaceDto findRaceById(Long raceId) {
     return convertRace(raceRepository.findById(raceId).orElseThrow(
         () -> new DatabaseException("Something went wrong when"
@@ -119,6 +125,7 @@ public class RaceService implements RaceServiceI {
    *
    * @return found races.
    */
+  @Transactional(readOnly = true)
   public List<RaceDto> findRaces() {
     return raceRepository.findAll().stream().map(this::convertRace).toList();
   }
@@ -130,6 +137,7 @@ public class RaceService implements RaceServiceI {
    * @param position position of driver two.
    * @return updated race.
    */
+  @Transactional
   public RaceDto assignPositionForDriverTwo(Long raceId, Integer position) {
     var race =
         raceRepository.findById(raceId).orElseThrow(() -> new DatabaseException("Race not found"));
@@ -144,10 +152,11 @@ public class RaceService implements RaceServiceI {
    * @param position position of driver one.
    * @return updated race.
    */
+  @Transactional
   public RaceDto assignPositionForDriverOne(Long raceId, Integer position) {
     var race =
         raceRepository.findById(raceId).orElseThrow(() -> new DatabaseException("Race not found"));
-    race.getDriver2().setFinalPosition(position);
+    race.getDriver1().setFinalPosition(position);
     return convertRace(raceRepository.save(race));
   }
 
@@ -157,6 +166,7 @@ public class RaceService implements RaceServiceI {
    * @param location location of the race.
    * @return set if ids of most suitable drivers for given location.
    */
+  @Transactional(readOnly = true)
   public Set<Long> findMostSuitableDriver(Location location) {
     var races = raceRepository.findRacesByLocation(location);
     Map<Long, Integer> driverWithPoints = new HashMap<>();
diff --git a/race/src/main/java/cz/muni/pa165/race/service/SeasonService.java b/race/src/main/java/cz/muni/pa165/race/service/SeasonService.java
index 78670308..1cfe9d61 100644
--- a/race/src/main/java/cz/muni/pa165/race/service/SeasonService.java
+++ b/race/src/main/java/cz/muni/pa165/race/service/SeasonService.java
@@ -10,6 +10,7 @@ import cz.muni.pa165.race.data.repository.SeasonRepository;
 import java.util.List;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * Season service.
@@ -33,6 +34,7 @@ public class SeasonService implements SeasonServiceI {
    * @param seasonDto season to insert.
    * @return inserted season.
    */
+  @Transactional
   public SeasonDto postSeason(SeasonDto seasonDto) {
     seasonDto.setId(null);
     return seasonConverter(seasonRepository.save(seasonDtoConverter(seasonDto)));
@@ -44,6 +46,7 @@ public class SeasonService implements SeasonServiceI {
    * @param seasonId season id.
    * @return found season.
    */
+  @Transactional(readOnly = true)
   public SeasonDto getSeasonById(Long seasonId) {
     return seasonConverter(seasonRepository.findById(seasonId).orElseThrow(
         () -> new DatabaseException("Something went wrong when"
@@ -55,6 +58,7 @@ public class SeasonService implements SeasonServiceI {
    *
    * @return found seasons.
    */
+  @Transactional(readOnly = true)
   public List<SeasonDto> getAllSeasons() {
     return seasonRepository.findAll()
         .stream()
@@ -68,6 +72,7 @@ public class SeasonService implements SeasonServiceI {
    * @param seasonId season id.
    * @return found season.
    */
+  @Transactional
   public String deleteById(Long seasonId) {
     seasonRepository.deleteById(seasonId);
     return "Season with id: " + seasonId + "was succesfully deleted";
@@ -80,6 +85,7 @@ public class SeasonService implements SeasonServiceI {
    * @param seasonId season id.
    * @return modified race.
    */
+  @Transactional(readOnly = true)
   public SeasonDto addRace(Long raceId, Long seasonId) {
     var season = seasonRepository.findById(seasonId)
         .orElseThrow(() -> new DatabaseException("Season not found"));
-- 
GitLab