Improved filters #162

This commit is contained in:
Sven Heidemann 2023-02-07 19:09:27 +01:00
parent 36fd3c73b9
commit 12f8f669ed
7 changed files with 39 additions and 10 deletions

View File

@ -1,3 +1,4 @@
from cpl_core.dependency_injection import ServiceProviderABC
from cpl_discord.service import DiscordBotServiceABC from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List from cpl_query.extension import List
@ -6,8 +7,9 @@ from bot_graphql.abc.filter_abc import FilterABC
class AutoRoleRuleFilter(FilterABC): class AutoRoleRuleFilter(FilterABC):
def __init__(self, bot: DiscordBotServiceABC): def __init__(self, services: ServiceProviderABC, bot: DiscordBotServiceABC):
FilterABC.__init__(self) FilterABC.__init__(self)
self._services = services
self._bot = bot self._bot = bot
self._id = None self._id = None
@ -32,9 +34,8 @@ class AutoRoleRuleFilter(FilterABC):
if "auto_role" in values: if "auto_role" in values:
from bot_graphql.filter.auto_role_filter import AutoRoleFilter from bot_graphql.filter.auto_role_filter import AutoRoleFilter
auto_role = AutoRoleFilter(self._bot) self._auto_role: AutoRoleFilter = self._services.get_service(AutoRoleFilter)
auto_role.from_dict(values["auto_role"]) self._auto_role.from_dict(values["auto_role"])
self._auto_role = auto_role
def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]: def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]:
if self._id is not None: if self._id is not None:

View File

@ -1,3 +1,4 @@
from cpl_core.dependency_injection import ServiceProviderABC
from cpl_query.extension import List from cpl_query.extension import List
from bot_data.model.level import Level from bot_data.model.level import Level
@ -5,12 +6,14 @@ from bot_graphql.abc.filter_abc import FilterABC
class LevelFilter(FilterABC): class LevelFilter(FilterABC):
def __init__(self): def __init__(self, services: ServiceProviderABC):
FilterABC.__init__(self) FilterABC.__init__(self)
self._services = services
self._id = None self._id = None
self._name = None self._name = None
self._server_id = None self._server = None
def from_dict(self, values: dict): def from_dict(self, values: dict):
if "id" in values: if "id" in values:
@ -19,6 +22,12 @@ class LevelFilter(FilterABC):
if "name" in values: if "name" in values:
self._name = values["name"] 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]: def filter(self, query: List[Level]) -> List[Level]:
if self._id is not None: if self._id is not None:
query = query.where(lambda x: x.id == self._id) query = query.where(lambda x: x.id == self._id)
@ -26,4 +35,8 @@ class LevelFilter(FilterABC):
if self._name is not None: 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()) 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 return query

View File

@ -36,18 +36,29 @@ class UserFilter(FilterABC):
def from_dict(self, values: dict): def from_dict(self, values: dict):
if "id" in values: if "id" in values:
self._id = int(values["id"]) self._id = int(values["id"])
if "discord_id" in values: if "discord_id" in values:
self._discord_id = int(values["discord_id"]) self._discord_id = int(values["discord_id"])
if "name" in values: if "name" in values:
self._name = values["name"] self._name = values["name"]
if "xp" in values: if "xp" in values:
self._xp = int(values["xp"]) self._xp = int(values["xp"])
if "ontime" in values: if "ontime" in values:
self._ontime = int(values["ontime"]) self._ontime = int(values["ontime"])
if "level" in values: if "level" in values:
self._level: LevelFilter = self._services.get_service(LevelFilter) self._level: LevelFilter = self._services.get_service(LevelFilter)
self._level.from_dict(values["level"]) 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]: def filter(self, query: List[User]) -> List[User]:
if self._id is not None: if self._id is not None:
query = query.where(lambda x: x.id == self._id) query = query.where(lambda x: x.id == self._id)

View File

@ -10,6 +10,7 @@ from bot_graphql.abc.filter_abc import FilterABC
from bot_graphql.abc.query_abc import QueryABC from bot_graphql.abc.query_abc import QueryABC
from bot_graphql.filter.auto_role_filter import AutoRoleFilter from bot_graphql.filter.auto_role_filter import AutoRoleFilter
from bot_graphql.filter.auto_role_rule_filter import AutoRoleRuleFilter 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.level_filter import LevelFilter
from bot_graphql.filter.server_filter import ServerFilter from bot_graphql.filter.server_filter import ServerFilter
from bot_graphql.filter.user_filter import UserFilter from bot_graphql.filter.user_filter import UserFilter
@ -54,8 +55,9 @@ class GraphQLModule(ModuleABC):
services.add_singleton(FilterABC, AutoRoleFilter) services.add_singleton(FilterABC, AutoRoleFilter)
services.add_singleton(FilterABC, AutoRoleRuleFilter) services.add_singleton(FilterABC, AutoRoleRuleFilter)
services.add_singleton(FilterABC, ClientFilter)
services.add_singleton(FilterABC, LevelFilter)
services.add_singleton(FilterABC, ServerFilter) services.add_singleton(FilterABC, ServerFilter)
services.add_singleton(FilterABC, UserFilter) services.add_singleton(FilterABC, UserFilter)
services.add_singleton(FilterABC, LevelFilter)
services.add_transient(SeederService) services.add_transient(SeederService)

View File

@ -14,6 +14,7 @@ type Level implements TableQuery {
input LevelFilter { input LevelFilter {
id: ID id: ID
name: String name: String
server: ServerFilter
} }
type LevelMutation { type LevelMutation {

View File

@ -3,16 +3,16 @@ type Server implements TableQuery {
discord_id: String discord_id: String
name: String name: String
auto_roles: [AutoRole] auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole]
auto_role_count: Int auto_role_count: Int
clients(filter: ClientFilter, page: Page, sort: Sort): [Client] clients(filter: ClientFilter, page: Page, sort: Sort): [Client]
client_count: Int client_count: Int
users(filter: UserFilter): [User] users(filter: UserFilter, page: Page, sort: Sort): [User]
user_count: Int user_count: Int
levels: [Level] levels(filter: LevelFilter, page: Page, sort: Sort): [Level]
level_count: Int level_count: Int
created_at: String created_at: String

View File

@ -25,4 +25,5 @@ input UserFilter {
xp: Int xp: Int
ontime: Float ontime: Float
level: LevelFilter level: LevelFilter
server: ServerFilter
} }