From 12f8f669ed46531f979fe1ad6d0a9b232bc04f6b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 7 Feb 2023 19:09:27 +0100 Subject: [PATCH] Improved filters #162 --- .../bot_graphql/filter/auto_role_rule_filter.py | 9 +++++---- kdb-bot/src/bot_graphql/filter/level_filter.py | 17 +++++++++++++++-- kdb-bot/src/bot_graphql/filter/user_filter.py | 11 +++++++++++ kdb-bot/src/bot_graphql/graphql_module.py | 4 +++- kdb-bot/src/bot_graphql/model/level.gql | 1 + kdb-bot/src/bot_graphql/model/server.gql | 6 +++--- kdb-bot/src/bot_graphql/model/user.gql | 1 + 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index 65e54eb0..eb2112e5 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -1,3 +1,4 @@ +from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List @@ -6,8 +7,9 @@ from bot_graphql.abc.filter_abc import FilterABC class AutoRoleRuleFilter(FilterABC): - def __init__(self, bot: DiscordBotServiceABC): + def __init__(self, services: ServiceProviderABC, bot: DiscordBotServiceABC): FilterABC.__init__(self) + self._services = services self._bot = bot self._id = None @@ -32,9 +34,8 @@ class AutoRoleRuleFilter(FilterABC): if "auto_role" in values: from bot_graphql.filter.auto_role_filter import AutoRoleFilter - auto_role = AutoRoleFilter(self._bot) - auto_role.from_dict(values["auto_role"]) - self._auto_role = auto_role + self._auto_role: AutoRoleFilter = self._services.get_service(AutoRoleFilter) + self._auto_role.from_dict(values["auto_role"]) def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]: if self._id is not None: diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index f4611107..c34ae63f 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -1,3 +1,4 @@ +from cpl_core.dependency_injection import ServiceProviderABC from cpl_query.extension import List from bot_data.model.level import Level @@ -5,12 +6,14 @@ from bot_graphql.abc.filter_abc import FilterABC class LevelFilter(FilterABC): - def __init__(self): + def __init__(self, services: ServiceProviderABC): FilterABC.__init__(self) + self._services = services + self._id = None self._name = None - self._server_id = None + self._server = None def from_dict(self, values: dict): if "id" in values: @@ -19,6 +22,12 @@ class LevelFilter(FilterABC): if "name" in values: self._name = values["name"] + if "server" in values: + from bot_graphql.filter.server_filter import ServerFilter + + self._server: ServerFilter = self._services.get_service(LevelFilter) + self._server.from_dict(values["server"]) + def filter(self, query: List[Level]) -> List[Level]: if self._id is not None: query = query.where(lambda x: x.id == self._id) @@ -26,4 +35,8 @@ class LevelFilter(FilterABC): if self._name is not None: query = query.where(lambda x: self._name.lower() == x.name.lower() or self._name.lower() in x.name.lower()) + if self._server is not None: + servers = self._server.filter(query.select(lambda x: x.server)).select(lambda x: x.server_id) + query = query.where(lambda x: x.server.server_id in servers) + return query diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index d92531df..8f772152 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -36,18 +36,29 @@ class UserFilter(FilterABC): def from_dict(self, values: dict): if "id" in values: self._id = int(values["id"]) + if "discord_id" in values: self._discord_id = int(values["discord_id"]) + if "name" in values: self._name = values["name"] + if "xp" in values: self._xp = int(values["xp"]) + if "ontime" in values: self._ontime = int(values["ontime"]) + if "level" in values: self._level: LevelFilter = self._services.get_service(LevelFilter) self._level.from_dict(values["level"]) + if "server" in values: + from bot_graphql.filter.server_filter import ServerFilter + + self._server: ServerFilter = self._services.get_service(LevelFilter) + self._server.from_dict(values["server"]) + def filter(self, query: List[User]) -> List[User]: if self._id is not None: query = query.where(lambda x: x.id == self._id) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 0d419007..9552d6a7 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -10,6 +10,7 @@ from bot_graphql.abc.filter_abc import FilterABC from bot_graphql.abc.query_abc import QueryABC from bot_graphql.filter.auto_role_filter import AutoRoleFilter from bot_graphql.filter.auto_role_rule_filter import AutoRoleRuleFilter +from bot_graphql.filter.client_filter import ClientFilter from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.server_filter import ServerFilter from bot_graphql.filter.user_filter import UserFilter @@ -54,8 +55,9 @@ class GraphQLModule(ModuleABC): services.add_singleton(FilterABC, AutoRoleFilter) services.add_singleton(FilterABC, AutoRoleRuleFilter) + services.add_singleton(FilterABC, ClientFilter) + services.add_singleton(FilterABC, LevelFilter) services.add_singleton(FilterABC, ServerFilter) services.add_singleton(FilterABC, UserFilter) - services.add_singleton(FilterABC, LevelFilter) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model/level.gql b/kdb-bot/src/bot_graphql/model/level.gql index 20588aa6..323ce8a5 100644 --- a/kdb-bot/src/bot_graphql/model/level.gql +++ b/kdb-bot/src/bot_graphql/model/level.gql @@ -14,6 +14,7 @@ type Level implements TableQuery { input LevelFilter { id: ID name: String + server: ServerFilter } type LevelMutation { diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql index 1f03360d..f3543fd3 100644 --- a/kdb-bot/src/bot_graphql/model/server.gql +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -3,16 +3,16 @@ type Server implements TableQuery { discord_id: String name: String - auto_roles: [AutoRole] + auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] auto_role_count: Int clients(filter: ClientFilter, page: Page, sort: Sort): [Client] client_count: Int - users(filter: UserFilter): [User] + users(filter: UserFilter, page: Page, sort: Sort): [User] user_count: Int - levels: [Level] + levels(filter: LevelFilter, page: Page, sort: Sort): [Level] level_count: Int created_at: String diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index 6ec8f992..cf075fa1 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -25,4 +25,5 @@ input UserFilter { xp: Int ontime: Float level: LevelFilter + server: ServerFilter } \ No newline at end of file