39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from typing import List
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from models.library import PracticeRecord
|
|
from schemas.practice import PracticeRecordRequest, PracticeRecordResponse
|
|
|
|
|
|
async def get_record(db: AsyncSession, library_file_id: int) -> List[PracticeRecordResponse]:
|
|
stmt = select(PracticeRecord).where(PracticeRecord.library_file_id == library_file_id).order_by(PracticeRecord.create_time.desc())
|
|
records = (await db.execute(stmt)).scalars().all()
|
|
|
|
return [
|
|
PracticeRecordResponse(
|
|
id=record.id,
|
|
libraryFileId=record.library_file_id,
|
|
totalCount=record.total_count,
|
|
correctCount=record.correct_count,
|
|
wrongCount=record.wrong_count,
|
|
createTime=record.create_time.strftime("%Y-%m-%d %H:%M:%S"),
|
|
)
|
|
for record in records
|
|
]
|
|
|
|
|
|
async def add_record(db: AsyncSession, data: PracticeRecordRequest):
|
|
record = PracticeRecord(
|
|
library_file_id=data.libraryFileId,
|
|
total_count=data.totalCount,
|
|
correct_count=data.correctCount,
|
|
wrong_count=data.wrongCount,
|
|
)
|
|
|
|
db.add(record)
|
|
await db.commit()
|
|
|
|
return True
|