🔨 Add value endpoint
This commit is contained in:
parent
17102a9e95
commit
239781fbbd
@ -1,11 +1,10 @@
|
||||
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 org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -23,4 +22,10 @@ public class SettingController {
|
||||
public List<SettingGroup> getSettingDescription(){
|
||||
return settingService.getSettingDescription();
|
||||
}
|
||||
|
||||
@GetMapping("/{guildId}/values")
|
||||
@PreAuthorize("isInGuild(#guildId) && canManageGuild(#guildId)")
|
||||
public List<Value> getSettingValues(@PathVariable String guildId){
|
||||
return settingService.getValues(guildId);
|
||||
}
|
||||
}
|
||||
|
4
src/main/java/net/Broken/Api/Data/Settings/Value.java
Normal file
4
src/main/java/net/Broken/Api/Data/Settings/Value.java
Normal file
@ -0,0 +1,4 @@
|
||||
package net.Broken.Api.Data.Settings;
|
||||
|
||||
public record Value(String id, String value) {
|
||||
}
|
@ -3,7 +3,9 @@ package net.Broken.Api.Security.Expression;
|
||||
import net.Broken.Api.Security.Data.JwtPrincipal;
|
||||
import net.Broken.MainBot;
|
||||
import net.Broken.Tools.CacheTools;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import okhttp3.Cache;
|
||||
import org.springframework.security.access.expression.SecurityExpressionRoot;
|
||||
import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
|
||||
@ -29,6 +31,16 @@ public class CustomMethodSecurityExpressionRoot
|
||||
return CacheTools.getJdaUser(jwtPrincipal.user()).getMutualGuilds().contains(guild);
|
||||
}
|
||||
|
||||
public boolean canManageGuild(String guildId){
|
||||
JwtPrincipal jwtPrincipal = (JwtPrincipal) authentication.getPrincipal();
|
||||
Member member = MainBot.jda.getGuildById(guildId).getMemberById(jwtPrincipal.user().getDiscordId());
|
||||
return member.hasPermission(
|
||||
Permission.MANAGE_SERVER,
|
||||
Permission.MANAGE_PERMISSIONS,
|
||||
Permission.MANAGE_CHANNEL
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFilterObject(Object filterObject) {
|
||||
this.filterObject = filterObject;
|
||||
|
@ -1,7 +1,13 @@
|
||||
package net.Broken.Api.Services;
|
||||
|
||||
import liquibase.pro.packaged.V;
|
||||
import net.Broken.Api.Data.Settings.SettingDescriber;
|
||||
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 org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -10,6 +16,14 @@ import java.util.List;
|
||||
@Service
|
||||
public class SettingService {
|
||||
|
||||
public final GuildPreferenceRepository preferenceRepository;
|
||||
private final Logger logger = LogManager.getLogger();
|
||||
|
||||
|
||||
public SettingService(GuildPreferenceRepository preferenceRepository) {
|
||||
this.preferenceRepository = preferenceRepository;
|
||||
}
|
||||
|
||||
public List<SettingGroup> getSettingDescription() {
|
||||
List<SettingGroup> toReturn = new ArrayList<>();
|
||||
toReturn.add(getWelcomeGroup());
|
||||
@ -112,4 +126,81 @@ public class SettingService {
|
||||
mainField,
|
||||
fields);
|
||||
}
|
||||
|
||||
|
||||
public List<Value> getValues(String guildId) {
|
||||
GuildPreferenceEntity pref = preferenceRepository.findByGuildId(guildId).orElseGet(() -> {
|
||||
logger.info("[API] : Generate default guild pref");
|
||||
return preferenceRepository.save(GuildPreferenceEntity.getDefault(guildId));
|
||||
});
|
||||
List<Value> values = new ArrayList<>(getWelcomeValues(pref));
|
||||
values.addAll(getDefaultRoleValues(pref));
|
||||
values.addAll(getDailyValues(pref));
|
||||
values.addAll(getAutoVoiceChannelValues(pref));
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
private List<Value> getWelcomeValues(GuildPreferenceEntity pref) {
|
||||
List<Value> toReturn = new ArrayList<>();
|
||||
toReturn.add(new Value(
|
||||
"welcome_enable",
|
||||
String.valueOf(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",
|
||||
String.valueOf(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",
|
||||
String.valueOf(pref.isDefaultRole())
|
||||
)
|
||||
);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private List<Value> getAutoVoiceChannelValues(GuildPreferenceEntity pref) {
|
||||
List<Value> toReturn = new ArrayList<>();
|
||||
toReturn.add(new Value(
|
||||
"auto_voice",
|
||||
String.valueOf(pref.isAutoVoice())
|
||||
)
|
||||
);
|
||||
toReturn.add(new Value(
|
||||
"auto_voice_base_channel",
|
||||
pref.getAutoVoiceChannelID()
|
||||
)
|
||||
);
|
||||
toReturn.add(new Value(
|
||||
"auto_voice_channel_title",
|
||||
pref.getAutoVoiceChannelTitle()
|
||||
)
|
||||
);
|
||||
return toReturn;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user