package com.nincraft.ninbot.components.config.component;

import lombok.val;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional
public class ComponentService {

    private ComponentRepository componentRepository;
    private DisabledComponentsRepository disabledComponentsRepository;

    public ComponentService(ComponentRepository componentRepository,
            DisabledComponentsRepository disabledComponentsRepository) {
        this.componentRepository = componentRepository;
        this.disabledComponentsRepository = disabledComponentsRepository;
    }

    public List<Component> getAllComponents() {
        return componentRepository.findAll();
    }

    public void registerComponent(String name, ComponentType componentType) {
        val componentOptional = componentRepository.findByNameAndType(name, componentType);
        if (!componentOptional.isPresent()) {
            Component component = new Component(name, componentType);
            componentRepository.save(component);
        }
    }

    void disableComponent(String name, String serverId) {
        val component = componentRepository.findByName(name);
        val list = disabledComponentsRepository.findByComponentAndServerId(component, serverId);
        if (list.isEmpty()) {
            DisabledComponents disabledComponents = new DisabledComponents(serverId, component);
            disabledComponentsRepository.save(disabledComponents);
        }
    }

    private List<DisabledComponents> getDisabledComponents(Component component, String serverId) {
        return disabledComponentsRepository.findByComponentAndServerId(component, serverId);
    }

    @Cacheable("disabled-component")
    public boolean isDisabled(String name, String serverId) {
        val component = componentRepository.findByName(name);
        return !getDisabledComponents(component, serverId).isEmpty();
    }

    void enableComponent(String name, String serverId) {
        val component = componentRepository.findByName(name);
        getDisabledComponents(component, serverId).forEach(disabledComponents ->
                disabledComponentsRepository.delete(disabledComponents));
    }

    @Cacheable("disabled-server-component")
    public List<DisabledComponents> getDisabledComponents(String serverId) {
        return disabledComponentsRepository.findByServerId(serverId);
    }
}

@@ -70,16 +70,72 @@
 ok.val;%0A
+import org.springframework.cache.annotation.CacheEvict;%0A
 import o
@@ -1238,24 +1238,126 @@
 %0A    %7D%0A%0A    
+@CacheEvict(allEntries = true, value = %7B%22disable-component%22, %22disabled-server-component%22%7D)%0A    public 
 void disable

Fix component cache issue