mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-18 12:33:21 +00:00
fix: total count query
This commit is contained in:
@@ -68,7 +68,7 @@ export class DecksRepository {
|
|||||||
orderBy,
|
orderBy,
|
||||||
}: GetAllDecksDto): Promise<PaginatedDecks> {
|
}: GetAllDecksDto): Promise<PaginatedDecks> {
|
||||||
if (!orderBy || orderBy === 'null') {
|
if (!orderBy || orderBy === 'null') {
|
||||||
orderBy = DecksOrderBy['updated-desc'] // Adjust this based on your actual ordering requirements
|
orderBy = DecksOrderBy['updated-desc']
|
||||||
}
|
}
|
||||||
let orderField = 'd.updated' // default order field
|
let orderField = 'd.updated' // default order field
|
||||||
let orderDirection = 'DESC' // default order direction
|
let orderDirection = 'DESC' // default order direction
|
||||||
@@ -150,11 +150,15 @@ export class DecksRepository {
|
|||||||
>(query, ...deckQueryParams)
|
>(query, ...deckQueryParams)
|
||||||
// Construct the raw SQL query for total count
|
// Construct the raw SQL query for total count
|
||||||
const countQuery = `
|
const countQuery = `
|
||||||
SELECT COUNT(DISTINCT d.id) AS total
|
SELECT COUNT(*) AS total
|
||||||
|
FROM (
|
||||||
|
SELECT d.id
|
||||||
FROM deck AS d
|
FROM deck AS d
|
||||||
LEFT JOIN card AS c ON d.id = c.deckId
|
LEFT JOIN card AS c ON d.id = c.deckId
|
||||||
${conditions.length ? `WHERE ${conditions.join(' AND ')}` : ''}
|
${conditions.length ? `WHERE ${conditions.join(' AND ')}` : ''}
|
||||||
${havingConditions.length ? `GROUP BY d.id HAVING ${havingConditions.join(' AND ')}` : ''};
|
GROUP BY d.id
|
||||||
|
${havingConditions.length ? `HAVING ${havingConditions.join(' AND ')}` : ''}
|
||||||
|
) AS subquery;
|
||||||
`
|
`
|
||||||
|
|
||||||
// Parameters for total count query
|
// Parameters for total count query
|
||||||
@@ -169,7 +173,7 @@ export class DecksRepository {
|
|||||||
// Execute the raw SQL query for total count
|
// Execute the raw SQL query for total count
|
||||||
const totalResult = await this.prisma.$queryRawUnsafe<any[]>(countQuery, ...countQueryParams)
|
const totalResult = await this.prisma.$queryRawUnsafe<any[]>(countQuery, ...countQueryParams)
|
||||||
|
|
||||||
const total = totalResult.length
|
const total = Number(totalResult[0]?.total) ?? 1
|
||||||
const modifiedDecks = decks.map(deck => {
|
const modifiedDecks = decks.map(deck => {
|
||||||
const cardsCount = deck.cardsCount
|
const cardsCount = deck.cardsCount
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user