Added more queries #162

This commit is contained in:
Sven Heidemann 2023-01-15 10:59:25 +01:00
parent b9e66bba9d
commit 5efb1da0b8
4 changed files with 97 additions and 11 deletions

View File

@ -1,8 +1,6 @@
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):

View File

@ -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)

View File

@ -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

View File

@ -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()