Step 3:Study / Site / StudyMember + 项目权限

This commit is contained in:
Cheng Zhou
2025-12-16 16:42:42 +08:00
parent 65df698570
commit f3692e1145
66 changed files with 639 additions and 6 deletions
+69
View File
@@ -0,0 +1,69 @@
import uuid
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.deps import get_db_session, require_study_member, require_study_roles
from app.crud import site as site_crud
from app.crud import study as study_crud
from app.schemas.site import SiteCreate, SiteRead, SiteUpdate
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(
"/",
response_model=SiteRead,
status_code=status.HTTP_201_CREATED,
dependencies=[Depends(require_study_roles(["PM"]))],
)
async def create_site(
study_id: uuid.UUID,
site_in: SiteCreate,
db: AsyncSession = Depends(get_db_session),
) -> SiteRead:
await _ensure_study_exists(db, study_id)
site = await site_crud.create_site(db, study_id, site_in)
return site
@router.get(
"/",
response_model=list[SiteRead],
dependencies=[Depends(require_study_member())],
)
async def list_sites(
study_id: uuid.UUID,
skip: int = 0,
limit: int = 100,
db: AsyncSession = Depends(get_db_session),
) -> list[SiteRead]:
await _ensure_study_exists(db, study_id)
sites = await site_crud.list_by_study(db, study_id, skip=skip, limit=limit)
return list(sites)
@router.patch(
"/{site_id}",
response_model=SiteRead,
dependencies=[Depends(require_study_roles(["PM"]))],
)
async def update_site(
study_id: uuid.UUID,
site_id: uuid.UUID,
site_in: SiteUpdate,
db: AsyncSession = Depends(get_db_session),
) -> SiteRead:
await _ensure_study_exists(db, study_id)
site = await site_crud.get_site(db, site_id)
if not site or site.study_id != study_id:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Site not found")
updated = await site_crud.update_site(db, site, site_in)
return updated