diff --git a/src/exam.rs b/src/exam.rs index c92e734..f4acae1 100644 --- a/src/exam.rs +++ b/src/exam.rs @@ -94,19 +94,9 @@ pub async fn build_paper(pool: &SqlitePool, uid: &str, f: &BuildForm) -> anyhow: }; let mut ids: Vec = Vec::new(); + // Chapters × subtopics: one indexed lookup per pair (sqlx needs a static + // string per branch, hence the unseen/not split inline). for ch in &chapters { - // Four branches: unseen×subtopic filter vs not. - // sqlx needs a static query string per branch. - let rows: Vec = if !subtopics.is_empty() && unseen { - sqlx::query( - "SELECT q.id FROM questions q WHERE q.chapter_id=? AND q.subtopic_id=? AND q.id NOT IN (SELECT question_id FROM seen_questions WHERE user_id=?)" - ) - // For each chapter, collect questions from matching subtopics. - .bind(ch).bind(0i64).bind(uid) // placeholder; handled per-subtopic below - .fetch_all(pool).await.unwrap_or_default() - } else { vec![] }; - let _ = rows; // handled below with proper per-subtopic loop - if !subtopics.is_empty() { for st in &subtopics { let rows: Vec = if unseen { diff --git a/src/main.rs b/src/main.rs index 08ac2a9..2f88571 100644 --- a/src/main.rs +++ b/src/main.rs @@ -254,7 +254,7 @@ async fn sign_in_with_google(ctx: &CtxArc, claims: &google::Claims, next: Option async fn load_chapter_blocks(pool: &sqlx::SqlitePool) -> Vec { use sqlx::Row; let rows = sqlx::query( - "SELECT c.id, c.subject, c.name, s.id AS sub_id, s.name AS sub, (SELECT COUNT(*) FROM questions q WHERE q.chapter_id=c.id) AS n FROM chapters c JOIN subtopics s ON s.chapter_id=c.id ORDER BY c.subject, c.name, s.name", + "SELECT c.id, c.subject, c.name, s.id AS sub_id, s.name AS sub, COALESCE(qc.n,0) AS n FROM chapters c JOIN subtopics s ON s.chapter_id=c.id LEFT JOIN (SELECT chapter_id, COUNT(*) AS n FROM questions GROUP BY chapter_id) qc ON qc.chapter_id=c.id ORDER BY c.subject, c.name, s.name", ) .fetch_all(pool) .await