mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-04-06 17:22:29 +00:00
refactored channel exception to have a unified interface to form the exception message
replaced getOne with findById in order to get optionals and handle those some places still have the general abstracto run time exception
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
package dev.sheldan.abstracto.core.exception;
|
||||
|
||||
public class ChannelException extends AbstractoRunTimeException {
|
||||
public ChannelException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package dev.sheldan.abstracto.core.exception;
|
||||
|
||||
import dev.sheldan.abstracto.templating.Templatable;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ChannelNotFoundException extends AbstractoRunTimeException implements Templatable {
|
||||
|
||||
private Long channelId;
|
||||
private Long guildId;
|
||||
|
||||
public ChannelNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ChannelNotFoundException(Long channelId, Long guildId) {
|
||||
super("");
|
||||
this.channelId = channelId;
|
||||
this.guildId = guildId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return "channel_not_found_exception";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
HashMap<String, Long> param = new HashMap<>();
|
||||
param.put("channelId", this.channelId);
|
||||
param.put("guildID", this.guildId);
|
||||
return param;
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@ public interface ChannelGroupService {
|
||||
AChannelGroup createChannelGroup(String name, Long serverId);
|
||||
void deleteChannelGroup(String name, Long serverId);
|
||||
void addChannelToChannelGroup(String channelGroupName, TextChannel textChannel);
|
||||
void addChannelToChannelGroup(String channelGroupName, Long channelId);
|
||||
void addChannelToChannelGroup(String channelGroupName, Long channelId, Long serverId);
|
||||
void addChannelToChannelGroup(String channelGroupName, AChannel channel);
|
||||
void removeChannelFromChannelGroup(String channelGroupName, TextChannel textChannel);
|
||||
void removeChannelFromChannelGroup(String channelGroupName, Long channelId);
|
||||
void removeChannelFromChannelGroup(String channelGroupName, Long channelId, Long serverId);
|
||||
void removeChannelFromChannelGroup(String channelGroupName, AChannel channel);
|
||||
void disableCommandInChannelGroup(String commandName, String channelGroupName, Long serverId);
|
||||
void enableCommandInChannelGroup(String commandName, String channelGroupName, Long serverId);
|
||||
|
||||
@@ -4,8 +4,10 @@ import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannelType;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ChannelManagementService {
|
||||
AChannel loadChannel(Long id);
|
||||
Optional<AChannel> loadChannel(Long id);
|
||||
AChannel createChannel(Long id, AChannelType type, AServer server);
|
||||
AChannel markAsDeleted(Long id);
|
||||
boolean channelExists(Long id);
|
||||
|
||||
@@ -7,7 +7,7 @@ import net.dv8tion.jda.api.entities.Emote;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmoteManagementService {
|
||||
AEmote loadEmote(Long id);
|
||||
Optional<AEmote> loadEmote(Long id);
|
||||
AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, Long serverId) ;
|
||||
AEmote createCustomEmote(String name, String emoteKey, Long emoteId, Boolean animated, AServer server) ;
|
||||
AEmote createDefaultEmote(String name, String emoteKey, Long serverId) ;
|
||||
|
||||
@@ -3,8 +3,10 @@ package dev.sheldan.abstracto.core.service.management;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface RoleManagementService {
|
||||
ARole createRole(Long id, AServer server);
|
||||
ARole findRole(Long id, AServer server);
|
||||
Optional<ARole> findRole(Long id, AServer server);
|
||||
void markDeleted(ARole role);
|
||||
}
|
||||
|
||||
@@ -6,12 +6,13 @@ import dev.sheldan.abstracto.core.models.database.AUserInAServer;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserInServerManagementService {
|
||||
AUserInAServer loadUser(Long serverId, Long userId);
|
||||
AUserInAServer loadUser(AServer server, AUser user);
|
||||
AUserInAServer loadUser(Member member);
|
||||
AUserInAServer loadUser(Long userInServerId);
|
||||
Optional<AUserInAServer> loadUser(Long userInServerId);
|
||||
AUserInAServer createUserInServer(Member member);
|
||||
AUserInAServer createUserInServer(Long guildId, Long userId);
|
||||
List<AUserInAServer> getUserInAllServers(Long userId);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package dev.sheldan.abstracto.core.utils;
|
||||
|
||||
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
|
||||
import dev.sheldan.abstracto.core.exception.ChannelNotFoundException;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
|
||||
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
|
||||
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
|
||||
@@ -14,6 +16,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -35,11 +38,13 @@ public class ContextUtils {
|
||||
m = clazz.getMethod("builder");
|
||||
UserInitiatedServerContext.UserInitiatedServerContextBuilder<?, ?> builder = (UserInitiatedServerContext.UserInitiatedServerContextBuilder) m.invoke(null, null);
|
||||
AUserInAServer aUserInAServer = userInServerManagementService.loadUser(message.getServerId(), message.getAuthorId());
|
||||
Optional<AChannel> channelOptional = channelManagementService.loadChannel(message.getChannelId());
|
||||
AChannel channel = channelOptional.orElseThrow(() -> new ChannelNotFoundException(message.getChannelId(), message.getServerId()));
|
||||
return builder
|
||||
.member(guildChannelMember.getMember())
|
||||
.guild(guildChannelMember.getGuild())
|
||||
.messageChannel(guildChannelMember.getTextChannel())
|
||||
.channel(channelManagementService.loadChannel(message.getChannelId()))
|
||||
.channel(channel)
|
||||
.server(aUserInAServer.getServerReference())
|
||||
.aUserInAServer(aUserInAServer)
|
||||
.user(aUserInAServer.getUserReference())
|
||||
|
||||
@@ -17,6 +17,8 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -47,7 +49,8 @@ public class ContextUtilsTest {
|
||||
AUserInAServer aUserInAServer = AUserInAServer.builder().userReference(AUser.builder().id(AUTHOR_ID).build()).serverReference(server).build();
|
||||
when(userInServerManagementService.loadUser(eq(SERVER_ID), eq(AUTHOR_ID))).thenReturn(aUserInAServer);
|
||||
AChannel channel = AChannel.builder().id(CHANNEL_ID).build();
|
||||
when(channelManagementService.loadChannel(eq(CHANNEL_ID))).thenReturn(channel);
|
||||
Optional<AChannel> op = Optional.of(channel);
|
||||
when(channelManagementService.loadChannel(eq(CHANNEL_ID))).thenReturn(op);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user