deck handling fixed, new launch config for nest.js

This commit is contained in:
Your Name 2025-03-09 11:51:39 +01:00
parent a718c87c63
commit 3228f0033b
2 changed files with 30 additions and 26 deletions

10
.vscode/launch.json vendored
View File

@ -2,13 +2,6 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{
"name": "ng serve",
"type": "chrome",
"request": "launch",
"preLaunchTask": "npm: start",
"url": "http://localhost:4200/"
},
{ {
"type": "node", "type": "node",
"request": "launch", "request": "launch",
@ -28,9 +21,8 @@
"name": "Debug NestJS API", "name": "Debug NestJS API",
"type": "node", "type": "node",
"request": "launch", "request": "launch",
"runtimeExecutable": "npx", "runtimeExecutable": "/home/aknuth/.nvm/versions/node/v22.14.0/bin/npx",
"runtimeArgs": ["nest", "start", "--debug", "--watch"], "runtimeArgs": ["nest", "start", "--debug", "--watch"],
"port": 9229,
"cwd": "${workspaceFolder}/api", "cwd": "${workspaceFolder}/api",
"envFile": "${workspaceFolder}/api/.env", "envFile": "${workspaceFolder}/api/.env",
"console": "integratedTerminal", "console": "integratedTerminal",

View File

@ -57,8 +57,11 @@ export class DrizzleService {
.where(and(eq(deck.user, user.email), sql`bildid IS NULL`)) .where(and(eq(deck.user, user.email), sql`bildid IS NULL`))
.limit(maxDecks); .limit(maxDecks);
// Ergebnis-Array, das später zurückgegeben wird
let result: Array<any> = [];
// Für jedes Deck werden nun die zugehörigen Bilder (Einträge mit bildid) abgerufen. // 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) => { deckHeaders.map(async (header: any) => {
const images: Array<any> = await this.db const images: Array<any> = await this.db
.select() .select()
@ -70,7 +73,12 @@ export class DrizzleService {
sql`bildid IS NOT NULL`, 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<any> = []; const selectedBildnamen: Array<any> = [];
// Sammle die ersten maxImages verschiedenen Bildnamen // Sammle die ersten maxImages verschiedenen Bildnamen
for (const item of images) { for (const item of images) {
@ -82,16 +90,26 @@ export class DrizzleService {
} }
} }
// Filtere das Array, um nur Einträge mit diesen Bildnamen zu behalten // 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), selectedBildnamen.includes(item.bildname),
); );
})(); // Füge die gefilterten Bilder zum Ergebnis hinzu
return { minimizedImagesArray }; result = result.concat(filteredImages);
//return { images }; }
}), }),
); );
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 const deckCountResult = await this.db
.select({ count: sql`count(*) as count` }) .select({ count: sql`count(*) as count` })
.from(deck) .from(deck)
.where(and(eq(deck.user, user.email), sql`bildid IS NULL`)) .where(and(eq(deck.user, user.email), sql`bildid IS NULL`));
.all();
const deckCount = Number(deckCountResult[0].count); const deckCount = Number(deckCountResult[0].count);
if (deckCount >= maxDecks) { if (deckCount >= maxDecks) {
throw new HttpException( throw new HttpException(
@ -235,8 +252,7 @@ export class DrizzleService {
eq(deck.deckname, data.deckname), eq(deck.deckname, data.deckname),
sql`bildid IS NOT NULL`, sql`bildid IS NOT NULL`,
), ),
) );
.all();
const distinctImageCount = Number(distinctImagesResult[0].count); const distinctImageCount = Number(distinctImagesResult[0].count);
if (distinctImageCount >= maxImages) { if (distinctImageCount >= maxImages) {
throw new HttpException( throw new HttpException(
@ -389,8 +405,7 @@ export class DrizzleService {
const existingImages = await this.db const existingImages = await this.db
.select() .select()
.from(deck) .from(deck)
.where(and(eq(deck.bildid, bildId), eq(deck.user, user.email))) .where(and(eq(deck.bildid, bildId), eq(deck.user, user.email)));
.all();
if (existingImages.length === 0) { if (existingImages.length === 0) {
throw new HttpException('Deck not found', HttpStatus.NOT_FOUND); throw new HttpException('Deck not found', HttpStatus.NOT_FOUND);
} }
@ -445,10 +460,7 @@ export class DrizzleService {
*/ */
async getDistinctBildIds(user: User): Promise<string[]> { async getDistinctBildIds(user: User): Promise<string[]> {
try { try {
const result = await this.db const result = await this.db.selectDistinct([deck.bildid]).from(deck);
.selectDistinct([deck.bildid])
.from(deck)
.all();
const usedIds = result const usedIds = result
.map((row: any) => row['0']) .map((row: any) => row['0'])
.filter((id: string | null) => id !== null) as string[]; .filter((id: string | null) => id !== null) as string[];