Fixed achievement query #268_achievements

This commit is contained in:
Sven Heidemann 2023-07-12 11:23:14 +02:00
parent 2c9434396d
commit 0e5ec588fc
6 changed files with 56 additions and 10 deletions

View File

@ -20,11 +20,11 @@ class AchievementsMigration(MigrationABC):
f""" f"""
CREATE TABLE IF NOT EXISTS `Achievements` ( CREATE TABLE IF NOT EXISTS `Achievements` (
`Id` BIGINT NOT NULL AUTO_INCREMENT, `Id` BIGINT NOT NULL AUTO_INCREMENT,
`ServerId` BIGINT,
`Name` VARCHAR(255) NOT NULL, `Name` VARCHAR(255) NOT NULL,
`Attribute` VARCHAR(255) NOT NULL, `Attribute` VARCHAR(255) NOT NULL,
`Operator` VARCHAR(2) NOT NULL, `Operator` VARCHAR(2) NOT NULL,
`Value` VARCHAR(255) NOT NULL, `Value` VARCHAR(255) NOT NULL,
`ServerId` BIGINT,
`CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6), `CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
`LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY(`Id`), PRIMARY KEY(`Id`),

View File

@ -41,6 +41,14 @@ class Achievement(TableABC):
def name(self, value: str): def name(self, value: str):
self._name = value self._name = value
@property
def attribute(self) -> str:
return self._attribute
@attribute.setter
def attribute(self, value: str):
self._attribute = value
@property @property
def operator(self) -> str: def operator(self) -> str:
return self._operator return self._operator

View File

@ -23,11 +23,11 @@ class AchievementRepositoryService(AchievementRepositoryABC):
def _from_result(self, result: tuple): def _from_result(self, result: tuple):
return Achievement( return Achievement(
result[1],
result[2],
result[3], result[3],
result[4], result[4],
result[5], self._servers.get_server_by_id(result[5]),
result[6],
self._servers.get_server_by_id(result[2]),
result[6], result[6],
result[7], result[7],
id=result[0], id=result[0],

View File

@ -3,6 +3,7 @@ from cpl_discord.service import DiscordBotServiceABC
from bot_data.abc.achievement_repository_abc import AchievementRepositoryABC from bot_data.abc.achievement_repository_abc import AchievementRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC 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_data.model.user_role_enum import UserRoleEnum
from bot_graphql.abc.query_abc import QueryABC from bot_graphql.abc.query_abc import QueryABC
from modules.permission.service.permission_service import PermissionService from modules.permission.service.permission_service import PermissionService
@ -25,7 +26,34 @@ class AchievementMutation(QueryABC):
self._db = db self._db = db
self._permissions = permissions self._permissions = permissions
self.set_field("createAchievement", self.resolve_create_achievement)
self.set_field("updateAchievement", self.resolve_update_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): def resolve_update_achievement(self, *_, input: dict):
achievement = self._achievements.get_achievement_by_id(input["id"]) 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"]) achievement = self._achievements.get_achievement_by_id(input["id"])
return achievement 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

View File

@ -50,6 +50,7 @@ class LevelMutation(QueryABC):
and l.color == level.color and l.color == level.color
and l.min_xp == level.min_xp and l.min_xp == level.min_xp
and l.permissions == level.permissions and l.permissions == level.permissions
and l.server.id == server.id
) )
self._bot.loop.create_task(self._level_seeder.seed()) self._bot.loop.create_task(self._level_seeder.seed())

View File

@ -11,9 +11,9 @@ class AchievementQuery(DataQueryWithHistoryABC):
): ):
DataQueryWithHistoryABC.__init__(self, "Achievement", "AchievementsHistory", UserHistory, db) DataQueryWithHistoryABC.__init__(self, "Achievement", "AchievementsHistory", UserHistory, db)
self.set_field("id", lambda x: x.id) self.set_field("id", lambda x, *_: x.id)
self.set_field("name", lambda x: x.name) self.set_field("name", lambda x, *_: x.name)
self.set_field("attribute", lambda x: x.attribute) self.set_field("attribute", lambda x, *_: x.attribute)
self.set_field("operator", lambda x: x.operator) self.set_field("operator", lambda x, *_: x.operator)
self.set_field("value", lambda x: x.value) self.set_field("value", lambda x, *_: x.value)
self.set_field("server", lambda x: x.server) self.set_field("server", lambda x, *_: x.server)