update
This commit is contained in:
@@ -47,6 +47,28 @@ CREATE TABLE IF NOT EXISTS elements (
|
||||
)
|
||||
"""
|
||||
|
||||
CREATE_VERTIEFUNGEN = """
|
||||
CREATE TABLE IF NOT EXISTS vertiefungen (
|
||||
topic TEXT NOT NULL,
|
||||
baustein TEXT NOT NULL,
|
||||
md TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
PRIMARY KEY (topic, baustein)
|
||||
)
|
||||
"""
|
||||
|
||||
CREATE_BAUSTEIN_PROGRESS = """
|
||||
CREATE TABLE IF NOT EXISTS baustein_progress (
|
||||
topic TEXT NOT NULL,
|
||||
baustein TEXT NOT NULL,
|
||||
gute_antworten INTEGER NOT NULL DEFAULT 0,
|
||||
absolviert TEXT,
|
||||
updated_at TEXT NOT NULL,
|
||||
PRIMARY KEY (topic, baustein)
|
||||
)
|
||||
"""
|
||||
|
||||
_db: aiosqlite.Connection | None = None
|
||||
|
||||
|
||||
@@ -67,6 +89,8 @@ async def init_db():
|
||||
await db.execute(CREATE_PROGRESS)
|
||||
await db.execute(CREATE_TOPICS)
|
||||
await db.execute(CREATE_ELEMENTS)
|
||||
await db.execute(CREATE_VERTIEFUNGEN)
|
||||
await db.execute(CREATE_BAUSTEIN_PROGRESS)
|
||||
try: # Migration für Bestands-DBs ohne step-Spalte
|
||||
await db.execute("ALTER TABLE guides ADD COLUMN step INTEGER")
|
||||
except aiosqlite.OperationalError:
|
||||
@@ -255,3 +279,104 @@ async def delete_progress(guide_id: str) -> None:
|
||||
db = await get_db()
|
||||
await db.execute("DELETE FROM guide_progress WHERE guide_id = ?", (guide_id,))
|
||||
await db.commit()
|
||||
|
||||
|
||||
# --- Baustein-Lernen: Vertiefungen + Prüfungs-Fortschritt ---
|
||||
|
||||
def _now() -> str:
|
||||
from datetime import datetime, timezone
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
|
||||
async def get_vertiefung(topic: str, baustein: str) -> str | None:
|
||||
db = await get_db()
|
||||
cursor = await db.execute(
|
||||
"SELECT md FROM vertiefungen WHERE topic = ? AND baustein = ?", (topic, baustein)
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return row[0] if row else None
|
||||
|
||||
|
||||
async def set_vertiefung(topic: str, baustein: str, md: str) -> None:
|
||||
db = await get_db()
|
||||
now = _now()
|
||||
await db.execute(
|
||||
"""INSERT INTO vertiefungen (topic, baustein, md, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(topic, baustein) DO UPDATE SET md = excluded.md, updated_at = excluded.updated_at""",
|
||||
(topic, baustein, md, now, now),
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def list_vertiefungen(topic: str) -> set[str]:
|
||||
"""Baustein-Titel, zu denen eine Vertiefung existiert."""
|
||||
db = await get_db()
|
||||
cursor = await db.execute("SELECT baustein FROM vertiefungen WHERE topic = ?", (topic,))
|
||||
rows = await cursor.fetchall()
|
||||
return {row[0] for row in rows}
|
||||
|
||||
|
||||
async def list_baustein_progress(topic: str) -> list[dict]:
|
||||
db = await get_db()
|
||||
cursor = await db.execute(
|
||||
"SELECT baustein, gute_antworten, absolviert FROM baustein_progress WHERE topic = ?", (topic,)
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
return [{"baustein": b, "gute_antworten": n, "absolviert": a} for b, n, a in rows]
|
||||
|
||||
|
||||
async def add_gute_antwort(topic: str, baustein: str) -> int:
|
||||
"""Zählt eine gut bewertete Antwort und liefert den neuen Stand."""
|
||||
db = await get_db()
|
||||
await db.execute(
|
||||
"""INSERT INTO baustein_progress (topic, baustein, gute_antworten, updated_at)
|
||||
VALUES (?, ?, 1, ?)
|
||||
ON CONFLICT(topic, baustein) DO UPDATE SET
|
||||
gute_antworten = gute_antworten + 1, updated_at = excluded.updated_at""",
|
||||
(topic, baustein, _now()),
|
||||
)
|
||||
await db.commit()
|
||||
cursor = await db.execute(
|
||||
"SELECT gute_antworten FROM baustein_progress WHERE topic = ? AND baustein = ?",
|
||||
(topic, baustein),
|
||||
)
|
||||
row = await cursor.fetchone()
|
||||
return row[0] if row else 0
|
||||
|
||||
|
||||
async def set_baustein_absolviert(topic: str, baustein: str) -> bool:
|
||||
"""Markiert absolviert; True nur beim ersten Mal (steuert den Element-Task)."""
|
||||
db = await get_db()
|
||||
now = _now()
|
||||
await db.execute(
|
||||
"INSERT OR IGNORE INTO baustein_progress (topic, baustein, gute_antworten, updated_at) VALUES (?, ?, 0, ?)",
|
||||
(topic, baustein, now),
|
||||
)
|
||||
cursor = await db.execute(
|
||||
"UPDATE baustein_progress SET absolviert = ?, updated_at = ? "
|
||||
"WHERE topic = ? AND baustein = ? AND absolviert IS NULL",
|
||||
(now, now, topic, baustein),
|
||||
)
|
||||
await db.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
|
||||
async def list_baustein_absolviert_all() -> dict[str, set[str]]:
|
||||
"""Alle absolvierten Bausteine in einem Query: topic → Baustein-Titel."""
|
||||
db = await get_db()
|
||||
cursor = await db.execute(
|
||||
"SELECT topic, baustein FROM baustein_progress WHERE absolviert IS NOT NULL"
|
||||
)
|
||||
rows = await cursor.fetchall()
|
||||
out: dict[str, set[str]] = {}
|
||||
for topic, baustein in rows:
|
||||
out.setdefault(topic, set()).add(baustein)
|
||||
return out
|
||||
|
||||
|
||||
async def delete_baustein_daten(topic: str) -> None:
|
||||
db = await get_db()
|
||||
await db.execute("DELETE FROM vertiefungen WHERE topic = ?", (topic,))
|
||||
await db.execute("DELETE FROM baustein_progress WHERE topic = ?", (topic,))
|
||||
await db.commit()
|
||||
|
||||
Reference in New Issue
Block a user