Skip to content

Commit 1281b60

Browse files
committed
fix: show total number of questions in question container
1 parent 45c7123 commit 1281b60

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

plugins/qeta-backend/src/database/DatabaseQetaStore.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,16 @@ export class DatabaseQetaStore implements QetaStore {
295295
}
296296

297297
if (options.noAnswers) {
298-
query.orderBy('answersCount', 'asc');
298+
query.whereNull('answers.questionId');
299299
}
300300
if (options.noCorrectAnswer) {
301-
query.orderBy('correctAnswers', 'asc');
301+
query
302+
.where('answers.correct', '!=', true)
303+
.orWhereNull('answers.questionId');
302304
}
303305

306+
const totalQuery = query.clone();
307+
304308
if (options.random) {
305309
query.orderByRaw('RANDOM()');
306310
} else if (options.orderBy) {
@@ -317,14 +321,14 @@ export class DatabaseQetaStore implements QetaStore {
317321
query.offset(options.offset);
318322
}
319323

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);
326330

327-
const ret = {
331+
return {
328332
questions: await Promise.all(
329333
rows.map(async val => {
330334
return this.mapQuestion(
@@ -337,17 +341,6 @@ export class DatabaseQetaStore implements QetaStore {
337341
),
338342
total,
339343
};
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;
351344
}
352345

353346
async getQuestion(

plugins/qeta/src/components/QuestionsContainer/QuestionsContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const QuestionsContainer = (props: QuestionsContainerProps) => {
5454
<Grid container justifyContent="space-between">
5555
<Grid item>
5656
<Typography variant="h6">{`${
57-
response?.questions.length ?? 0
57+
response?.total ?? 0
5858
} questions`}</Typography>
5959
</Grid>
6060
{(showFilters ?? true) && (

0 commit comments

Comments
 (0)