From 8aa96482c12fe7088a24cfbef1460c47aba3b22b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 12 Jul 2023 13:11:26 +0200 Subject: [PATCH] Fixed server cache & improved frontend implementation #268_achievements --- kdb-bot/src/bot_data/service/cache_service.py | 19 ++++++++++++++----- .../service/server_repository_service.py | 5 +++-- .../src/app/models/graphql/mutations.model.ts | 12 ++++++------ .../achievement/achievement.component.html | 12 ++++++------ kdb-web/src/assets/i18n/de.json | 3 ++- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/kdb-bot/src/bot_data/service/cache_service.py b/kdb-bot/src/bot_data/service/cache_service.py index 31dcd73a..50d05e7e 100644 --- a/kdb-bot/src/bot_data/service/cache_service.py +++ b/kdb-bot/src/bot_data/service/cache_service.py @@ -5,12 +5,21 @@ from bot_data.model.server import Server class CacheService: def __init__(self): - self._cached_server = List(Server) + self._cached_servers = List(Server) @property def cached_server(self) -> List[Server]: - return self._cached_server + return self._cached_servers - @cached_server.setter - def cached_server(self, value: List[Server]): - self._cached_server = value + def add_server(self, server: Server): + if self._cached_servers.where(lambda x: x.id == server.id).count() > 0: + return + self._cached_servers.add(server) + + def add_servers(self, servers: List[Server]): + new_ids = servers.select(lambda x: x.id) + for s in self._cached_servers: + if s.id in new_ids: + return + + self._cached_servers.extend(servers) diff --git a/kdb-bot/src/bot_data/service/server_repository_service.py b/kdb-bot/src/bot_data/service/server_repository_service.py index 2d158fad..924cc036 100644 --- a/kdb-bot/src/bot_data/service/server_repository_service.py +++ b/kdb-bot/src/bot_data/service/server_repository_service.py @@ -26,7 +26,7 @@ class ServerRepositoryService(ServerRepositoryABC): for result in results: servers.append(Server(result[1], result[2], result[3], id=result[0])) - self._cache.cached_server = List(Server, servers) + self._cache.add_servers(servers) return servers def get_filtered_servers(self, criteria: ServerSelectCriteria) -> FilteredResult: @@ -56,13 +56,14 @@ class ServerRepositoryService(ServerRepositoryABC): def get_server_by_id(self, server_id: int) -> Server: cs = self._cache.cached_server.where(lambda x: x.id == server_id).single_or_default() + if cs is not None: return cs self._logger.trace(__name__, f"Send SQL command: {Server.get_select_by_id_string(server_id)}") result = self._context.select(Server.get_select_by_id_string(server_id))[0] server = Server(result[1], result[2], result[3], id=result[0]) - self._cache.cached_server.add(server) + self._cache.add_server(server) return server def get_server_by_discord_id(self, discord_id: int) -> Server: diff --git a/kdb-web/src/app/models/graphql/mutations.model.ts b/kdb-web/src/app/models/graphql/mutations.model.ts index db95bd86..df3b539d 100644 --- a/kdb-web/src/app/models/graphql/mutations.model.ts +++ b/kdb-web/src/app/models/graphql/mutations.model.ts @@ -124,7 +124,7 @@ export class Mutations { static createAchievement = ` mutation createAchievement($name: String, $attribute: String, $operator: String, $value: String, $serverId: ID) { - level { + achievement { createAchievement(input: { name: $name, attribute: $attribute, operator: $operator, value: $value, serverId: $serverId}) { id name @@ -140,9 +140,9 @@ export class Mutations { `; static updateAchievement = ` - mutation updateAchievement($name: String, $attribute: String, $operator: String, $value: String, $serverId: ID) { - level { - updateAchievement(input: { name: $name, attribute: $attribute, operator: $operator, value: $value}) { + mutation updateAchievement($id: ID, $name: String, $attribute: String, $operator: String, $value: String) { + achievement { + updateAchievement(input: { id: $id, name: $name, attribute: $attribute, operator: $operator, value: $value}) { id name attribute @@ -155,8 +155,8 @@ export class Mutations { static deleteAchievement = ` mutation deleteAchievement($id: ID) { - level { - deleteLevel(id: $id) { + achievement { + deleteAchievement(id: $id) { id name } diff --git a/kdb-web/src/app/modules/view/server/achievements/components/achievement/achievement.component.html b/kdb-web/src/app/modules/view/server/achievements/components/achievement/achievement.component.html index 83879d48..7bdfc233 100644 --- a/kdb-web/src/app/modules/view/server/achievements/components/achievement/achievement.component.html +++ b/kdb-web/src/app/modules/view/server/achievements/components/achievement/achievement.component.html @@ -18,7 +18,7 @@
+ (click)="onRowEditInit(dt, achievement, ri)" [disabled]="!user || !user.isModerator && !user.isAdmin"> + (click)="deleteAchievement(achievement)" [disabled]="!user || !user.isModerator && !user.isAdmin"> + icon="pi pi-check-circle" (click)="onRowEditSave(dt, achievement, ri)" [disabled]="!user || !user.isModerator && !user.isAdmin"> + icon="pi pi-times-circle" (click)="onRowEditCancel(ri)" [disabled]="!user || !user.isModerator && !user.isAdmin">
diff --git a/kdb-web/src/assets/i18n/de.json b/kdb-web/src/assets/i18n/de.json index 4078a0be..70139bd4 100644 --- a/kdb-web/src/assets/i18n/de.json +++ b/kdb-web/src/assets/i18n/de.json @@ -280,7 +280,8 @@ "dashboard": "Dashboard", "levels": "Level", "members": "Mitglieder", - "profile": "Dein Profil" + "profile": "Dein Profil", + "achievements": "Errungenschaften" }, "server_empty": "Kein Server ausgewählt", "settings": "Einstellungen",