@@ -522,28 +522,34 @@ func (dr *dataReader) getPracticeTestsByStudysetIDs(ctx context.Context, studyse
522522 type dbPracticeTest struct {
523523 ID * string `db:"id"`
524524 Timestamp * string `db:"timestamp"`
525- StudysetID * string `db:"studyset_id"`
526525 QuestionsCorrect * int32 `db:"questions_correct"`
527526 QuestionsTotal * int32 `db:"questions_total"`
528527 Questions []* model.Question `db:"questions"`
528+ StudysetID * string `db:"studyset_id"`
529529 }
530530 var dbPracticeTests []* dbPracticeTest
531531
532532 err := pgxscan .Select (
533533 ctx ,
534534 dr .db ,
535535 & dbPracticeTests ,
536- `SELECT pt.id,
536+ `SELECT DISTINCT ON (input.og_order, pt.id)
537+ pt.id,
537538 to_char(pt.timestamp, 'YYYY-MM-DD"T"HH24:MI:SS.MSTZH:TZM') as timestamp,
538- pt.studyset_id,
539539 pt.questions_correct,
540540 pt.questions_total,
541- pt.questions
541+ pt.questions,
542+ input.studyset_id
542543FROM unnest($1::uuid[]) WITH ORDINALITY AS input(studyset_id, og_order)
543- LEFT JOIN practice_tests pt
544- ON pt.studyset_id = input.studyset_id
545- AND pt.user_id = $2
546- ORDER BY input.og_order ASC, pt.timestamp DESC` ,
544+ JOIN (
545+ SELECT practice_test_id, term_id FROM practice_test_question_terms
546+ UNION
547+ SELECT practice_test_id, term_id FROM practice_test_distractor_terms
548+ ) mapping ON TRUE
549+ JOIN terms t ON mapping.term_id = t.id AND t.studyset_id = input.studyset_id
550+ JOIN practice_tests pt ON pt.id = mapping.practice_test_id
551+ WHERE pt.user_id = $2
552+ ORDER BY input.og_order ASC, pt.id, pt.timestamp DESC` ,
547553 studysetIDs ,
548554 authedUser .ID ,
549555 )
@@ -556,7 +562,6 @@ ORDER BY input.og_order ASC, pt.timestamp DESC`,
556562 if pt .ID != nil && pt .StudysetID != nil {
557563 grouped [* pt .StudysetID ] = append (grouped [* pt .StudysetID ], & model.PracticeTest {
558564 ID : pt .ID ,
559- StudysetID : pt .StudysetID ,
560565 Timestamp : pt .Timestamp ,
561566 QuestionsCorrect : pt .QuestionsCorrect ,
562567 QuestionsTotal : pt .QuestionsTotal ,
@@ -659,7 +664,6 @@ func (dr *dataReader) getPracticeTestsByTermIDs(ctx context.Context, termIDs []s
659664 type dbPracticeTest struct {
660665 ID * string `db:"id"`
661666 Timestamp * string `db:"timestamp"`
662- StudysetID * string `db:"studyset_id"`
663667 QuestionsCorrect * int32 `db:"questions_correct"`
664668 QuestionsTotal * int32 `db:"questions_total"`
665669 Questions []* model.Question `db:"questions"`
@@ -673,7 +677,6 @@ func (dr *dataReader) getPracticeTestsByTermIDs(ctx context.Context, termIDs []s
673677 & dbPracticeTests ,
674678 `SELECT pt.id,
675679 to_char(pt.timestamp, 'YYYY-MM-DD"T"HH24:MI:SS.MSTZH:TZM') as timestamp,
676- pt.studyset_id,
677680 pt.questions_correct,
678681 pt.questions_total,
679682 pt.questions,
@@ -708,7 +711,6 @@ ORDER BY input.og_order ASC, pt.timestamp DESC`,
708711 if ! exists {
709712 grouped [* pt .TermID ] = append (grouped [* pt .TermID ], & model.PracticeTest {
710713 ID : pt .ID ,
711- StudysetID : pt .StudysetID ,
712714 Timestamp : pt .Timestamp ,
713715 QuestionsCorrect : pt .QuestionsCorrect ,
714716 QuestionsTotal : pt .QuestionsTotal ,
0 commit comments