信息架构/菜单框架大重构-20260109

This commit is contained in:
Cheng Zhou
2026-01-09 11:19:04 +08:00
parent ba3cf95b8a
commit 9021c7fe2b
188 changed files with 7632 additions and 11658 deletions
+144
View File
@@ -0,0 +1,144 @@
import uuid
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.deps import get_current_user, get_db_session, require_study_member, require_study_roles
from app.crud import audit as audit_crud
from app.crud import subject_history as history_crud
from app.crud import study as study_crud
from app.schemas.subject_history import SubjectHistoryCreate, SubjectHistoryRead, SubjectHistoryUpdate
router = APIRouter()
async def _ensure_study_exists(db: AsyncSession, study_id: uuid.UUID):
study = await study_crud.get(db, study_id)
if not study:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Study not found")
return study
@router.post(
"/histories",
response_model=SubjectHistoryRead,
status_code=status.HTTP_201_CREATED,
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
)
async def create_history(
study_id: uuid.UUID,
subject_id: uuid.UUID,
history_in: SubjectHistoryCreate,
db: AsyncSession = Depends(get_db_session),
current_user=Depends(get_current_user),
) -> SubjectHistoryRead:
await _ensure_study_exists(db, study_id)
if history_in.subject_id != subject_id:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Subject mismatch")
history = await history_crud.create_history(db, study_id, history_in, created_by=current_user.id)
await audit_crud.log_action(
db,
study_id=study_id,
entity_type="subject_history",
entity_id=history.id,
action="CREATE_SUBJECT_HISTORY",
detail="Subject history created",
operator_id=current_user.id,
operator_role=current_user.role,
)
return SubjectHistoryRead.model_validate(history)
@router.get(
"/histories",
response_model=list[SubjectHistoryRead],
dependencies=[Depends(require_study_member())],
)
async def list_histories(
study_id: uuid.UUID,
subject_id: uuid.UUID,
skip: int = 0,
limit: int = 200,
db: AsyncSession = Depends(get_db_session),
) -> list[SubjectHistoryRead]:
await _ensure_study_exists(db, study_id)
items = await history_crud.list_histories(db, study_id, subject_id, skip=skip, limit=limit)
return [SubjectHistoryRead.model_validate(item) for item in items]
@router.get(
"/histories/{history_id}",
response_model=SubjectHistoryRead,
dependencies=[Depends(require_study_member())],
)
async def get_history(
study_id: uuid.UUID,
subject_id: uuid.UUID,
history_id: uuid.UUID,
db: AsyncSession = Depends(get_db_session),
) -> SubjectHistoryRead:
await _ensure_study_exists(db, study_id)
history = await history_crud.get_history(db, history_id)
if not history or history.study_id != study_id or history.subject_id != subject_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="History not found")
return SubjectHistoryRead.model_validate(history)
@router.patch(
"/histories/{history_id}",
response_model=SubjectHistoryRead,
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
)
async def update_history(
study_id: uuid.UUID,
subject_id: uuid.UUID,
history_id: uuid.UUID,
history_in: SubjectHistoryUpdate,
db: AsyncSession = Depends(get_db_session),
current_user=Depends(get_current_user),
) -> SubjectHistoryRead:
await _ensure_study_exists(db, study_id)
history = await history_crud.get_history(db, history_id)
if not history or history.study_id != study_id or history.subject_id != subject_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="History not found")
history = await history_crud.update_history(db, history, history_in)
await audit_crud.log_action(
db,
study_id=study_id,
entity_type="subject_history",
entity_id=history_id,
action="UPDATE_SUBJECT_HISTORY",
detail="Subject history updated",
operator_id=current_user.id,
operator_role=current_user.role,
)
return SubjectHistoryRead.model_validate(history)
@router.delete(
"/histories/{history_id}",
status_code=status.HTTP_204_NO_CONTENT,
dependencies=[Depends(require_study_roles(["PM", "CRA"]))],
)
async def delete_history(
study_id: uuid.UUID,
subject_id: uuid.UUID,
history_id: uuid.UUID,
db: AsyncSession = Depends(get_db_session),
current_user=Depends(get_current_user),
) -> None:
await _ensure_study_exists(db, study_id)
history = await history_crud.get_history(db, history_id)
if not history or history.study_id != study_id or history.subject_id != subject_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="History not found")
await history_crud.delete_history(db, history)
await audit_crud.log_action(
db,
study_id=study_id,
entity_type="subject_history",
entity_id=history_id,
action="DELETE_SUBJECT_HISTORY",
detail="Subject history deleted",
operator_id=current_user.id,
operator_role=current_user.role,
)