diff --git a/database.py b/database.py index c62b308..dad15c5 100644 --- a/database.py +++ b/database.py @@ -258,20 +258,31 @@ async def init_db(): ) """) - # ============ Убираем старый UNIQUE constraint с sentences.sentence_number ============ + # ============ Убираем составной UNIQUE (level, sentence_number) ============ await conn.execute(""" - ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_key + ALTER TABLE sentences DROP CONSTRAINT IF EXISTS sentences_sentence_number_unique """) - # ============ Добавляем составной UNIQUE (level, sentence_number) ============ + # ============ Пересчёт номеров: единый пул 1..N ============ + await conn.execute(""" + UPDATE sentences s + SET sentence_number = sub.rn + FROM ( + SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS rn + FROM sentences + ) sub + WHERE s.id = sub.id + """) + + # ============ Добавляем UNIQUE (sentence_number) ============ await conn.execute(""" DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_constraint - WHERE conname = 'sentences_sentence_number_unique' + WHERE conname = 'sentences_sentence_number_key' ) THEN - ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_unique UNIQUE (level, sentence_number); + ALTER TABLE sentences ADD CONSTRAINT sentences_sentence_number_key UNIQUE (sentence_number); END IF; END $$; """) diff --git a/routers/sentences.py b/routers/sentences.py index c732c98..22b56bc 100644 --- a/routers/sentences.py +++ b/routers/sentences.py @@ -65,6 +65,7 @@ async def init_sentences(): ("B1", "sentences_b1.csv"), ] + offset = 0 for level, filename in level_files: sentences = load_sentences_from_csv(filename) for i, s in enumerate(sentences): @@ -74,11 +75,12 @@ async def init_sentences(): await conn.execute( """INSERT INTO sentences (sentence_number, sentence_pl, translation_ru, translation_uk, translation_en, difficulty, level) VALUES ($1, $2, $3, $4, $5, $6, $7) - ON CONFLICT (level, sentence_number) DO NOTHING""", - i + 1, s["sentence_pl"], + ON CONFLICT (sentence_number) DO NOTHING""", + offset + i + 1, s["sentence_pl"], s["translation_ru"], s["translation_uk"], s["translation_en"], difficulty, level ) + offset += len(sentences) def normalize_word_for_display(word: str) -> str: @@ -100,25 +102,23 @@ async def get_next_sentence(request: Request): pool = await get_pool() async with pool.acquire() as conn: user = await conn.fetchrow( - "SELECT app_language, current_level, difficulty FROM users WHERE telegram_id = $1", telegram_id + "SELECT app_language, difficulty FROM users WHERE telegram_id = $1", telegram_id ) if not user: raise HTTPException(401, "User not registered") lang = user["app_language"] or "pl" - current_level = user["current_level"] or "A1" difficulty = user["difficulty"] or "medium" - # Находим случайное непройденное предложение нужного уровня + # Находим случайное непройденное предложение (единый пул) row = await conn.fetchrow(""" SELECT s.* FROM sentences s - WHERE s.level = $1 - AND s.id NOT IN ( - SELECT sentence_id FROM sentence_progress WHERE user_id = $2 AND completed_at IS NOT NULL + WHERE s.id NOT IN ( + SELECT sentence_id FROM sentence_progress WHERE user_id = $1 AND completed_at IS NOT NULL ) ORDER BY RANDOM() LIMIT 1 - """, current_level, telegram_id) + """, telegram_id) if not row: return {"completed": True, "message": "All sentences completed!"} @@ -410,21 +410,14 @@ async def get_sentence_stats(request: Request): pool = await get_pool() async with pool.acquire() as conn: - user = await conn.fetchrow( - "SELECT current_level FROM users WHERE telegram_id = $1", telegram_id - ) - current_level = user["current_level"] if user else "A1" - - total = await conn.fetchval( - "SELECT COUNT(*) FROM sentences WHERE level = $1", current_level - ) + total = await conn.fetchval("SELECT COUNT(*) FROM sentences") completed = await conn.fetchval( - "SELECT COUNT(*) FROM sentence_progress WHERE user_id = $1 AND level = $2", - telegram_id, current_level + "SELECT COUNT(*) FROM sentence_progress WHERE user_id = $1 AND completed_at IS NOT NULL", + telegram_id ) total_score = await conn.fetchval( - "SELECT COALESCE(SUM(score), 0) FROM sentence_progress WHERE user_id = $1 AND level = $2", - telegram_id, current_level + "SELECT COALESCE(SUM(score), 0) FROM sentence_progress WHERE user_id = $1 AND completed_at IS NOT NULL", + telegram_id ) return { @@ -433,5 +426,4 @@ async def get_sentence_stats(request: Request): "remaining": total - (completed or 0), "total_score": total_score or 0, "progress_percent": round(((completed or 0) / total * 100), 1) if total > 0 else 0, - "level": current_level, } \ No newline at end of file