🔨 Add setting post

This commit is contained in:
SebClem 2022-06-17 23:04:20 +02:00
parent f8b134df34
commit bfcba4eb30
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
5 changed files with 267 additions and 166 deletions

View File

@ -3,6 +3,8 @@ package net.Broken.Api.Controllers;
import net.Broken.Api.Data.Settings.SettingGroup; import net.Broken.Api.Data.Settings.SettingGroup;
import net.Broken.Api.Data.Settings.Value; import net.Broken.Api.Data.Settings.Value;
import net.Broken.Api.Services.SettingService; 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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -28,4 +30,11 @@ public class SettingController {
public List<Value> getSettingValues(@PathVariable String guildId){ public List<Value> getSettingValues(@PathVariable String guildId){
return settingService.getValues(guildId); return settingService.getValues(guildId);
} }
@PostMapping("/{guildId}/values")
@PreAuthorize("isInGuild(#guildId) && canManageGuild(#guildId)")
public List<Value> getSettingValues(@PathVariable String guildId, @RequestBody List<Value> values){
GuildPreferenceEntity pref = settingService.saveValue(guildId, values);
return new SettingValueBuilder(pref).build();
}
} }

View File

@ -6,6 +6,9 @@ import net.Broken.Api.Data.Settings.SettingGroup;
import net.Broken.Api.Data.Settings.Value; import net.Broken.Api.Data.Settings.Value;
import net.Broken.DB.Entity.GuildPreferenceEntity; import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.DB.Repository.GuildPreferenceRepository; 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.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -25,106 +28,7 @@ public class SettingService {
} }
public List<SettingGroup> getSettingDescription() { public List<SettingGroup> getSettingDescription() {
List<SettingGroup> toReturn = new ArrayList<>(); return new SettingDescriptionBuilder().build();
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<SettingDescriber> 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<SettingDescriber> 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<SettingDescriber> 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<SettingDescriber> 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);
} }
@ -133,74 +37,15 @@ public class SettingService {
logger.info("[API] : Generate default guild pref"); logger.info("[API] : Generate default guild pref");
return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId)); return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId));
}); });
List<Value> values = new ArrayList<>(getWelcomeValues(pref)); return new SettingValueBuilder(pref).build();
values.addAll(getDefaultRoleValues(pref));
values.addAll(getDailyValues(pref));
values.addAll(getAutoVoiceChannelValues(pref));
return values;
} }
private List<Value> getWelcomeValues(GuildPreferenceEntity pref) { public GuildPreferenceEntity saveValue(String guildId, List<Value> values){
List<Value> toReturn = new ArrayList<>(); GuildPreferenceEntity pref = preferenceRepository.findByGuildId(guildId).orElseGet(() -> {
toReturn.add(new Value( logger.info("[API] : Generate default guild pref");
"welcome_enable", return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId));
pref.isWelcome() });
) return new SettingSaver(preferenceRepository, pref).save(values);
);
toReturn.add(new Value(
"welcome_chanel_id",
pref.getWelcomeChanelID()
)
);
toReturn.add(new Value(
"welcome_message",
pref.getWelcomeMessage()
)
);
return toReturn;
}
private List<Value> getDefaultRoleValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"default_role",
pref.isDefaultRole()
)
);
toReturn.add(new Value(
"default_role_id",
pref.getDefaultRoleId()
)
);
return toReturn;
}
private List<Value> getDailyValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"daily_madame",
pref.isDailyMadame()
)
);
return toReturn;
} }
private List<Value> getAutoVoiceChannelValues(GuildPreferenceEntity pref) {
List<Value> 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;
}
} }

View File

@ -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<SettingGroup> build(){
List<SettingGroup> 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<SettingDescriber> 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<SettingDescriber> 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<SettingDescriber> 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<SettingDescriber> 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);
}
}

View File

@ -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<Value> 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());
}
}
}

View File

@ -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<Value> build() {
List<Value> values = new ArrayList<>(getWelcomeValues(guildPreference));
values.addAll(getDefaultRoleValues(guildPreference));
values.addAll(getDailyValues(guildPreference));
values.addAll(getAutoVoiceChannelValues(guildPreference));
return values;
}
private List<Value> getWelcomeValues(GuildPreferenceEntity pref) {
List<Value> 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<Value> getDefaultRoleValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"default_role",
pref.isDefaultRole()
)
);
toReturn.add(new Value(
"default_role_id",
pref.getDefaultRoleId()
)
);
return toReturn;
}
private List<Value> getDailyValues(GuildPreferenceEntity pref) {
List<Value> toReturn = new ArrayList<>();
toReturn.add(new Value(
"daily_madame",
pref.isDailyMadame()
)
);
return toReturn;
}
private List<Value> getAutoVoiceChannelValues(GuildPreferenceEntity pref) {
List<Value> 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;
}
}