From 5efb1da0b8591836b0a568c37a8df5c5a24b28d0 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 10:59:25 +0100 Subject: [PATCH] Added more queries #162 --- kdb-bot/src/bot_data/model/known_user.py | 14 +++--- kdb-bot/src/bot_graphql/filter/user_filter.py | 47 +++++++++++++++++++ kdb-bot/src/bot_graphql/model.gql | 28 ++++++++++- kdb-bot/src/bot_graphql/query.py | 19 +++++++- 4 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/filter/user_filter.py diff --git a/kdb-bot/src/bot_data/model/known_user.py b/kdb-bot/src/bot_data/model/known_user.py index be64d9d5..92aa5f4d 100644 --- a/kdb-bot/src/bot_data/model/known_user.py +++ b/kdb-bot/src/bot_data/model/known_user.py @@ -1,17 +1,15 @@ from datetime import datetime -from typing import Optional -from cpl_core.database import TableABC -from bot_data.model.server import Server +from cpl_core.database import TableABC class KnownUser(TableABC): def __init__( - self, - dc_id: int, - created_at: datetime = None, - modified_at: datetime = None, - id=0, + self, + dc_id: int, + created_at: datetime = None, + modified_at: datetime = None, + id=0, ): self._known_user_id = id self._discord_id = dc_id diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py new file mode 100644 index 00000000..bb3ddd02 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -0,0 +1,47 @@ +from cpl_query.extension import List + +from bot_data.model.user import User +from bot_graphql.abc.filter_abc import FilterABC + + +class UserFilter(FilterABC): + def __init__( + self, + ): + FilterABC.__init__(self) + + self._id = None + self._discord_id = None + self._name = None + self._xp = None + self._ontime = None + self._level = None + + def from_dict(self, values: dict): + if 'id' in values: + self._id = values['id'] + if 'discord_id' in values: + self._discord_id = values['discord_id'] + if 'name' in values: + self._name = values['name'] + if 'xp' in values: + self._xp = values['xp'] + if 'ontime' in values: + self._ontime = values['ontime'] + if 'level' in values: + self._level = values['level'] + + def filter(self, query: List[User]) -> List[User]: + if self._id is not None: + query = query.where(lambda x: x.id == self._id) + + if self._discord_id is not None: + query = query.where(lambda x: x.discord_id == self._discord_id) + + if self._xp is not None: + query = query.where(lambda x: x.xp == self._xp) + + # if self._ontime is not None: + # query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) + + return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index e30efb9b..fc6be69c 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -10,7 +10,17 @@ type Mutation { type Query { servers(filter: ServerFilter): [Server] server_count: Int - known_users: [User] + + known_users: [KnownUser] + known_user_count: Int +} + +type KnownUser implements TableQuery { + id: ID + discord_id: String + + created_at: String + modified_at: String } input ServerFilter { @@ -39,6 +49,7 @@ type Server implements TableQuery { type Client implements TableQuery { id: ID discord_id: String + name: String sent_message_count: Int received_message_count: Int deleted_message_count: Int @@ -51,6 +62,20 @@ type Client implements TableQuery { modified_at: String } +input UserFilter { + id: ID + discord_id: String + name: String + xp: Int + ontime: Int + level: LevelFilter + + page_index: Int + page_size: Int + sort_direction: String + sort_column: String +} + type User implements TableQuery { id: ID discord_id: String @@ -83,6 +108,7 @@ type UserJoinedVoiceChannel implements TableQuery { id: ID channel_id: String user: User + server: Server joined_on: String leaved_on: String diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index 8e234f14..63209c78 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -1,18 +1,27 @@ from ariadne import QueryType -from bot_data.service.server_repository_service import ServerRepositoryService +from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_graphql.abc.filter_abc import FilterABC from bot_graphql.filter.server_filter import ServerFilter class Query(QueryType): - def __init__(self, servers: ServerRepositoryService): + def __init__( + self, + servers: ServerRepositoryABC, + known_users: KnownUserRepositoryABC, + ): QueryType.__init__(self) self._servers = servers + self._known_users = known_users self.set_field("servers", self.resolve_servers) self.set_field("server_count", self.resolve_server_count) + self.set_field("known_users", self.resolve_servers) + self.set_field("known_users_count", self.resolve_server_count) + @FilterABC.resolve_filter_annotation def resolve_servers(self, *_, filter: ServerFilter = None): if filter is not None: @@ -22,3 +31,9 @@ class Query(QueryType): def resolve_server_count(self, *_): return self._servers.get_servers().count() + + def resolve_known_users(self, *_): + return self._known_users.get_users() + + def resolve_known_users_count(self, *_): + return self._known_users.get_users().count()