mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-01 23:53:27 +00:00
added exception in case the feature is not known to the system
This commit is contained in:
@@ -7,7 +7,7 @@ import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.config.AbstractoFeatures;
|
||||
import dev.sheldan.abstracto.core.commands.utility.UtilityModuleInterface;
|
||||
import dev.sheldan.abstracto.core.service.management.FeatureFlagManagementService;
|
||||
import dev.sheldan.abstracto.core.service.FeatureFlagService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -17,13 +17,13 @@ import java.util.List;
|
||||
@Component
|
||||
public class Disable implements Command {
|
||||
|
||||
|
||||
@Autowired
|
||||
private FeatureFlagManagementService featureFlagManagementService;
|
||||
private FeatureFlagService featureFlagManagementService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
String flagKey = (String) commandContext.getParameters().getParameters().get(0);
|
||||
featureFlagManagementService.updateOrCreateFeatureFlag(flagKey, commandContext.getGuild().getIdLong(), false);
|
||||
featureFlagManagementService.disableFeature(flagKey, commandContext.getGuild().getIdLong());
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import dev.sheldan.abstracto.core.command.execution.CommandResult;
|
||||
import dev.sheldan.abstracto.core.command.config.Parameter;
|
||||
import dev.sheldan.abstracto.core.config.AbstractoFeatures;
|
||||
import dev.sheldan.abstracto.core.commands.utility.UtilityModuleInterface;
|
||||
import dev.sheldan.abstracto.core.service.management.FeatureFlagManagementService;
|
||||
import dev.sheldan.abstracto.core.service.FeatureFlagService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -19,11 +19,12 @@ public class Enable implements Command {
|
||||
|
||||
|
||||
@Autowired
|
||||
private FeatureFlagManagementService featureFlagManagementService;
|
||||
private FeatureFlagService featureFlagManagementService;
|
||||
|
||||
@Override
|
||||
public CommandResult execute(CommandContext commandContext) {
|
||||
String flagKey = (String) commandContext.getParameters().getParameters().get(0);
|
||||
featureFlagManagementService.updateOrCreateFeatureFlag(flagKey, commandContext.getGuild().getIdLong(), true);
|
||||
featureFlagManagementService.enableFeature(flagKey, commandContext.getGuild().getIdLong());
|
||||
return CommandResult.fromSuccess();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,9 @@ import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Getter
|
||||
@@ -13,4 +15,12 @@ import java.util.HashMap;
|
||||
@ConfigurationProperties(prefix = "abstracto")
|
||||
public class FeatureFlagConfig {
|
||||
private HashMap<String, Boolean> features = new HashMap<>();
|
||||
|
||||
public boolean doesFeatureExist(String name) {
|
||||
return features.containsKey(name);
|
||||
}
|
||||
|
||||
public List<String> getFeaturesAsList() {
|
||||
return new ArrayList<>(features.keySet());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.service;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.FeatureFlagConfig;
|
||||
import dev.sheldan.abstracto.core.exception.FeatureNotFoundException;
|
||||
import dev.sheldan.abstracto.core.service.management.FeatureFlagManagementService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -10,8 +12,28 @@ public class FeatureFlagServiceBean implements FeatureFlagService {
|
||||
@Autowired
|
||||
private FeatureFlagManagementService managementService;
|
||||
|
||||
@Autowired
|
||||
private FeatureFlagConfig featureFlagConfig;
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isFeatureEnabled(String name, Long serverId) {
|
||||
return managementService.getFeatureFlagValue(name, serverId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableFeature(String name, Long serverId) {
|
||||
if(!featureFlagConfig.doesFeatureExist(name)) {
|
||||
throw new FeatureNotFoundException("Feature not found.", name, featureFlagConfig.getFeaturesAsList());
|
||||
}
|
||||
managementService.updateFeatureFlag(name, serverId, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableFeature(String name, Long serverId) {
|
||||
if(!featureFlagConfig.doesFeatureExist(name)) {
|
||||
throw new FeatureNotFoundException("Feature not found.", name, featureFlagConfig.getFeaturesAsList());
|
||||
}
|
||||
managementService.updateFeatureFlag(name, serverId, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,14 +41,12 @@ public class FeatureFlagManagementServiceBean implements FeatureFlagManagementSe
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOrCreateFeatureFlag(String key, Long serverId, Boolean newValue) {
|
||||
public void updateFeatureFlag(String key, Long serverId, Boolean newValue) {
|
||||
Optional<AFeatureFlag> existing = getFeatureFlag(key, serverId);
|
||||
if(existing.isPresent()) {
|
||||
AFeatureFlag flag = existing.get();
|
||||
flag.setEnabled(newValue);
|
||||
repository.save(flag);
|
||||
} else {
|
||||
createFeatureFlag(key, serverId, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Feature ${featureName} not available. The available features are: ${availableFeatures?join(", ")}
|
||||
Reference in New Issue
Block a user