54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
from typing import List
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from models.library import Record
|
|
from schemas.record import RecordResponse, RecordRequest
|
|
from service import subject_service
|
|
|
|
|
|
async def get_record(db: AsyncSession, subject: str) -> List[RecordResponse]:
|
|
subject_id = await subject_service.get_subject_by_name(db, subject)
|
|
|
|
stmt = (
|
|
select(Record)
|
|
.options(joinedload(Record.subject))
|
|
.where(Record.subject_id == subject_id)
|
|
.order_by(Record.create_time.desc())
|
|
)
|
|
|
|
records = (await db.execute(stmt)).scalars().unique().all()
|
|
|
|
return [
|
|
RecordResponse(
|
|
id=record.id,
|
|
type=record.type,
|
|
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, subject: str, data: RecordRequest):
|
|
subject_id = await subject_service.get_subject_by_name(db, subject)
|
|
|
|
record = Record(
|
|
subject_id=subject_id,
|
|
type=data.type,
|
|
total_count=data.totalCount,
|
|
correct_count=data.correctCount,
|
|
wrong_count=data.wrongCount,
|
|
)
|
|
|
|
db.add(record)
|
|
await db.flush()
|
|
await db.refresh(record)
|
|
await db.commit()
|
|
|
|
return True
|