feat:更新练习题接口
This commit is contained in:
@@ -1,68 +1,51 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, delete
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models.library import Mistake, LibraryFile, LibraryFileQuestion
|
||||
from schemas.mistake import MistakeResponse
|
||||
from models.library import Mistake
|
||||
from schemas.mistake import MistakeResponse, MistakeRequest
|
||||
|
||||
|
||||
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()
|
||||
stmt = select(Mistake).order_by(Mistake.id.desc())
|
||||
mistakes = (await db.execute(stmt)).scalars().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"],
|
||||
id=mistake.id,
|
||||
subject=mistake.subject,
|
||||
module=mistake.module,
|
||||
name=mistake.name,
|
||||
type=mistake.type,
|
||||
question=mistake.question,
|
||||
options=mistake.options,
|
||||
answer=mistake.answer
|
||||
)
|
||||
for row in rows
|
||||
for mistake in mistakes
|
||||
]
|
||||
|
||||
|
||||
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,
|
||||
async def add_mistakes(db: AsyncSession, data: list[MistakeRequest]):
|
||||
db.add_all(
|
||||
[
|
||||
Mistake(
|
||||
subject=item.subject,
|
||||
module=item.module,
|
||||
name=item.name,
|
||||
type=item.type,
|
||||
question=item.question,
|
||||
options=item.options,
|
||||
answer=item.answer,
|
||||
)
|
||||
for item in data
|
||||
]
|
||||
)
|
||||
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.commit()
|
||||
return True
|
||||
|
||||
await db.flush()
|
||||
|
||||
async def remove_mistake(db: AsyncSession, mistake_id: int) -> bool:
|
||||
await db.execute(delete(Mistake).where(Mistake.id == mistake_id))
|
||||
await db.commit()
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user