From af8c2dea60995fe5f0ea1f06690f6dab01814044 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 17:48:54 +0100 Subject: [PATCH] Added mutations #162 --- kdb-bot/src/bot_data/model/auto_role.py | 8 +++ kdb-bot/src/bot_data/model/auto_role_rule.py | 8 +++ kdb-bot/src/bot_graphql/model/auto_role.gql | 13 ++++ .../src/bot_graphql/model/auto_role_rule.gql | 13 ++++ kdb-bot/src/bot_graphql/model/level.gql | 1 + kdb-bot/src/bot_graphql/model/mutation.gql | 3 + kdb-bot/src/bot_graphql/model/user.gql | 9 +++ .../mutations/auto_role_mutation.py | 57 ++++++++++++++++++ .../mutations/auto_role_rule_mutation.py | 59 +++++++++++++++++++ .../bot_graphql/mutations/level_mutation.py | 14 ++--- .../bot_graphql/mutations/user_mutation.py | 31 ++++++++++ 11 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py create mode 100644 kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py create mode 100644 kdb-bot/src/bot_graphql/mutations/user_mutation.py diff --git a/kdb-bot/src/bot_data/model/auto_role.py b/kdb-bot/src/bot_data/model/auto_role.py index 5b2862c8..16287e6e 100644 --- a/kdb-bot/src/bot_data/model/auto_role.py +++ b/kdb-bot/src/bot_data/model/auto_role.py @@ -37,10 +37,18 @@ class AutoRole(TableABC): def discord_channel_id(self) -> int: return self._discord_channel_id + @discord_channel_id.setter + def discord_channel_id(self, value: int): + self._discord_channel_id = value + @property def discord_message_id(self) -> int: return self._discord_message_id + @discord_message_id.setter + def discord_message_id(self, value: int): + self._discord_message_id = value + @staticmethod def get_select_all_string() -> str: return str( diff --git a/kdb-bot/src/bot_data/model/auto_role_rule.py b/kdb-bot/src/bot_data/model/auto_role_rule.py index fc79f622..f7166637 100644 --- a/kdb-bot/src/bot_data/model/auto_role_rule.py +++ b/kdb-bot/src/bot_data/model/auto_role_rule.py @@ -36,10 +36,18 @@ class AutoRoleRule(TableABC): def emoji_name(self) -> str: return self._discord_emoji_name + @emoji_name.setter + def emoji_name(self, value: str): + self._discord_emoji_name = value + @property def role_id(self) -> int: return self._discord_role_id + @role_id.setter + def role_id(self, value: int): + self._discord_role_id = value + @staticmethod def get_select_all_string() -> str: return str( diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql index 345f0df9..a45c925c 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role.gql @@ -19,4 +19,17 @@ input AutoRoleFilter { channel_name: String message_id: String server: ServerFilter +} + +type AutoRoleMutation { + create_auto_role(input: AutoRoleInput!): AutoRole + update_auto_role(input: AutoRoleInput!): AutoRole + delete_auto_role(id: ID): AutoRole +} + +input AutoRoleInput { + id: ID! + channel_id: String! + message_id: String! + server_id: ID! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql index 34c9ccdd..18460656 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql @@ -16,4 +16,17 @@ input AutoRoleRuleFilter { role_id: String role_name: String auto_role: AutoRoleFilter +} + +type AutoRoleRuleMutation { + create_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule + update_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule + delete_auto_role_rule(id: ID): AutoRoleRule +} + +input AutoRoleRuleInput { + id: ID! + emoji_name: String! + role_id: String! + auto_role_id: ID! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/level.gql b/kdb-bot/src/bot_graphql/model/level.gql index 323ce8a5..cae1e3ab 100644 --- a/kdb-bot/src/bot_graphql/model/level.gql +++ b/kdb-bot/src/bot_graphql/model/level.gql @@ -24,6 +24,7 @@ type LevelMutation { } input LevelInput { + id: ID name: String! color: String! min_xp: Int! diff --git a/kdb-bot/src/bot_graphql/model/mutation.gql b/kdb-bot/src/bot_graphql/model/mutation.gql index 1f3a002a..1eee7819 100644 --- a/kdb-bot/src/bot_graphql/model/mutation.gql +++ b/kdb-bot/src/bot_graphql/model/mutation.gql @@ -1,3 +1,6 @@ type Mutation { + auto_role: AutoRoleMutation + auto_role_rule: AutoRoleRuleMutation level: LevelMutation + user: UserMutation } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index 01c0305d..e175dab5 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -26,4 +26,13 @@ input UserFilter { ontime: Float level: LevelFilter server: ServerFilter +} + +type UserMutation { + update_user(input: UserInput!): User +} + +input UserInput { + id: ID! + xp: Int! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py new file mode 100644 index 00000000..29f999b2 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py @@ -0,0 +1,57 @@ +from cpl_core.database.context import DatabaseContextABC + +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.query_abc import QueryABC + + +class AutoRoleMutation(QueryABC): + def __init__( + self, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + db: DatabaseContextABC, + ): + QueryABC.__init__(self, "AutoRoleMutation") + + self._servers = servers + self._auto_roles = auto_roles + self._db = db + + self.set_field("create_auto_role", self.resolve_create_auto_role) + self.set_field("update_auto_role", self.resolve_update_auto_role) + self.set_field("delete_auto_role", self.resolve_delete_auto_role) + + def resolve_create_auto_role(self, *_, input: dict): + auto_role = AutoRole( + self._servers.get_server_by_id(input["server_id"]), input["channel_id"], input["message_id"] + ) + self._auto_roles.add_auto_role(auto_role) + self._db.save_changes() + + def get_new(x: AutoRole): + return ( + x.server.server_id == input["server_id"] + and x.discord_channel_id == input["channel_id"] + and x.discord_message_id == input["message_id"] + ) + + return self._auto_roles.get_auto_roles_by_server_id(auto_role.server.server_id).where(get_new) + + def resolve_update_auto_role(self, *_, input: dict): + auto_role = self._auto_roles.get_auto_role_by_id(input["id"]) + auto_role.discord_channel_id = input["channel_id"] if "channel_id" in input else auto_role.discord_channel_id + auto_role.discord_message_id = input["message_id"] if "message_id" in input else auto_role.discord_message_id + + self._auto_roles.update_auto_role(auto_role) + self._db.save_changes() + + auto_role = self._auto_roles.get_auto_role_by_id(input["id"]) + return auto_role + + def resolve_delete_auto_role(self, *_, id: int): + auto_role = self._auto_roles.get_auto_role_by_id(id) + self._auto_roles.delete_auto_role(auto_role) + self._db.save_changes() + return auto_role diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py new file mode 100644 index 00000000..37052692 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py @@ -0,0 +1,59 @@ +from cpl_core.database.context import DatabaseContextABC + +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_rule import AutoRoleRule +from bot_graphql.abc.query_abc import QueryABC + + +class AutoRoleRuleMutation(QueryABC): + def __init__( + self, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + db: DatabaseContextABC, + ): + QueryABC.__init__(self, "AutoRoleRuleMutation") + + self._servers = servers + self._auto_roles = auto_roles + self._db = db + + self.set_field("create_auto_role_rule", self.resolve_create_auto_role_rule) + self.set_field("update_auto_role_rule", self.resolve_update_auto_role_rule) + self.set_field("delete_auto_role_rule", self.resolve_delete_auto_role_rule) + + def resolve_create_auto_role_rule(self, *_, input: dict): + auto_role_rule = AutoRoleRule( + self._auto_roles.get_auto_role_by_id(input["auto_role_id"]), input["emoji_name"], input["role_id"] + ) + self._auto_roles.add_auto_role_rule(auto_role_rule) + self._db.save_changes() + + def get_new(x: AutoRoleRule): + return ( + x.auto_role.auto_role_id == input["auto_role_id"] + and x.emoji_name == input["emoji_name"] + and x.role_id == input["role_id"] + ) + + return self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.auto_role_id).where( + get_new + ) + + def resolve_update_auto_role_rule(self, *_, input: dict): + auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(input["id"]) + auto_role_rule.emoji_name = input["emoji_name"] if "emoji_name" in input else auto_role_rule.emoji_name + auto_role_rule.role_id = input["role_id"] if "role_id" in input else auto_role_rule.role_id + + self._auto_roles.update_auto_role_rule(auto_role_rule) + self._db.save_changes() + + auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(input["id"]) + return auto_role_rule + + def resolve_delete_auto_role_rule(self, *_, id: int): + auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(id) + self._auto_roles.delete_auto_role_rule(auto_role_rule) + self._db.save_changes() + return auto_role_rule diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index 083207cc..dd9681fc 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -20,8 +20,8 @@ class LevelMutation(QueryABC): self._db = db self.set_field("create_level", self.resolve_create_level) - self.set_field("update_level", self.resolve_create_level) - self.set_field("delete_level", self.resolve_create_level) + self.set_field("update_level", self.resolve_update_level) + self.set_field("delete_level", self.resolve_delete_level) def resolve_create_level(self, *_, input: dict): level = Level( @@ -34,7 +34,7 @@ class LevelMutation(QueryABC): self._levels.add_level(level) self._db.save_changes() - def get_new_server(l: Level): + def get_new_level(l: Level): return ( l.name == level.name and l.color == level.color @@ -42,10 +42,10 @@ class LevelMutation(QueryABC): and l.permissions == level.permissions ) - return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_server) + return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_level) - def resolve_update_level(self, *_, input): - level = self._levels.get_level_by_id(input.id) + def resolve_update_level(self, *_, input: dict): + level = self._levels.get_level_by_id(input["id"]) level.name = input["name"] if "name" in input else level.name level.color = input["color"] if "color" in input else level.color level.min_xp = input["min_xp"] if "min_xp" in input else level.min_xp @@ -54,7 +54,7 @@ class LevelMutation(QueryABC): self._levels.update_level(level) self._db.save_changes() - level = self._levels.get_level_by_id(input.id) + level = self._levels.get_level_by_id(input["id"]) return level def resolve_delete_level(self, *_, id: int): diff --git a/kdb-bot/src/bot_graphql/mutations/user_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_mutation.py new file mode 100644 index 00000000..38ef276e --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/user_mutation.py @@ -0,0 +1,31 @@ +from cpl_core.database.context import DatabaseContextABC + +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_graphql.abc.query_abc import QueryABC + + +class UserMutation(QueryABC): + def __init__( + self, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + db: DatabaseContextABC, + ): + QueryABC.__init__(self, "UserMutation") + + self._servers = servers + self._users = users + self._db = db + + self.set_field("update_user", self.resolve_update_user) + + def resolve_update_user(self, *_, input: dict): + user = self._users.get_user_by_id(input["id"]) + user.xp = input["xp"] if "xp" in input else user.xp + + self._users.update_user(user) + self._db.save_changes() + + user = self._users.get_user_by_id(input["id"]) + return user