From 290b5f38a7358f6da63ab19ed1607950da2cf0a2 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 28 Sep 2023 10:15:35 +0200 Subject: [PATCH] Added mutation #378 --- kdb-bot/src/bot_graphql/graphql/mutation.gql | 1 + kdb-bot/src/bot_graphql/graphql_module.py | 2 + kdb-bot/src/bot_graphql/mutation.py | 3 + .../mutations/short_role_name_mutation.py | 80 +++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 kdb-bot/src/bot_graphql/mutations/short_role_name_mutation.py diff --git a/kdb-bot/src/bot_graphql/graphql/mutation.gql b/kdb-bot/src/bot_graphql/graphql/mutation.gql index aa1d8cd6..9578fde0 100644 --- a/kdb-bot/src/bot_graphql/graphql/mutation.gql +++ b/kdb-bot/src/bot_graphql/graphql/mutation.gql @@ -5,6 +5,7 @@ type Mutation { user: UserMutation userJoinedGameServer: UserJoinedGameServerMutation achievement: AchievementMutation + shortRoleName: ShortRoleNameMutation technicianConfig: TechnicianConfigMutation serverConfig: ServerConfigMutation } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 0c903f08..e4e99a16 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -26,6 +26,7 @@ from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation from bot_graphql.mutations.server_config_mutation import ServerConfigMutation +from bot_graphql.mutations.short_role_name_mutation import ShortRoleNameMutation from bot_graphql.mutations.technician_config_mutation import TechnicianConfigMutation from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation @@ -142,6 +143,7 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, LevelMutation) services.add_transient(QueryABC, UserMutation) services.add_transient(QueryABC, AchievementMutation) + services.add_transient(QueryABC, ShortRoleNameMutation) services.add_transient(QueryABC, UserJoinedGameServerMutation) services.add_transient(QueryABC, TechnicianConfigMutation) services.add_transient(QueryABC, ServerConfigMutation) diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py index 4631e321..3dc7d599 100644 --- a/kdb-bot/src/bot_graphql/mutation.py +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -5,6 +5,7 @@ from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation from bot_graphql.mutations.server_config_mutation import ServerConfigMutation +from bot_graphql.mutations.short_role_name_mutation import ShortRoleNameMutation from bot_graphql.mutations.technician_config_mutation import TechnicianConfigMutation from bot_graphql.mutations.user_joined_game_server_mutation import UserJoinedGameServerMutation from bot_graphql.mutations.user_mutation import UserMutation @@ -21,6 +22,7 @@ class Mutation(MutationType): user_joined_game_server: UserJoinedGameServerMutation, technician_config: TechnicianConfigMutation, server_config: ServerConfigMutation, + short_role_name_mutation: ShortRoleNameMutation, ): MutationType.__init__(self) @@ -30,5 +32,6 @@ class Mutation(MutationType): self.set_field("user", lambda *_: user_mutation) self.set_field("achievement", lambda *_: achievement_mutation) self.set_field("userJoinedGameServer", lambda *_: user_joined_game_server) + self.set_field("shortRoleName", lambda *_: short_role_name_mutation) self.set_field("technicianConfig", lambda *_: technician_config) self.set_field("serverConfig", lambda *_: server_config) diff --git a/kdb-bot/src/bot_graphql/mutations/short_role_name_mutation.py b/kdb-bot/src/bot_graphql/mutations/short_role_name_mutation.py new file mode 100644 index 00000000..c8368112 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/short_role_name_mutation.py @@ -0,0 +1,80 @@ +from cpl_core.database.context import DatabaseContextABC +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.short_role_name_repository_abc import ShortRoleNameRepositoryABC +from bot_data.model.short_role_name import ShortRoleName +from bot_data.model.user_role_enum import UserRoleEnum +from bot_graphql.abc.query_abc import QueryABC +from modules.permission.service.permission_service import PermissionService + + +class ShortRoleNameMutation(QueryABC): + def __init__( + self, + servers: ServerRepositoryABC, + short_role_names: ShortRoleNameRepositoryABC, + bot: DiscordBotServiceABC, + db: DatabaseContextABC, + permissions: PermissionService, + ): + QueryABC.__init__(self, "ShortRoleNameMutation") + + self._servers = servers + self._short_role_names = short_role_names + self._bot = bot + self._db = db + self._permissions = permissions + + self.set_field("createShortRoleName", self.resolve_create_short_role_name) + self.set_field("updateShortRoleName", self.resolve_update_short_role_name) + self.set_field("deleteShortRoleName", self.resolve_delete_short_role_name) + + def resolve_create_short_role_name(self, *_, input: dict): + server = self._servers.get_server_by_id(input["serverId"]) + self._can_user_mutate_data(server, UserRoleEnum.admin) + + short_role_name = ShortRoleName( + input["shortName"], + input["roleId"], + input["position"], + server, + ) + self._short_role_names.add_short_role_name(short_role_name) + self._db.save_changes() + + def get_new_short_role_name(srn: ShortRoleName): + return ( + srn.short_name == short_role_name.short_name + and srn.role_id == short_role_name.role_id + and srn.position == short_role_name.position + ) + + return ( + self._short_role_names.get_short_role_names_by_server_id(short_role_name.server.id) + .where(get_new_short_role_name) + .last() + ) + + def resolve_update_short_role_name(self, *_, input: dict): + short_role_name = self._short_role_names.get_short_role_name_by_id(input["id"]) + self._can_user_mutate_data(short_role_name.server, UserRoleEnum.moderator) + + short_role_name.short_name = input["shortName"] if "shortName" in input else short_role_name.short_name + short_role_name.role_id = input["roleId"] if "roleId" in input else short_role_name.role_id + short_role_name.position = input["position"] if "position" in input else short_role_name.position + + self._short_role_names.update_short_role_name(short_role_name) + self._db.save_changes() + + short_role_name = self._short_role_names.get_short_role_name_by_id(input["id"]) + return short_role_name + + def resolve_delete_short_role_name(self, *_, id: int): + short_role_name = self._short_role_names.get_short_role_name_by_id(id) + self._can_user_mutate_data(short_role_name.server, UserRoleEnum.admin) + + self._short_role_names.delete_short_role_name(short_role_name) + self._db.save_changes() + + return short_role_name