diff --git a/kdb-bot/src/bot_data/migration/achievements_migration.py b/kdb-bot/src/bot_data/migration/achievements_migration.py index 160d379c..294d1b8b 100644 --- a/kdb-bot/src/bot_data/migration/achievements_migration.py +++ b/kdb-bot/src/bot_data/migration/achievements_migration.py @@ -20,11 +20,11 @@ class AchievementsMigration(MigrationABC): f""" CREATE TABLE IF NOT EXISTS `Achievements` ( `Id` BIGINT NOT NULL AUTO_INCREMENT, - `ServerId` BIGINT, `Name` VARCHAR(255) NOT NULL, `Attribute` VARCHAR(255) NOT NULL, `Operator` VARCHAR(2) NOT NULL, `Value` VARCHAR(255) NOT NULL, + `ServerId` BIGINT, `CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), `LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), PRIMARY KEY(`Id`), diff --git a/kdb-bot/src/bot_data/model/achievement.py b/kdb-bot/src/bot_data/model/achievement.py index b9a44089..a4c8b831 100644 --- a/kdb-bot/src/bot_data/model/achievement.py +++ b/kdb-bot/src/bot_data/model/achievement.py @@ -41,6 +41,14 @@ class Achievement(TableABC): def name(self, value: str): self._name = value + @property + def attribute(self) -> str: + return self._attribute + + @attribute.setter + def attribute(self, value: str): + self._attribute = value + @property def operator(self) -> str: return self._operator diff --git a/kdb-bot/src/bot_data/service/achievements_repository_service.py b/kdb-bot/src/bot_data/service/achievements_repository_service.py index d910a6c2..d9c157e2 100644 --- a/kdb-bot/src/bot_data/service/achievements_repository_service.py +++ b/kdb-bot/src/bot_data/service/achievements_repository_service.py @@ -23,11 +23,11 @@ class AchievementRepositoryService(AchievementRepositoryABC): def _from_result(self, result: tuple): return Achievement( + result[1], + result[2], result[3], result[4], - result[5], - result[6], - self._servers.get_server_by_id(result[2]), + self._servers.get_server_by_id(result[5]), result[6], result[7], id=result[0], diff --git a/kdb-bot/src/bot_graphql/mutations/achievement_mutation.py b/kdb-bot/src/bot_graphql/mutations/achievement_mutation.py index c7daa8c8..99459515 100644 --- a/kdb-bot/src/bot_graphql/mutations/achievement_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/achievement_mutation.py @@ -3,6 +3,7 @@ from cpl_discord.service import DiscordBotServiceABC from bot_data.abc.achievement_repository_abc import AchievementRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.achievement import Achievement from bot_data.model.user_role_enum import UserRoleEnum from bot_graphql.abc.query_abc import QueryABC from modules.permission.service.permission_service import PermissionService @@ -25,7 +26,34 @@ class AchievementMutation(QueryABC): self._db = db self._permissions = permissions + self.set_field("createAchievement", self.resolve_create_achievement) self.set_field("updateAchievement", self.resolve_update_achievement) + self.set_field("deleteAchievement", self.resolve_delete_achievement) + + def resolve_create_achievement(self, *_, input: dict): + server = self._servers.get_server_by_id(input["serverId"]) + self._can_user_mutate_data(server, UserRoleEnum.admin) + + achievement = Achievement( + input["name"], + input["attribute"], + input["operator"], + input["value"], + server, + ) + self._achievements.add_achievement(achievement) + self._db.save_changes() + + def get_new_achievement(a: Achievement): + return ( + a.name == achievement.name + and a.attribute == achievement.attribute + and a.operator == achievement.operator + and a.value == achievement.value + and a.server.id == server.id + ) + + return self._achievements.get_achievements_by_server_id(achievement.server.id).where(get_new_achievement).last() def resolve_update_achievement(self, *_, input: dict): achievement = self._achievements.get_achievement_by_id(input["id"]) @@ -41,3 +69,12 @@ class AchievementMutation(QueryABC): achievement = self._achievements.get_achievement_by_id(input["id"]) return achievement + + def resolve_delete_achievement(self, *_, id: int): + achievement = self._achievements.get_achievement_by_id(id) + self._can_user_mutate_data(achievement.server, UserRoleEnum.admin) + + self._achievements.delete_achievement(achievement) + self._db.save_changes() + + return achievement diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index 47f61e7c..215538b7 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -50,6 +50,7 @@ class LevelMutation(QueryABC): and l.color == level.color and l.min_xp == level.min_xp and l.permissions == level.permissions + and l.server.id == server.id ) self._bot.loop.create_task(self._level_seeder.seed()) diff --git a/kdb-bot/src/bot_graphql/queries/achievement_query.py b/kdb-bot/src/bot_graphql/queries/achievement_query.py index c9782973..46ee137d 100644 --- a/kdb-bot/src/bot_graphql/queries/achievement_query.py +++ b/kdb-bot/src/bot_graphql/queries/achievement_query.py @@ -11,9 +11,9 @@ class AchievementQuery(DataQueryWithHistoryABC): ): DataQueryWithHistoryABC.__init__(self, "Achievement", "AchievementsHistory", UserHistory, db) - self.set_field("id", lambda x: x.id) - self.set_field("name", lambda x: x.name) - self.set_field("attribute", lambda x: x.attribute) - self.set_field("operator", lambda x: x.operator) - self.set_field("value", lambda x: x.value) - self.set_field("server", lambda x: x.server) + self.set_field("id", lambda x, *_: x.id) + self.set_field("name", lambda x, *_: x.name) + self.set_field("attribute", lambda x, *_: x.attribute) + self.set_field("operator", lambda x, *_: x.operator) + self.set_field("value", lambda x, *_: x.value) + self.set_field("server", lambda x, *_: x.server)