diff --git a/kdb-bot/src/bot_graphql/filter/client_filter.py b/kdb-bot/src/bot_graphql/filter/client_filter.py new file mode 100644 index 00000000..2d1fd20a --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/client_filter.py @@ -0,0 +1,47 @@ +from cpl_query.extension import List + +from bot_data.model.client import Client +from bot_graphql.abc.filter_abc import FilterABC + + +class ClientFilter(FilterABC): + def __init__(self): + FilterABC.__init__(self) + + self._id = None + self._discord_id = None + self._name = None + self._server = None + + def from_dict(self, values: dict): + if "id" in values: + self._id = int(values["id"]) + + if "discord_id" in values: + self._id = int(values["discord_id"]) + + if "name" in values: + self._name = values["name"] + + if "server" in values: + from bot_graphql.filter.server_filter import ServerFilter + + server = ServerFilter() + server.from_dict(values["server"]) + self._server = server + + def filter(self, query: List[Client]) -> List[Client]: + if self._id is not None: + query = query.where(lambda x: x.client_id == self._id) + + if self._discord_id is not None: + query = query.where(lambda x: x.client_id == self._discord_id) + + 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/model/client.gql b/kdb-bot/src/bot_graphql/model/client.gql index 7330fc6f..284ca0d6 100644 --- a/kdb-bot/src/bot_graphql/model/client.gql +++ b/kdb-bot/src/bot_graphql/model/client.gql @@ -18,10 +18,5 @@ input ClientFilter { id: ID discord_id: String name: String - sent_message_count: Int - received_message_count: Int - deleted_message_count: Int - received_command_count: Int - moved_users_count: Int server: ServerFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql index a5e8f7b2..1f03360d 100644 --- a/kdb-bot/src/bot_graphql/model/server.gql +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -6,7 +6,7 @@ type Server implements TableQuery { auto_roles: [AutoRole] auto_role_count: Int - clients: [Client] + clients(filter: ClientFilter, page: Page, sort: Sort): [Client] client_count: Int users(filter: UserFilter): [User]