From 8256ebed719dbe1ef572201f37e85de2da7f4d16 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 16:24:08 +0100 Subject: [PATCH] Added auto role query #162 --- kdb-bot/src/bot_graphql/graphql_module.py | 4 ++ kdb-bot/src/bot_graphql/model.gql | 23 +++++++++++ .../bot_graphql/queries/auto_role_query.py | 40 +++++++++++++++++++ .../queries/auto_role_rule_query.py | 33 +++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 kdb-bot/src/bot_graphql/queries/auto_role_query.py create mode 100644 kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 37ca29fd..decdf095 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -10,6 +10,8 @@ from bot_graphql.abc.query_abc import QueryABC from bot_graphql.graphql_service import GraphQLService from bot_graphql.mutation import Mutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.queries.auto_role_query import AutoRoleQuery +from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery from bot_graphql.queries.client_query import ClientQuery from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery @@ -39,5 +41,7 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, UserQuery) services.add_transient(QueryABC, UserJoinedServerQuery) services.add_transient(QueryABC, UserJoinedVoiceChannelQuery) + services.add_transient(QueryABC, AutoRoleQuery) + services.add_transient(QueryABC, AutoRoleRuleQuery) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index fc6be69c..77661c7f 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -62,6 +62,29 @@ type Client implements TableQuery { modified_at: String } +type AutoRole implements TableQuery { + id: ID + channel_id: String + message_id: String + + server: Server + rules: [AutoRoleRule] + + created_at: String + modified_at: String +} + +type AutoRoleRule implements TableQuery { + id: ID + emoji_name: String + role_id: String + + auto_role: AutoRole + + created_at: String + modified_at: String +} + input UserFilter { id: ID discord_id: String diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py new file mode 100644 index 00000000..96615e6e --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -0,0 +1,40 @@ +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.auto_role import AutoRole +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class AutoRoleQuery(DataQueryABC): + def __init__( + self, + auto_role_rules: AutoRoleRepositoryABC, + servers: ServerRepositoryABC, + ): + DataQueryABC.__init__(self, "AutoRole") + + self._auto_role_rules = auto_role_rules + self._servers = servers + + self.set_field("id", self.resolve_id) + self.set_field("channel_id", self.resolve_channel_id) + self.set_field("message_id", self.resolve_message_id) + self.set_field("server", self.resolve_server) + self.set_field("rules", self.resolve_rules) + + @staticmethod + def resolve_id(x: AutoRole, *_): + return x.auto_role_id + + @staticmethod + def resolve_channel_id(x: AutoRole, *_): + return x.discord_channel_id + + @staticmethod + def resolve_message_id(x: AutoRole, *_): + return x.discord_message_id + + def resolve_server(self, x: AutoRole, *_): + return self._servers.get_server_by_id(x.server_id) + + def resolve_rules(self, x: AutoRole, *_): + return self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py new file mode 100644 index 00000000..1cbd870c --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py @@ -0,0 +1,33 @@ +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.model.auto_role_rule import AutoRoleRule +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class AutoRoleRuleQuery(DataQueryABC): + def __init__( + self, + auto_roles: AutoRoleRepositoryABC, + ): + DataQueryABC.__init__(self, "AutoRoleRule") + + self._auto_roles = auto_roles + + self.set_field("id", self.resolve_id) + self.set_field("emoji_name", self.resolve_emoji_name) + self.set_field("role_id", self.resolve_role_id) + self.set_field("auto_role", self.resolve_auto_role) + + @staticmethod + def resolve_id(x: AutoRoleRule, *_): + return x.auto_role_rule_id + + @staticmethod + def resolve_emoji_name(x: AutoRoleRule, *_): + return x.emoji_name + + @staticmethod + def resolve_role_id(x: AutoRoleRule, *_): + return x.role_id + + def resolve_auto_role(self, x: AutoRoleRule, *_): + return self._auto_roles.get_auto_role_by_id(x.auto_role_id)