package com.nincraft.ninbot.components.config;

import lombok.val;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ConfigService {

    private ConfigRepository configRepository;

    public ConfigService(ConfigRepository configRepository) {
        this.configRepository = configRepository;
    }

    @Transactional
    @Cacheable("configs-by-name")
    public List<Config> getConfigByName(String serverId, String configName) {
        return configRepository.getConfigsByServerIdAndName(serverId, configName);
    }

    @Transactional
    @Cacheable("config-values-by-name")
    public List<String> getValuesByName(String serverId, String configName) {
        val list = configRepository.getConfigsByServerIdAndName(serverId, configName);
        return list.stream().map(Config::getValue).collect(Collectors.toCollection(ArrayList::new));
    }

    @Transactional
    public Optional<String> getSingleValueByName(String serverId, String configName) {
        val list = configRepository.getConfigsByServerIdAndName(serverId, configName);
        val valueList = list.stream().map(Config::getValue).collect(Collectors.toCollection(ArrayList::new));
        if (valueList.isEmpty()) {
            return Optional.empty();
        } else {
            return Optional.ofNullable(valueList.get(0));
        }
    }

    @Transactional
    public Optional<Config> getConfigByServerIdAndName(String serverId, String configName) {
        return configRepository.getConfigByServerIdAndName(serverId, configName);
    }

    @Transactional
    public void removeConfig(Config config) {
        configRepository.delete(config);
    }

    @Transactional
    public void removeConfig(String serverId, String configName, String configValue) {
        configRepository.getConfigByServerIdAndNameAndValue(serverId, configName, configValue).ifPresent(config ->
                configRepository.delete(config)
        );
    }

    @Transactional
    public void addConfig(Config config) {
        configRepository.save(config);
    }

    @Transactional
    public void addConfig(String serverId, String configName, String configValue) {
        configRepository.save(new Config(serverId, configName, configValue));
    }

    @Transactional
    public List<Config> getConfigsByServerId(String serverId) {
        return configRepository.getConfigsByServerId(serverId);
    }

    @Transactional
    public void updateConfig(Config config) {
        configRepository.getConfigByServerIdAndName(config.getServerId(), config.getName()).ifPresent(oldConfig -> {
            oldConfig.setValue(config.getValue());
            configRepository.save(oldConfig);
        });
    }
}

@@ -56,24 +56,80 @@
 lombok.val;%0A
+import org.springframework.cache.annotation.CacheEvict;%0A
 import org.s
@@ -1812,32 +1812,121 @@
  @Transactional%0A
+    @CacheEvict(allEntries = true, value = %7B%22configs-by-name%22, %22config-values-by-name%22%7D)%0A
     public void 
@@ -2014,32 +2014,121 @@
  @Transactional%0A
+    @CacheEvict(allEntries = true, value = %7B%22configs-by-name%22, %22config-values-by-name%22%7D)%0A
     public void 
@@ -2390,32 +2390,121 @@
  @Transactional%0A
+    @CacheEvict(allEntries = true, value = %7B%22configs-by-name%22, %22config-values-by-name%22%7D)%0A
     public void 
@@ -2587,32 +2587,121 @@
  @Transactional%0A
+    @CacheEvict(allEntries = true, value = %7B%22configs-by-name%22, %22config-values-by-name%22%7D)%0A
     public void 
@@ -3018,32 +3018,121 @@
  @Transactional%0A
+    @CacheEvict(allEntries = true, value = %7B%22configs-by-name%22, %22config-values-by-name%22%7D)%0A
     public void 

Fix config cache issue