From 63d1250b1fdff6f3b7c106507e0facea8bf9c3a6 Mon Sep 17 00:00:00 2001
From: Oto Stanko <493068@fi.muni.cz>
Date: Sat, 29 Apr 2023 13:56:35 +0200
Subject: [PATCH] Persisting some drivers

---
 .../java/cz/muni/pa165/component/App.java     |  3 +-
 ...Seed.java => PersistingCarComponents.java} |  6 +--
 .../component/rest/ComponentController.java   |  1 +
 .../driver/config/PersistingDrivers.java      | 50 +++++++++++++++++++
 4 files changed, 55 insertions(+), 5 deletions(-)
 rename component/src/main/java/cz/muni/pa165/component/config/{Seed.java => PersistingCarComponents.java} (93%)
 create mode 100644 driver/src/main/java/cz/muni/pa165/driver/config/PersistingDrivers.java

diff --git a/component/src/main/java/cz/muni/pa165/component/App.java b/component/src/main/java/cz/muni/pa165/component/App.java
index 4e6c468a..cfef7ff3 100644
--- a/component/src/main/java/cz/muni/pa165/component/App.java
+++ b/component/src/main/java/cz/muni/pa165/component/App.java
@@ -1,13 +1,12 @@
 package cz.muni.pa165.component;
 
-import jakarta.persistence.EntityManagerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.domain.EntityScan;
 import org.springframework.context.annotation.Bean;
-import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 import org.springframework.web.client.RestTemplate;
 
+
 /**
  * Main app.
  */
diff --git a/component/src/main/java/cz/muni/pa165/component/config/Seed.java b/component/src/main/java/cz/muni/pa165/component/config/PersistingCarComponents.java
similarity index 93%
rename from component/src/main/java/cz/muni/pa165/component/config/Seed.java
rename to component/src/main/java/cz/muni/pa165/component/config/PersistingCarComponents.java
index d319b60a..56cc6e87 100644
--- a/component/src/main/java/cz/muni/pa165/component/config/Seed.java
+++ b/component/src/main/java/cz/muni/pa165/component/config/PersistingCarComponents.java
@@ -9,16 +9,16 @@ import org.springframework.stereotype.Service;
 import java.math.BigDecimal;
 
 @Service
-public class Seed {
+public class PersistingCarComponents {
   private final ComponentRepositoryInterface carComponentRepository;
 
   @Autowired
-  public Seed(ComponentRepositoryInterface carComponentRepository) {
+  public PersistingCarComponents(ComponentRepositoryInterface carComponentRepository) {
     this.carComponentRepository = carComponentRepository;
   }
 
   @PostConstruct
-  public void seed() {
+  public void persistingCarComponents() {
     // name, weight, price, manufacturer
     carComponentRepository.deleteAll();
 
diff --git a/component/src/main/java/cz/muni/pa165/component/rest/ComponentController.java b/component/src/main/java/cz/muni/pa165/component/rest/ComponentController.java
index 9fa71ac3..ddf95cbd 100644
--- a/component/src/main/java/cz/muni/pa165/component/rest/ComponentController.java
+++ b/component/src/main/java/cz/muni/pa165/component/rest/ComponentController.java
@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
+
 /**
  * Controller for managing the components.
  */
diff --git a/driver/src/main/java/cz/muni/pa165/driver/config/PersistingDrivers.java b/driver/src/main/java/cz/muni/pa165/driver/config/PersistingDrivers.java
new file mode 100644
index 00000000..7fe653fe
--- /dev/null
+++ b/driver/src/main/java/cz/muni/pa165/driver/config/PersistingDrivers.java
@@ -0,0 +1,50 @@
+package cz.muni.pa165.driver.config;
+
+import cz.muni.pa165.driver.data.model.Driver;
+import cz.muni.pa165.driver.data.repository.DriverRepository;
+import jakarta.annotation.PostConstruct;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Map;
+
+@Service
+public class PersistingDrivers {
+  private final DriverRepository driverRepository;
+
+  @Autowired
+  public PersistingDrivers(DriverRepository driverRepository) {
+    this.driverRepository = driverRepository;
+  }
+
+  @PostConstruct
+  public void persistDrivers() {
+    driverRepository.deleteAll();
+
+    // name, surname, nationality
+    var firstDriver = new Driver("Andrej", "MamNaSalame", "Z koša");
+    var firstDriverChars = Map.of("Experience", 4,
+        "Consistency", 0,
+        "Racecraft", 5,
+        "Aggressiveness", 1);
+    firstDriver.setCharacteristics(firstDriverChars);
+
+    var secondDriver = new Driver("Tomas", "Tomas", "Tomas");
+    var secondDriverChars = Map.of("Experience", 5,
+        "Consistency", 5,
+        "Racecraft", 2,
+        "Aggressiveness", 4);
+    secondDriver.setCharacteristics(secondDriverChars);
+
+    var thirdDriver = new Driver("Oto", "VymyslamHluposti", "yes");
+    var thirdDriverChars = Map.of("Experience", 2,
+        "Consistency", 4,
+        "Racecraft", 4,
+        "Aggressiveness", 5);
+    thirdDriver.setCharacteristics(thirdDriverChars);
+    
+    driverRepository.save(firstDriver);
+    driverRepository.save(secondDriver);
+    driverRepository.save(thirdDriver);
+  }
+}
-- 
GitLab