@@ -295,12 +295,16 @@ export class DatabaseQetaStore implements QetaStore {
295
295
}
296
296
297
297
if ( options . noAnswers ) {
298
- query . orderBy ( 'answersCount' , 'asc ') ;
298
+ query . whereNull ( 'answers.questionId ') ;
299
299
}
300
300
if ( options . noCorrectAnswer ) {
301
- query . orderBy ( 'correctAnswers' , 'asc' ) ;
301
+ query
302
+ . where ( 'answers.correct' , '!=' , true )
303
+ . orWhereNull ( 'answers.questionId' ) ;
302
304
}
303
305
306
+ const totalQuery = query . clone ( ) ;
307
+
304
308
if ( options . random ) {
305
309
query . orderByRaw ( 'RANDOM()' ) ;
306
310
} else if ( options . orderBy ) {
@@ -317,14 +321,14 @@ export class DatabaseQetaStore implements QetaStore {
317
321
query . offset ( options . offset ) ;
318
322
}
319
323
320
- const rows = ( await query ) as RawQuestionEntity [ ] ;
321
- const total = (
322
- ( await this . db < RawQuestionEntity > ( 'questions' )
323
- . count ( 'id as CNT' )
324
- . first ( ) ) as any
325
- ) ?. CNT ;
324
+ const results = await Promise . all ( [
325
+ query ,
326
+ this . db ( totalQuery . as ( 'totalQuery' ) ) . count ( '* as CNT' ) . first ( ) ,
327
+ ] ) ;
328
+ const rows = results [ 0 ] as RawQuestionEntity [ ] ;
329
+ const total = this . mapToInteger ( ( results [ 1 ] as any ) ?. CNT ) ;
326
330
327
- const ret = {
331
+ return {
328
332
questions : await Promise . all (
329
333
rows . map ( async val => {
330
334
return this . mapQuestion (
@@ -337,17 +341,6 @@ export class DatabaseQetaStore implements QetaStore {
337
341
) ,
338
342
total,
339
343
} ;
340
-
341
- // Does not work in postgresql for some reason to have where clause
342
- // using count aliases
343
- if ( options . noAnswers ) {
344
- ret . questions = ret . questions . filter ( q => q . answersCount === 0 ) ;
345
- }
346
- if ( options . noCorrectAnswer ) {
347
- ret . questions = ret . questions . filter ( q => ! q . correctAnswer ) ;
348
- }
349
-
350
- return ret ;
351
344
}
352
345
353
346
async getQuestion (
0 commit comments