69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from models.library import Mistake, LibraryFile, LibraryFileQuestion
|
|
from schemas.mistake import MistakeResponse
|
|
|
|
|
|
async def list_mistakes(db: AsyncSession) -> list[MistakeResponse]:
|
|
stmt = (
|
|
select(
|
|
Mistake.id,
|
|
LibraryFile.subject,
|
|
LibraryFile.module,
|
|
LibraryFile.name,
|
|
LibraryFileQuestion.type,
|
|
LibraryFileQuestion.question,
|
|
LibraryFileQuestion.options,
|
|
LibraryFileQuestion.answer,
|
|
Mistake.wrong_count,
|
|
)
|
|
.join(
|
|
LibraryFileQuestion,
|
|
Mistake.question_id == LibraryFileQuestion.id,
|
|
)
|
|
.join(
|
|
LibraryFile,
|
|
Mistake.library_file_id == LibraryFile.id,
|
|
)
|
|
)
|
|
stmt = stmt.order_by(Mistake.create_time.desc())
|
|
result = await db.execute(stmt)
|
|
rows = result.mappings().all()
|
|
|
|
return [
|
|
MistakeResponse(
|
|
id=row["id"],
|
|
subject=row["subject"],
|
|
module=row["module"],
|
|
name=row["name"],
|
|
type=row["type"],
|
|
question=row["question"],
|
|
options=row["options"],
|
|
answer=row["answer"],
|
|
wrongCount=row["wrong_count"],
|
|
)
|
|
for row in rows
|
|
]
|
|
|
|
|
|
async def update_mistake(db: AsyncSession, library_file_id: int, question_id: int):
|
|
stmt = select(Mistake).where(
|
|
Mistake.library_file_id == library_file_id,
|
|
Mistake.question_id == question_id,
|
|
)
|
|
result = await db.execute(stmt)
|
|
wrong = result.scalar_one_or_none()
|
|
|
|
if wrong:
|
|
wrong.wrong_count += 1
|
|
else:
|
|
wrong = Mistake(
|
|
library_file_id=library_file_id,
|
|
question_id=question_id,
|
|
wrong_count=1
|
|
)
|
|
db.add(wrong)
|
|
|
|
await db.flush()
|