From 3228f0033b8f7670dc8addfa8e1b083820e36ea1 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 9 Mar 2025 11:51:39 +0100 Subject: [PATCH] deck handling fixed, new launch config for nest.js --- .vscode/launch.json | 10 +-------- api/src/drizzle.service.ts | 46 ++++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 6a3b225..1c02dad 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -2,13 +2,6 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ - { - "name": "ng serve", - "type": "chrome", - "request": "launch", - "preLaunchTask": "npm: start", - "url": "http://localhost:4200/" - }, { "type": "node", "request": "launch", @@ -28,9 +21,8 @@ "name": "Debug NestJS API", "type": "node", "request": "launch", - "runtimeExecutable": "npx", + "runtimeExecutable": "/home/aknuth/.nvm/versions/node/v22.14.0/bin/npx", "runtimeArgs": ["nest", "start", "--debug", "--watch"], - "port": 9229, "cwd": "${workspaceFolder}/api", "envFile": "${workspaceFolder}/api/.env", "console": "integratedTerminal", diff --git a/api/src/drizzle.service.ts b/api/src/drizzle.service.ts index d9b3349..98cdf78 100644 --- a/api/src/drizzle.service.ts +++ b/api/src/drizzle.service.ts @@ -57,8 +57,11 @@ export class DrizzleService { .where(and(eq(deck.user, user.email), sql`bildid IS NULL`)) .limit(maxDecks); + // Ergebnis-Array, das später zurückgegeben wird + let result: Array = []; + // Für jedes Deck werden nun die zugehörigen Bilder (Einträge mit bildid) abgerufen. - const imagesForDecks = await Promise.all( + await Promise.all( deckHeaders.map(async (header: any) => { const images: Array = await this.db .select() @@ -70,7 +73,12 @@ export class DrizzleService { sql`bildid IS NOT NULL`, ), ); - const minimizedImagesArray = (() => { + + if (images.length === 0) { + // Wenn keine Bilder vorhanden sind, füge den Header selbst hinzu + result.push(header); + } else { + // Wenn Bilder vorhanden sind, verarbeite sie wie zuvor const selectedBildnamen: Array = []; // Sammle die ersten maxImages verschiedenen Bildnamen for (const item of images) { @@ -82,16 +90,26 @@ export class DrizzleService { } } // Filtere das Array, um nur Einträge mit diesen Bildnamen zu behalten - return images.filter((item) => + const filteredImages = images.filter((item) => selectedBildnamen.includes(item.bildname), ); - })(); - return { minimizedImagesArray }; - //return { images }; + // Füge die gefilterten Bilder zum Ergebnis hinzu + result = result.concat(filteredImages); + } }), ); - return imagesForDecks.flatMap((item) => item.minimizedImagesArray); + // Sortiere das Ergebnis nach dem Einfügedatum (absteigend, neueste zuerst) + result.sort((a, b) => { + // Wenn inserted null oder undefined ist, setze es ans Ende + if (!a.inserted) return 1; + if (!b.inserted) return -1; + + // Vergleiche die Daten (neuere Einträge zuerst) + return new Date(a.inserted).getTime() - new Date(b.inserted).getTime(); + }); + + return result; } /** @@ -108,8 +126,7 @@ export class DrizzleService { const deckCountResult = await this.db .select({ count: sql`count(*) as count` }) .from(deck) - .where(and(eq(deck.user, user.email), sql`bildid IS NULL`)) - .all(); + .where(and(eq(deck.user, user.email), sql`bildid IS NULL`)); const deckCount = Number(deckCountResult[0].count); if (deckCount >= maxDecks) { throw new HttpException( @@ -235,8 +252,7 @@ export class DrizzleService { eq(deck.deckname, data.deckname), sql`bildid IS NOT NULL`, ), - ) - .all(); + ); const distinctImageCount = Number(distinctImagesResult[0].count); if (distinctImageCount >= maxImages) { throw new HttpException( @@ -389,8 +405,7 @@ export class DrizzleService { const existingImages = await this.db .select() .from(deck) - .where(and(eq(deck.bildid, bildId), eq(deck.user, user.email))) - .all(); + .where(and(eq(deck.bildid, bildId), eq(deck.user, user.email))); if (existingImages.length === 0) { throw new HttpException('Deck not found', HttpStatus.NOT_FOUND); } @@ -445,10 +460,7 @@ export class DrizzleService { */ async getDistinctBildIds(user: User): Promise { try { - const result = await this.db - .selectDistinct([deck.bildid]) - .from(deck) - .all(); + const result = await this.db.selectDistinct([deck.bildid]).from(deck); const usedIds = result .map((row: any) => row['0']) .filter((id: string | null) => id !== null) as string[];