Fixed server cache & improved frontend implementation #268_achievements

This commit is contained in:
Sven Heidemann 2023-07-12 13:11:26 +02:00
parent a0d38bec49
commit b99dd1bded
5 changed files with 31 additions and 20 deletions

View File

@ -5,12 +5,21 @@ from bot_data.model.server import Server
class CacheService: class CacheService:
def __init__(self): def __init__(self):
self._cached_server = List(Server) self._cached_servers = List(Server)
@property @property
def cached_server(self) -> List[Server]: def cached_server(self) -> List[Server]:
return self._cached_server return self._cached_servers
@cached_server.setter def add_server(self, server: Server):
def cached_server(self, value: List[Server]): if self._cached_servers.where(lambda x: x.id == server.id).count() > 0:
self._cached_server = value 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)

View File

@ -26,7 +26,7 @@ class ServerRepositoryService(ServerRepositoryABC):
for result in results: for result in results:
servers.append(Server(result[1], result[2], result[3], id=result[0])) 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 return servers
def get_filtered_servers(self, criteria: ServerSelectCriteria) -> FilteredResult: 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: 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() cs = self._cache.cached_server.where(lambda x: x.id == server_id).single_or_default()
if cs is not None: if cs is not None:
return cs return cs
self._logger.trace(__name__, f"Send SQL command: {Server.get_select_by_id_string(server_id)}") 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] result = self._context.select(Server.get_select_by_id_string(server_id))[0]
server = Server(result[1], result[2], result[3], id=result[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 return server
def get_server_by_discord_id(self, discord_id: int) -> Server: def get_server_by_discord_id(self, discord_id: int) -> Server:

View File

@ -124,7 +124,7 @@ export class Mutations {
static createAchievement = ` static createAchievement = `
mutation createAchievement($name: String, $attribute: String, $operator: String, $value: String, $serverId: ID) { 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}) { createAchievement(input: { name: $name, attribute: $attribute, operator: $operator, value: $value, serverId: $serverId}) {
id id
name name
@ -140,9 +140,9 @@ export class Mutations {
`; `;
static updateAchievement = ` static updateAchievement = `
mutation updateAchievement($name: String, $attribute: String, $operator: String, $value: String, $serverId: ID) { mutation updateAchievement($id: ID, $name: String, $attribute: String, $operator: String, $value: String) {
level { achievement {
updateAchievement(input: { name: $name, attribute: $attribute, operator: $operator, value: $value}) { updateAchievement(input: { id: $id, name: $name, attribute: $attribute, operator: $operator, value: $value}) {
id id
name name
attribute attribute
@ -155,8 +155,8 @@ export class Mutations {
static deleteAchievement = ` static deleteAchievement = `
mutation deleteAchievement($id: ID) { mutation deleteAchievement($id: ID) {
level { achievement {
deleteLevel(id: $id) { deleteAchievement(id: $id) {
id id
name name
} }

View File

@ -18,7 +18,7 @@
<div class="table-caption-btn-wrapper btn-wrapper"> <div class="table-caption-btn-wrapper btn-wrapper">
<button pButton label="{{'common.add' | translate}}" class="icon-btn btn" <button pButton label="{{'common.add' | translate}}" class="icon-btn btn"
icon="pi pi-user-plus" (click)="addAchievement(dt)" [disabled]="isEditingNew || user?.isModerator && !user?.isAdmin"> icon="pi pi-user-plus" (click)="addAchievement(dt)" [disabled]="isEditingNew || !user?.isModerator && !user?.isAdmin">
</button> </button>
<button pButton label="{{'common.reset_filters' | translate}}" icon="pi pi-undo" <button pButton label="{{'common.reset_filters' | translate}}" icon="pi pi-undo"
class="icon-btn btn" (click)="resetFilters()"> class="icon-btn btn" (click)="resetFilters()">
@ -143,7 +143,7 @@
<td> <td>
<p-cellEditor> <p-cellEditor>
<ng-template pTemplate="input"> <ng-template pTemplate="input">
<input class="table-edit-input" pInputText min="0" type="number" [(ngModel)]="achievement.operator"> <input class="table-edit-input" pInputText type="text" [(ngModel)]="achievement.operator">
</ng-template> </ng-template>
<ng-template pTemplate="output"> <ng-template pTemplate="output">
{{achievement.operator}} {{achievement.operator}}
@ -186,14 +186,14 @@
<div class="btn-wrapper"> <div class="btn-wrapper">
<app-history-btn *ngIf="!isEditingNew" [id]="achievement.id" [query]="query" translationKey="view.server.achievements.header"></app-history-btn> <app-history-btn *ngIf="!isEditingNew" [id]="achievement.id" [query]="query" translationKey="view.server.achievements.header"></app-history-btn>
<button *ngIf="!editing" pButton pInitEditableRow class="btn icon-btn" icon="pi pi-pencil" <button *ngIf="!editing" pButton pInitEditableRow class="btn icon-btn" icon="pi pi-pencil"
(click)="onRowEditInit(dt, achievement, ri)" [disabled]="!user || user.isModerator && !user.isAdmin"></button> (click)="onRowEditInit(dt, achievement, ri)" [disabled]="!user || !user.isModerator && !user.isAdmin"></button>
<button *ngIf="!editing" pButton class="btn icon-btn danger-icon-btn" icon="pi pi-trash" <button *ngIf="!editing" pButton class="btn icon-btn danger-icon-btn" icon="pi pi-trash"
(click)="deleteAchievement(achievement)" [disabled]="!user || user.isModerator && !user.isAdmin"></button> (click)="deleteAchievement(achievement)" [disabled]="!user || !user.isModerator && !user.isAdmin"></button>
<button *ngIf="editing" pButton pSaveEditableRow class="btn icon-btn" <button *ngIf="editing" pButton pSaveEditableRow class="btn icon-btn"
icon="pi pi-check-circle" (click)="onRowEditSave(dt, achievement, ri)" [disabled]="!user || user.isModerator && !user.isAdmin"></button> icon="pi pi-check-circle" (click)="onRowEditSave(dt, achievement, ri)" [disabled]="!user || !user.isModerator && !user.isAdmin"></button>
<button *ngIf="editing" pButton pCancelEditableRow class="btn icon-btn danger-icon-btn" <button *ngIf="editing" pButton pCancelEditableRow class="btn icon-btn danger-icon-btn"
icon="pi pi-times-circle" (click)="onRowEditCancel(ri)" [disabled]="!user || user.isModerator && !user.isAdmin"></button> icon="pi pi-times-circle" (click)="onRowEditCancel(ri)" [disabled]="!user || !user.isModerator && !user.isAdmin"></button>
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -280,7 +280,8 @@
"dashboard": "Dashboard", "dashboard": "Dashboard",
"levels": "Level", "levels": "Level",
"members": "Mitglieder", "members": "Mitglieder",
"profile": "Dein Profil" "profile": "Dein Profil",
"achievements": "Errungenschaften"
}, },
"server_empty": "Kein Server ausgewählt", "server_empty": "Kein Server ausgewählt",
"settings": "Einstellungen", "settings": "Einstellungen",