Files
2026-09-20 11:45:58 +00:00

46 lines
1.7 KiB
Python

import random
from fastapi import APIRouter, Request, HTTPException
from auth import require_auth
from database import get_pool
router = APIRouter(prefix="/pronunciation", tags=["pronunciation"])
@router.get("/next")
async def get_next_word(request: Request):
"""Выдаёт случайное слово из всех уровней для раздела Произношение."""
telegram_id, _ = await require_auth(request)
pool = await get_pool()
async with pool.acquire() as conn:
user = await conn.fetchrow(
"SELECT app_language FROM users WHERE telegram_id = $1", telegram_id
)
if not user:
raise HTTPException(401, "User not registered")
lang = user["app_language"] or "pl"
# Берём случайное слово из всех уровней (без привязки к конкретному уровню)
word = await conn.fetchrow("""
SELECT word_pl, translation_ru, translation_uk, translation_en, level
FROM (
SELECT DISTINCT ON (word_pl) word_pl, translation_ru, translation_uk, translation_en, level
FROM level_words
WHERE level_id IS NULL
) AS unique_words
ORDER BY RANDOM()
LIMIT 1
""")
if not word:
raise HTTPException(404, "No words found")
translation_field = f"translation_{lang}"
translation = word.get(translation_field) or word.get("translation_en") or word["word_pl"]
return {
"word_pl": word["word_pl"],
"translation": translation,
"level": word["level"] or "A1",
}