From b8484185e984fe01e2ca06707ba800ca72590862 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 09:51:31 +0100 Subject: [PATCH] Improved level stuff #162 --- .../src/bot_graphql/filter/level_filter.py | 8 ++-- .../bot_graphql/mutations/level_mutation.py | 40 +++++++++++++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index 314b9112..34126cfe 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -16,6 +16,9 @@ class LevelFilter(FilterABC): if "id" in values: self._id = values["id"] + if "name" in values: + self._id = values["name"] + def filter(self, query: List[Level]) -> List[Level]: if self._id is not None: query = query.where(lambda x: x.id == self._id) @@ -26,7 +29,4 @@ class LevelFilter(FilterABC): # if self._server_id is not None: # query = query.where(lambda x: x.server.server_id == self._server_id) - skip = self.page_size * self.page_index - result = query.skip(skip).take(self.page_size) - - return result + return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index 5ede5953..083207cc 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -1,3 +1,5 @@ +from cpl_core.database.context import DatabaseContextABC + from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.level import Level @@ -5,11 +7,17 @@ from bot_graphql.abc.query_abc import QueryABC class LevelMutation(QueryABC): - def __init__(self, servers: ServerRepositoryABC, levels: LevelRepositoryABC): + def __init__( + self, + servers: ServerRepositoryABC, + levels: LevelRepositoryABC, + db: DatabaseContextABC, + ): QueryABC.__init__(self, "LevelMutation") self._servers = servers self._levels = levels + self._db = db self.set_field("create_level", self.resolve_create_level) self.set_field("update_level", self.resolve_create_level) @@ -23,10 +31,34 @@ class LevelMutation(QueryABC): int(input["permissions"]), self._servers.get_server_by_id(input["server_id"]), ) - return level + self._levels.add_level(level) + self._db.save_changes() + + def get_new_server(l: Level): + return ( + l.name == level.name + and l.color == level.color + and l.min_xp == level.min_xp + and l.permissions == level.permissions + ) + + return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_server) def resolve_update_level(self, *_, input): - return self._levels.get_level_by_id(input.id) + level = self._levels.get_level_by_id(input.id) + level.name = input["name"] if "name" in input else level.name + level.color = input["color"] if "color" in input else level.color + level.min_xp = input["min_xp"] if "min_xp" in input else level.min_xp + level.permissions = input["permissions"] if "permissions" in input else level.permissions + + self._levels.update_level(level) + self._db.save_changes() + + level = self._levels.get_level_by_id(input.id) + return level def resolve_delete_level(self, *_, id: int): - return self._levels.get_level_by_id(id) + level = self._levels.get_level_by_id(id) + self._levels.delete_level(level) + self._db.save_changes() + return level