commit b84f8085eeeecefa160ca35e08c228f06d0911fe
Author: Hrabikv <hrabikv@students.zcu.cz>
Date:   Sun Oct 15 21:46:53 2023 +0200

    https://kivprogrammers.atlassian.net/browse/TSP2-30 připrava backendu stránky configuration

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
index cc97875..fe755b0 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/ConfigurationController.java
@@ -49,28 +49,41 @@ public class ConfigurationController {
      *         500 if db server died or no default configurations are present in the database
      */
     @PostMapping(value="/configuration")
-    public ResponseEntity<String> getUserConfigurations(@RequestBody User user) {
+    public ResponseEntity<String> getConfiguration(@RequestBody UserConfiguration userConfiguration) {
         Map<String, Object> json = new HashMap<>();
-        List<Configuration> configuration = this.configurationService.getUserConfigurations(user);
-        //this can only happen if db server is offline
-        //or no default configuration exists in database
-        if(configuration == null) {
+//        Configuration config = this.configurationService.getConfigurationById(Integer.parseInt(userConfiguration.getId()));
+
+        Configuration config = this.configurationService.getConfiguration(userConfiguration.getUser().getName(), Integer.parseInt(userConfiguration.getId()));
+
+        if(config == null) {
             json.put("message", "internal sever error");
             return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR);
         }
-        //list of configuration definitions
-        List<String> configurationDefinition = new ArrayList<>();
-        //and their names
-        List<String> configurationNames = new ArrayList<>();
-        //hipster syntax for Petr
-        configuration.forEach(cfg -> {
-            configurationDefinition.add(cfg.getConfig());
-            configurationNames.add(cfg.getConfigurationName());
-        });
-        json.put("configuration",configurationDefinition);
-        json.put("configuration_names",configurationNames);
+
+        json.put("configuration", config.getConfig());
 
         return new ResponseEntity<>(new Gson().toJson(json),HttpStatus.OK);
+
+//        List<Configuration> configuration = this.configurationService.getUserConfigurations(user);
+//        //this can only happen if db server is offline
+//        //or no default configuration exists in database
+//        if(configuration == null) {
+//            json.put("message", "internal sever error");
+//            return new ResponseEntity<>(JSONBuilder.buildJSON(json), HttpStatus.INTERNAL_SERVER_ERROR);
+//        }
+//        //list of configuration definitions
+//        List<String> configurationDefinition = new ArrayList<>();
+//        //and their names
+//        List<String> configurationNames = new ArrayList<>();
+//        //hipster syntax for Petr
+//        configuration.forEach(cfg -> {
+//            configurationDefinition.add(cfg.getConfig());
+//            configurationNames.add(cfg.getConfigurationName());
+//        });
+//        json.put("configuration",configurationDefinition);
+//        json.put("configuration_names",configurationNames);
+//
+//        return new ResponseEntity<>(new Gson().toJson(json),HttpStatus.OK);
     }
 
     /**
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/Configuration.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/Configuration.java
index 3fc4059..a9c0b77 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/Configuration.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/Configuration.java
@@ -35,6 +35,10 @@ public class Configuration {
         this.id = id;
     }
 
+    public Configuration(String config) {
+        this.config = config;
+    }
+
     public Configuration(String config, String isDefault, String configurationName, String defaultConfigName) {
         this.config = config;
         this.configHash = null;
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfiguration.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfiguration.java
index a8b7fe2..ddf3451 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfiguration.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/UserConfiguration.java
@@ -11,14 +11,25 @@ public class UserConfiguration {
     private String isDefault;
     private String configurationName;
 
+    private String id;
+
     public UserConfiguration(User user, ConfigurationDto configuration, String isDefault, String configurationName) {
         this.user = user;
         this.configuration = configuration;
         this.isDefault = isDefault;
         this.configurationName = configurationName;
+        this.id = null;
     }
     public UserConfiguration(){}
 
+    public UserConfiguration(User user, String id) {
+        this.user = user;
+        this.configuration = null;
+        this.configurationName = null;
+        this.isDefault = null;
+        this.id = id;
+    }
+
 
     public User getUser() {
         return this.user;
@@ -51,4 +62,12 @@ public class UserConfiguration {
     public void setConfigurationName(String configurationName) {
         this.configurationName = configurationName;
     }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java
index b606dbb..f296d07 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/ConfigRepository.java
@@ -1,6 +1,7 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository;
 
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Configuration;
+import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.User;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.UserConfigKey;
 import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.UserConfigurationJoin;
 import org.springframework.beans.factory.annotation.Value;
@@ -28,4 +29,13 @@ public interface ConfigRepository extends JpaRepository<Configuration,Integer> {
 
     @Query("select userconfig.configurationName from UserConfigurationJoin userconfig where userconfig.id = ?1")
     String findConfigurationByCompoundKey(UserConfigKey key);
+
+    @Query("SELECT \n" +
+            "cfg.config \n" +
+            "FROM \n" +
+            "UserConfigurationJoin userconfig INNER JOIN Configuration cfg \n" +
+            "ON userconfig.id.configId = cfg.id\n" +
+            "WHERE\n" +
+            "userconfig.id = ?1")
+    Configuration findConfigurationByUserNameAndID(UserConfigKey key);
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java
index 73bc352..b79c3d2 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigService.java
@@ -16,6 +16,8 @@ public interface ConfigService {
     List<Configuration> getUserConfigurations(User user);
 
     Configuration getConfigurationById(int id);
+
+    public Configuration getConfiguration(String userName, int id);
     String getConfigurationName(int userId, int configurationId);
 
 
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java
index f7c6417..97782c7 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/configuration/ConfigurationServiceImplementation.java
@@ -153,6 +153,13 @@ public class ConfigurationServiceImplementation implements ConfigService {
         return this.configurationRepository.findConfigurationById(id);
     }
 
+    public Configuration getConfiguration(String userName, int id) {
+        User user = this.userService.getUserByName(userName);
+        UserConfigKey key = new UserConfigKey(user.getId(), id);
+        Configuration configuration = this.configurationRepository.findConfigurationByUserNameAndID(key);
+        return configuration;
+    }
+
     @Override
     public String getConfigurationName(int userId, int configurationId) {
         return this.configurationRepository.findConfigurationByCompoundKey(new UserConfigKey(userId, configurationId));
