feat:更新数据表结构
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models.library import ExamRecord
|
||||
from schemas.exam import ExamRecordRequest
|
||||
|
||||
|
||||
async def add_record(db: AsyncSession, data: ExamRecordRequest) -> bool:
|
||||
record = ExamRecord(
|
||||
subject=data.subject,
|
||||
total_count=data.totalCount,
|
||||
correct_count=data.correctCount,
|
||||
wrong_count=data.wrongCount,
|
||||
)
|
||||
|
||||
db.add(record)
|
||||
await db.commit()
|
||||
|
||||
return True
|
||||
@@ -1,19 +1,30 @@
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from models.library import LibraryFile
|
||||
from models.library import LibraryFile, Subject, Module
|
||||
from schemas.library import LibraryFileRequest, LibraryFileResponse
|
||||
|
||||
|
||||
async def list_files(db: AsyncSession) -> list[LibraryFileResponse]:
|
||||
stmt = select(LibraryFile).order_by(LibraryFile.create_time.desc())
|
||||
files = (await db.execute(stmt)).scalars().all()
|
||||
stmt = (
|
||||
select(LibraryFile)
|
||||
.options(
|
||||
joinedload(LibraryFile.subject),
|
||||
joinedload(LibraryFile.module),
|
||||
)
|
||||
.order_by(LibraryFile.create_time.desc())
|
||||
)
|
||||
|
||||
files = (await db.execute(stmt)).scalars().unique().all()
|
||||
|
||||
return [
|
||||
LibraryFileResponse(
|
||||
id=file.id,
|
||||
subject=file.subject,
|
||||
module=file.module,
|
||||
subject=file.subject.name,
|
||||
module=file.module.name,
|
||||
name=file.name,
|
||||
type=file.type,
|
||||
size=file.size,
|
||||
@@ -25,60 +36,108 @@ async def list_files(db: AsyncSession) -> list[LibraryFileResponse]:
|
||||
|
||||
|
||||
async def add_file(db: AsyncSession, data: LibraryFileRequest) -> bool:
|
||||
"""
|
||||
创建题库文件
|
||||
"""
|
||||
file = LibraryFile(
|
||||
subject=data.subject,
|
||||
module=data.module,
|
||||
name=data.name,
|
||||
size=data.size,
|
||||
type=data.type,
|
||||
content=data.content,
|
||||
)
|
||||
try:
|
||||
subject_id = await get_or_create_subject(db, data.subject)
|
||||
module_id = await get_or_create_module(db, subject_id, data.module)
|
||||
|
||||
db.add(file)
|
||||
await db.commit()
|
||||
file = LibraryFile(
|
||||
subject_id=subject_id,
|
||||
module_id=module_id,
|
||||
name=data.name,
|
||||
size=data.size,
|
||||
type=data.type,
|
||||
content=data.content,
|
||||
)
|
||||
|
||||
return True
|
||||
db.add(file)
|
||||
await db.commit()
|
||||
return True
|
||||
|
||||
except SQLAlchemyError:
|
||||
await db.rollback()
|
||||
raise
|
||||
|
||||
|
||||
async def edit_file(db: AsyncSession, file_id: int, data: LibraryFileRequest) -> bool:
|
||||
"""
|
||||
编辑题库文件
|
||||
"""
|
||||
try:
|
||||
result = await db.execute(
|
||||
select(LibraryFile).where(LibraryFile.id == file_id)
|
||||
)
|
||||
file = result.scalar_one_or_none()
|
||||
if not file:
|
||||
return False
|
||||
|
||||
result = await db.execute(
|
||||
select(LibraryFile).where(LibraryFile.id == file_id)
|
||||
)
|
||||
file = result.scalar_one_or_none()
|
||||
if not file:
|
||||
return False
|
||||
subject_id = await get_or_create_subject(db, data.subject)
|
||||
module_id = await get_or_create_module(db, subject_id, data.module)
|
||||
|
||||
file.subject = data.subject
|
||||
file.module = data.module
|
||||
file.name = data.name
|
||||
file.size = data.size
|
||||
file.type = data.type
|
||||
file.content = data.content
|
||||
file.subject_id = subject_id
|
||||
file.module_id = module_id
|
||||
file.name = data.name
|
||||
file.size = data.size
|
||||
file.type = data.type
|
||||
file.content = data.content
|
||||
|
||||
await db.commit()
|
||||
await db.commit()
|
||||
return True
|
||||
|
||||
return True
|
||||
except SQLAlchemyError:
|
||||
await db.rollback()
|
||||
raise
|
||||
|
||||
|
||||
async def get_file(db: AsyncSession, file_id: int) -> LibraryFileResponse:
|
||||
stmt = (select(LibraryFile).where(LibraryFile.id == file_id))
|
||||
result = await db.execute(stmt)
|
||||
file = result.scalar_one_or_none()
|
||||
stmt = (
|
||||
select(LibraryFile)
|
||||
.options(
|
||||
joinedload(LibraryFile.subject),
|
||||
joinedload(LibraryFile.module),
|
||||
)
|
||||
.where(LibraryFile.id == file_id)
|
||||
)
|
||||
|
||||
file = (await db.execute(stmt)).scalar_one_or_none()
|
||||
if not file:
|
||||
raise HTTPException(status_code=404, detail="File not found")
|
||||
|
||||
return LibraryFileResponse(
|
||||
id=file.id,
|
||||
subject=file.subject,
|
||||
module=file.module,
|
||||
subject=file.subject.name,
|
||||
module=file.module.name,
|
||||
name=file.name,
|
||||
type=file.type,
|
||||
size=file.size,
|
||||
content=file.content,
|
||||
uploadTime=file.create_time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
)
|
||||
|
||||
|
||||
async def get_or_create_subject(db: AsyncSession, name: str) -> int:
|
||||
stmt = select(Subject).where(Subject.name == name)
|
||||
result = await db.execute(stmt)
|
||||
subject = result.scalar_one_or_none()
|
||||
|
||||
if subject:
|
||||
return subject.id
|
||||
|
||||
subject = Subject(name=name)
|
||||
db.add(subject)
|
||||
await db.flush()
|
||||
|
||||
return subject.id
|
||||
|
||||
|
||||
async def get_or_create_module(db: AsyncSession, subject_id: int, name: str) -> int:
|
||||
stmt = select(Module).where(
|
||||
Module.subject_id == subject_id,
|
||||
Module.name == name,
|
||||
)
|
||||
module = (await db.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
if module:
|
||||
return module.id
|
||||
|
||||
module = Module(subject_id=subject_id, name=name)
|
||||
db.add(module)
|
||||
await db.flush()
|
||||
|
||||
return module.id
|
||||
|
||||
@@ -1,47 +1,67 @@
|
||||
from sqlalchemy import select, delete
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from models.library import Mistake
|
||||
from models.library import Mistake, Subject
|
||||
from schemas.mistake import MistakeResponse, MistakeRequest
|
||||
from service import subject_service
|
||||
|
||||
|
||||
async def list_mistakes(db: AsyncSession) -> list[MistakeResponse]:
|
||||
stmt = select(Mistake).order_by(Mistake.id.desc())
|
||||
mistakes = (await db.execute(stmt)).scalars().all()
|
||||
async def list_mistakes(subject: str, db: AsyncSession) -> list[MistakeResponse]:
|
||||
subject_id = await subject_service.get_subject_by_name(db, subject)
|
||||
stmt = (
|
||||
select(Mistake)
|
||||
.options(joinedload(Mistake.subject))
|
||||
.where(Mistake.subject_id == subject_id)
|
||||
.order_by(Mistake.id.desc())
|
||||
)
|
||||
|
||||
mistakes = (await db.execute(stmt)).scalars().unique().all()
|
||||
|
||||
return [
|
||||
MistakeResponse(
|
||||
id=mistake.id,
|
||||
subject=mistake.subject,
|
||||
module=mistake.module,
|
||||
name=mistake.name,
|
||||
type=mistake.type,
|
||||
question=mistake.question,
|
||||
options=mistake.options,
|
||||
answer=mistake.answer
|
||||
answer=mistake.answer,
|
||||
)
|
||||
for mistake in mistakes
|
||||
]
|
||||
|
||||
|
||||
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
|
||||
]
|
||||
)
|
||||
async def add_mistakes(db: AsyncSession, subject: str, data: list[MistakeRequest]):
|
||||
subject_id = await subject_service.get_subject_by_name(db, subject)
|
||||
|
||||
await db.commit()
|
||||
return True
|
||||
subject = (
|
||||
await db.execute(
|
||||
select(Subject).where(Subject.id == subject_id)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
|
||||
if not subject:
|
||||
raise ValueError("Subject not found")
|
||||
|
||||
mistakes = [
|
||||
Mistake(
|
||||
subject_id=subject_id,
|
||||
type=item.type,
|
||||
question=item.question,
|
||||
options=item.options,
|
||||
answer=item.answer,
|
||||
)
|
||||
for item in data
|
||||
]
|
||||
|
||||
try:
|
||||
db.add_all(mistakes)
|
||||
await db.flush()
|
||||
await db.commit()
|
||||
return True
|
||||
except SQLAlchemyError:
|
||||
await db.rollback()
|
||||
raise
|
||||
|
||||
|
||||
async def remove_mistake(db: AsyncSession, mistake_id: int) -> bool:
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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
|
||||
53
service/record_service.py
Normal file
53
service/record_service.py
Normal file
@@ -0,0 +1,53 @@
|
||||
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
|
||||
13
service/subject_service.py
Normal file
13
service/subject_service.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models.library import Subject
|
||||
|
||||
|
||||
async def get_subject_by_name(db: AsyncSession, subject: str) -> int:
|
||||
stmt = select(Subject).where(Subject.name == subject)
|
||||
subject = (await db.execute(stmt)).scalar_one_or_none()
|
||||
if not subject:
|
||||
raise ValueError("Subject not found")
|
||||
|
||||
return subject.id
|
||||
Reference in New Issue
Block a user