From 03a3d3f2d65d894e9f004ee8ae2d56f6b78ca1b4 Mon Sep 17 00:00:00 2001 From: Sindre Kjelsrud Date: Wed, 17 Jan 2024 18:52:10 +0100 Subject: [PATCH] :bug: fix cohort-bug when all questions are answered at least 3 times, it didn't fill the last cohort with random questions Co-authored-by: haraldnilsen Signed-off-by: Sindre Kjelsrud --- backend/cmd/db/get_user_questions.go | 47 ++++++++++++++-------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/backend/cmd/db/get_user_questions.go b/backend/cmd/db/get_user_questions.go index 5bba8c7..c6c4a8d 100644 --- a/backend/cmd/db/get_user_questions.go +++ b/backend/cmd/db/get_user_questions.go @@ -45,29 +45,30 @@ func GetUserQuestions(respondentID int) ([]UserQuestions, error) { } getQuestionsStatement := ` - WITH FilteredQuestions AS ( - SELECT s.SpørsmålID, s.tekst - FROM Spørsmål s - LEFT JOIN ( - SELECT ss.spørsmålID - FROM Spørsmålsvar ss - LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID - GROUP BY ss.spørsmålID - HAVING COUNT(sv.vurderingID) < 3 - ) AS subquery ON s.spørsmålID = subquery.spørsmålID - WHERE subquery.spørsmålID IS NOT NULL - LIMIT 5 - ) - - SELECT * FROM FilteredQuestions - UNION ALL - SELECT * FROM ( - SELECT s.SpørsmålID, s.tekst - FROM Spørsmål s - ORDER BY RANDOM() - LIMIT 5 - ) RandomQuestions - WHERE NOT EXISTS (SELECT 1 FROM FilteredQuestions) + WITH FilteredQuestions AS ( + SELECT s.SpørsmålID, s.tekst + FROM Spørsmål s + LEFT JOIN ( + SELECT ss.spørsmålID + FROM Spørsmålsvar ss + LEFT JOIN SvarVurdering sv ON ss.svarID = sv.svarID + GROUP BY ss.spørsmålID + HAVING COUNT(sv.vurderingID) < 3 + ) AS subquery ON s.spørsmålID = subquery.spørsmålID + WHERE subquery.spørsmålID IS NOT NULL + LIMIT 5 + ), + RandomQuestions AS ( + SELECT s.SpørsmålID, s.tekst + FROM Spørsmål s + WHERE s.SpørsmålID NOT IN (SELECT SpørsmålID FROM FilteredQuestions) + ORDER BY RANDOM() + LIMIT 5 + ) + SELECT * FROM FilteredQuestions + UNION ALL + SELECT * FROM RandomQuestions + LIMIT 5 ` stmt, err := db.Prepare(getQuestionsStatement)