From 7d20d838341f96bd8ce2fe7e6854ef260f18635f Mon Sep 17 00:00:00 2001 From: andres Date: Tue, 12 Mar 2024 18:27:07 +0100 Subject: [PATCH] wip --- .../cards/infrastructure/cards.repository.ts | 50 +++++++++++++------ .../decks/infrastructure/decks.repository.ts | 7 +-- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/modules/cards/infrastructure/cards.repository.ts b/src/modules/cards/infrastructure/cards.repository.ts index eb6e6c9..8fe201d 100644 --- a/src/modules/cards/infrastructure/cards.repository.ts +++ b/src/modules/cards/infrastructure/cards.repository.ts @@ -61,9 +61,11 @@ export class CardsRepository { }, question: { contains: question || undefined, + mode: 'insensitive' as const, }, answer: { contains: answer || undefined, + mode: 'insensitive' as const, }, } @@ -72,26 +74,46 @@ export class CardsRepository { if (key === 'grade') { const start = (currentPage - 1) * itemsPerPage + // Initialize parts of the WHERE clause + const whereParts = [] + const queryParams: any[] = [userId, deckId] + + // Add conditions for question and answer if they are provided + if (question) { + whereParts.push(`c."question" ILIKE $${queryParams.length + 1}`) + queryParams.push(`%${question}%`) + } + + if (answer) { + whereParts.push(`c."answer" ILIKE $${queryParams.length + 1}`) + queryParams.push(`%${answer}%`) + } + + // If no specific conditions are provided, match everything + if (whereParts.length === 0) { + whereParts.push('TRUE') + } + + const whereClause = whereParts.join(' OR ') + const sqlQuery = ` - SELECT c.*, g.grade as userGrade - FROM card AS c - LEFT JOIN grade AS g ON c.id = g.cardId AND g.userId = ? - WHERE c.deckId = ? AND - (c.question LIKE ? OR c.answer LIKE ?) - ORDER BY g.grade ${direction} - LIMIT ?, ? - ` + SELECT c.*, g.grade as "userGrade" + FROM card AS c + LEFT JOIN grade AS g ON c.id = g."cardId" AND g."userId" = $1 + WHERE c."deckId" = $2 AND (${whereClause}) + ORDER BY g."grade" ${direction === 'asc' ? 'ASC NULLS FIRST' : 'DESC NULLS LAST'} + LIMIT $${queryParams.length + 1} OFFSET $${queryParams.length + 2} +` + + // Add itemsPerPage and start to the queryParams + queryParams.push(itemsPerPage, start) const cardsRaw = (await this.prisma.$queryRawUnsafe( sqlQuery, - userId, - deckId, - `%${question || ''}%`, - `%${answer || ''}%`, - start, - itemsPerPage + ...queryParams )) satisfies Array + console.log('123', sqlQuery, queryParams) const cards: CardWithGrades[] = cardsRaw.map(({ userGrade, ...card }) => ({ ...card, grades: [ diff --git a/src/modules/decks/infrastructure/decks.repository.ts b/src/modules/decks/infrastructure/decks.repository.ts index 56834e4..984d265 100644 --- a/src/modules/decks/infrastructure/decks.repository.ts +++ b/src/modules/decks/infrastructure/decks.repository.ts @@ -59,7 +59,9 @@ export class DecksRepository { async findMinMaxCards(): Promise<{ min: number; max: number }> { const result = await this.prisma - .$queryRaw`SELECT MAX(card_count) as maxCardsCount, MIN(card_count) as minCardsCount FROM (SELECT deck.id, COUNT(card.id) as card_count FROM deck LEFT JOIN card ON deck.id = card."deckId" GROUP BY deck.id) AS card_counts;` + .$queryRaw`SELECT MAX(card_count) as "maxCardsCount", MIN(card_count) as "minCardsCount" FROM (SELECT deck.id, COUNT(card.id) as card_count FROM deck LEFT JOIN card ON deck.id = card."deckId" GROUP BY deck.id) AS card_counts;` + + console.log(result) return { max: Number(result[0].maxCardsCount), @@ -111,7 +113,7 @@ export class DecksRepository { // Prepare the where clause conditions const conditions = [] - if (name) conditions.push(`d.name LIKE ('%' || ? || '%')`) + if (name) conditions.push(`d.name ILIKE ('%' || ? || '%')`) if (authorId) conditions.push(`d."userId" = ?`) if (userId) conditions.push(`(d."isPrivate" = FALSE OR (d."isPrivate" = TRUE AND d."userId" = ?))`) @@ -208,7 +210,6 @@ LIMIT $${conditions.length + havingConditions.length + 1} OFFSET $${ // Execute the raw SQL query for total count const totalResult = await this.prisma.$queryRawUnsafe(countQuery, ...countQueryParams) - const total = Number(totalResult[0]?.total) ?? 1 const modifiedDecks = decks.map(deck => { const cardsCount = deck.cardsCount