From bfcba4eb304b4df570b049e2b2850802b7390049 Mon Sep 17 00:00:00 2001 From: SebClem Date: Fri, 17 Jun 2022 23:04:20 +0200 Subject: [PATCH] :hammer: Add setting post --- .../Api/Controllers/SettingController.java | 9 + .../Broken/Api/Services/SettingService.java | 177 ++---------------- .../Settings/SettingDescriptionBuilder.java | 111 +++++++++++ .../Broken/Tools/Settings/SettingSaver.java | 47 +++++ .../Tools/Settings/SettingValueBuilder.java | 89 +++++++++ 5 files changed, 267 insertions(+), 166 deletions(-) create mode 100644 src/main/java/net/Broken/Tools/Settings/SettingDescriptionBuilder.java create mode 100644 src/main/java/net/Broken/Tools/Settings/SettingSaver.java create mode 100644 src/main/java/net/Broken/Tools/Settings/SettingValueBuilder.java diff --git a/src/main/java/net/Broken/Api/Controllers/SettingController.java b/src/main/java/net/Broken/Api/Controllers/SettingController.java index 9968250..2cbe1f6 100644 --- a/src/main/java/net/Broken/Api/Controllers/SettingController.java +++ b/src/main/java/net/Broken/Api/Controllers/SettingController.java @@ -3,6 +3,8 @@ package net.Broken.Api.Controllers; import net.Broken.Api.Data.Settings.SettingGroup; import net.Broken.Api.Data.Settings.Value; import net.Broken.Api.Services.SettingService; +import net.Broken.DB.Entity.GuildPreferenceEntity; +import net.Broken.Tools.Settings.SettingValueBuilder; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; @@ -28,4 +30,11 @@ public class SettingController { public List getSettingValues(@PathVariable String guildId){ return settingService.getValues(guildId); } + + @PostMapping("/{guildId}/values") + @PreAuthorize("isInGuild(#guildId) && canManageGuild(#guildId)") + public List getSettingValues(@PathVariable String guildId, @RequestBody List values){ + GuildPreferenceEntity pref = settingService.saveValue(guildId, values); + return new SettingValueBuilder(pref).build(); + } } diff --git a/src/main/java/net/Broken/Api/Services/SettingService.java b/src/main/java/net/Broken/Api/Services/SettingService.java index f7407f4..d608639 100644 --- a/src/main/java/net/Broken/Api/Services/SettingService.java +++ b/src/main/java/net/Broken/Api/Services/SettingService.java @@ -6,6 +6,9 @@ import net.Broken.Api.Data.Settings.SettingGroup; import net.Broken.Api.Data.Settings.Value; import net.Broken.DB.Entity.GuildPreferenceEntity; import net.Broken.DB.Repository.GuildPreferenceRepository; +import net.Broken.Tools.Settings.SettingDescriptionBuilder; +import net.Broken.Tools.Settings.SettingSaver; +import net.Broken.Tools.Settings.SettingValueBuilder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.stereotype.Service; @@ -25,106 +28,7 @@ public class SettingService { } public List getSettingDescription() { - List toReturn = new ArrayList<>(); - toReturn.add(getWelcomeGroup()); - toReturn.add(getDefaultRoleGroup()); - toReturn.add(getAutoVoiceChannelGroup()); - toReturn.add(getDailyGroup()); - return toReturn; - } - - private SettingGroup getWelcomeGroup() { - SettingDescriber mainField = new SettingDescriber( - "welcome_enable", - "Enable Welcome Message", - null, - SettingDescriber.TYPE.BOOL - ); - - List fields = new ArrayList<>(); - fields.add(new SettingDescriber( - "welcome_chanel_id", - "Welcome Message chanel", - null, - SettingDescriber.TYPE.TEXT_CHANNEL - )); - fields.add(new SettingDescriber( - "welcome_message", - "Welcome Message", - null, - SettingDescriber.TYPE.STRING - )); - - - return new SettingGroup( - "Welcome Message", - mainField, - fields); - } - - private SettingGroup getDefaultRoleGroup() { - SettingDescriber mainField = new SettingDescriber( - "default_role", - "Enable Default Role", - null, - SettingDescriber.TYPE.BOOL - ); - - List fields = new ArrayList<>(); - fields.add(new SettingDescriber( - "default_role_id", - "Default Role", - null, - SettingDescriber.TYPE.ROLE - )); - - return new SettingGroup( - "Default Role", - mainField, - fields); - } - - private SettingGroup getDailyGroup() { - List fields = new ArrayList<>(); - fields.add(new SettingDescriber( - "daily_madame", - "[NSFW] Enable Daily Madame Message", - null, - SettingDescriber.TYPE.BOOL - )); - - return new SettingGroup( - "Daily", - null, - fields); - } - - private SettingGroup getAutoVoiceChannelGroup() { - SettingDescriber mainField = new SettingDescriber( - "auto_voice", - "Enable Auto Create Voice Chanel", - null, - SettingDescriber.TYPE.BOOL - ); - - List fields = new ArrayList<>(); - fields.add(new SettingDescriber( - "auto_voice_base_channel", - "Base Voice Channel For Auto Create", - "If someone joint this channel, a new voice channel will be created with the same settings.", - SettingDescriber.TYPE.VOICE_CHANNEL - )); - fields.add(new SettingDescriber( - "auto_voice_channel_title", - "Auto Created Voice Channel title", - "Auto created voice channel will use this title, @count will be replaced by the channel count.", - SettingDescriber.TYPE.STRING - )); - - return new SettingGroup( - "Auto Voice Channel", - mainField, - fields); + return new SettingDescriptionBuilder().build(); } @@ -133,74 +37,15 @@ public class SettingService { logger.info("[API] : Generate default guild pref"); return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId)); }); - List values = new ArrayList<>(getWelcomeValues(pref)); - values.addAll(getDefaultRoleValues(pref)); - values.addAll(getDailyValues(pref)); - values.addAll(getAutoVoiceChannelValues(pref)); - - return values; + return new SettingValueBuilder(pref).build(); } - private List getWelcomeValues(GuildPreferenceEntity pref) { - List toReturn = new ArrayList<>(); - toReturn.add(new Value( - "welcome_enable", - pref.isWelcome() - ) - ); - toReturn.add(new Value( - "welcome_chanel_id", - pref.getWelcomeChanelID() - ) - ); - toReturn.add(new Value( - "welcome_message", - pref.getWelcomeMessage() - ) - ); - return toReturn; - } - private List getDefaultRoleValues(GuildPreferenceEntity pref) { - List toReturn = new ArrayList<>(); - toReturn.add(new Value( - "default_role", - pref.isDefaultRole() - ) - ); - toReturn.add(new Value( - "default_role_id", - pref.getDefaultRoleId() - ) - ); - return toReturn; - } - private List getDailyValues(GuildPreferenceEntity pref) { - List toReturn = new ArrayList<>(); - toReturn.add(new Value( - "daily_madame", - pref.isDailyMadame() - ) - ); - return toReturn; + public GuildPreferenceEntity saveValue(String guildId, List values){ + GuildPreferenceEntity pref = preferenceRepository.findByGuildId(guildId).orElseGet(() -> { + logger.info("[API] : Generate default guild pref"); + return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId)); + }); + return new SettingSaver(preferenceRepository, pref).save(values); } - private List getAutoVoiceChannelValues(GuildPreferenceEntity pref) { - List toReturn = new ArrayList<>(); - toReturn.add(new Value( - "auto_voice", - pref.isAutoVoice() - ) - ); - toReturn.add(new Value( - "auto_voice_base_channel", - pref.getAutoVoiceChannelID() - ) - ); - toReturn.add(new Value( - "auto_voice_channel_title", - pref.getAutoVoiceChannelTitle() - ) - ); - return toReturn; - } } diff --git a/src/main/java/net/Broken/Tools/Settings/SettingDescriptionBuilder.java b/src/main/java/net/Broken/Tools/Settings/SettingDescriptionBuilder.java new file mode 100644 index 0000000..a33a075 --- /dev/null +++ b/src/main/java/net/Broken/Tools/Settings/SettingDescriptionBuilder.java @@ -0,0 +1,111 @@ +package net.Broken.Tools.Settings; + +import net.Broken.Api.Data.Settings.SettingDescriber; +import net.Broken.Api.Data.Settings.SettingGroup; + +import java.util.ArrayList; +import java.util.List; + +public class SettingDescriptionBuilder { + public List build(){ + List toReturn = new ArrayList<>(); + toReturn.add(getWelcomeGroup()); + toReturn.add(getDefaultRoleGroup()); + toReturn.add(getAutoVoiceChannelGroup()); + toReturn.add(getDailyGroup()); + return toReturn; + } + private SettingGroup getWelcomeGroup() { + SettingDescriber mainField = new SettingDescriber( + "welcome_enable", + "Enable Welcome Message", + null, + SettingDescriber.TYPE.BOOL + ); + + List fields = new ArrayList<>(); + fields.add(new SettingDescriber( + "welcome_chanel_id", + "Welcome Message chanel", + null, + SettingDescriber.TYPE.TEXT_CHANNEL + )); + fields.add(new SettingDescriber( + "welcome_message", + "Welcome Message", + null, + SettingDescriber.TYPE.STRING + )); + + + return new SettingGroup( + "Welcome Message", + mainField, + fields); + } + + private SettingGroup getDefaultRoleGroup() { + SettingDescriber mainField = new SettingDescriber( + "default_role", + "Enable Default Role", + null, + SettingDescriber.TYPE.BOOL + ); + + List fields = new ArrayList<>(); + fields.add(new SettingDescriber( + "default_role_id", + "Default Role", + null, + SettingDescriber.TYPE.ROLE + )); + + return new SettingGroup( + "Default Role", + mainField, + fields); + } + + private SettingGroup getDailyGroup() { + List fields = new ArrayList<>(); + fields.add(new SettingDescriber( + "daily_madame", + "[NSFW] Enable Daily Madame Message", + null, + SettingDescriber.TYPE.BOOL + )); + + return new SettingGroup( + "Daily", + null, + fields); + } + + private SettingGroup getAutoVoiceChannelGroup() { + SettingDescriber mainField = new SettingDescriber( + "auto_voice", + "Enable Auto Create Voice Chanel", + null, + SettingDescriber.TYPE.BOOL + ); + + List fields = new ArrayList<>(); + fields.add(new SettingDescriber( + "auto_voice_base_channel", + "Base Voice Channel For Auto Create", + "If someone joint this channel, a new voice channel will be created with the same settings.", + SettingDescriber.TYPE.VOICE_CHANNEL + )); + fields.add(new SettingDescriber( + "auto_voice_channel_title", + "Auto Created Voice Channel title", + "Auto created voice channel will use this title, @count will be replaced by the channel count.", + SettingDescriber.TYPE.STRING + )); + + return new SettingGroup( + "Auto Voice Channel", + mainField, + fields); + } +} diff --git a/src/main/java/net/Broken/Tools/Settings/SettingSaver.java b/src/main/java/net/Broken/Tools/Settings/SettingSaver.java new file mode 100644 index 0000000..3742462 --- /dev/null +++ b/src/main/java/net/Broken/Tools/Settings/SettingSaver.java @@ -0,0 +1,47 @@ +package net.Broken.Tools.Settings; + +import net.Broken.Api.Data.Settings.Value; +import net.Broken.DB.Entity.GuildPreferenceEntity; +import net.Broken.DB.Repository.GuildPreferenceRepository; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; + +public class SettingSaver { + private final GuildPreferenceRepository guildPreferenceRepository; + private final GuildPreferenceEntity guildPreference; + + private final Logger logger = LogManager.getLogger(); + + public SettingSaver(GuildPreferenceRepository guildPreferenceRepository, GuildPreferenceEntity guildPreference) { + this.guildPreferenceRepository = guildPreferenceRepository; + this.guildPreference = guildPreference; + } + + public GuildPreferenceEntity save(List values) { + for (Value value : values) { + setValue(value); + } + return guildPreferenceRepository.save(guildPreference); + } + + private void setValue(Value value) { + switch (value.id()) { +// WELCOME + case "welcome_enable" -> guildPreference.setWelcome((Boolean) value.value()); + case "welcome_chanel_id" -> guildPreference.setWelcomeChanelID((String) value.value()); + case "welcome_message" -> guildPreference.setWelcomeMessage((String) value.value()); +// DEFAULT ROLE + case "default_role" -> guildPreference.setDefaultRole((Boolean) value.value()); + case "default_role_id" -> guildPreference.setDefaultRoleId((String) value.value()); +// DAILY + case "daily_madame" -> guildPreference.setDailyMadame((Boolean) value.value()); +// AUTO VOICE CHAN + case "auto_voice" -> guildPreference.setAutoVoice((Boolean) value.value()); + case "auto_voice_base_channel" -> guildPreference.setAutoVoiceChannelID((String) value.value()); + case "auto_voice_channel_title" -> guildPreference.setAutoVoiceChannelTitle((String) value.value()); + default -> logger.warn("Invalid setting received: " + value.id()); + } + } +} diff --git a/src/main/java/net/Broken/Tools/Settings/SettingValueBuilder.java b/src/main/java/net/Broken/Tools/Settings/SettingValueBuilder.java new file mode 100644 index 0000000..9456c2f --- /dev/null +++ b/src/main/java/net/Broken/Tools/Settings/SettingValueBuilder.java @@ -0,0 +1,89 @@ +package net.Broken.Tools.Settings; + +import net.Broken.Api.Data.Settings.Value; +import net.Broken.DB.Entity.GuildPreferenceEntity; + +import java.util.ArrayList; +import java.util.List; + +public class SettingValueBuilder { + private final GuildPreferenceEntity guildPreference; + + public SettingValueBuilder(GuildPreferenceEntity guildPreference) { + this.guildPreference = guildPreference; + } + + public List build() { + List values = new ArrayList<>(getWelcomeValues(guildPreference)); + values.addAll(getDefaultRoleValues(guildPreference)); + values.addAll(getDailyValues(guildPreference)); + values.addAll(getAutoVoiceChannelValues(guildPreference)); + return values; + } + + private List getWelcomeValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "welcome_enable", + pref.isWelcome() + ) + ); + toReturn.add(new Value( + "welcome_chanel_id", + pref.getWelcomeChanelID() + ) + ); + toReturn.add(new Value( + "welcome_message", + pref.getWelcomeMessage() + ) + ); + return toReturn; + } + + private List getDefaultRoleValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "default_role", + pref.isDefaultRole() + ) + ); + toReturn.add(new Value( + "default_role_id", + pref.getDefaultRoleId() + ) + ); + return toReturn; + } + + private List getDailyValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "daily_madame", + pref.isDailyMadame() + ) + ); + return toReturn; + } + + private List getAutoVoiceChannelValues(GuildPreferenceEntity pref) { + List toReturn = new ArrayList<>(); + toReturn.add(new Value( + "auto_voice", + pref.isAutoVoice() + ) + ); + toReturn.add(new Value( + "auto_voice_base_channel", + pref.getAutoVoiceChannelID() + ) + ); + toReturn.add(new Value( + "auto_voice_channel_title", + pref.getAutoVoiceChannelTitle() + ) + ); + return toReturn; + } + +}