from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from typing import List
from app.api.dependencies.database import get_async_session
from app.api.dependencies.auth import get_current_active_user
from app.schemas.patient import PatientCreate, PatientUpdate, PatientResponse
from app.models.patient import Patient
from app.models.user import User
router = APIRouter(prefix="/patients", tags=["patients"])
@router.post("/", response_model=PatientResponse, operation_id="create_patient")
async def create_patient(
patient_data: PatientCreate,
db: AsyncSession = Depends(get_async_session),
current_user: User = Depends(get_current_active_user),
):
patient = Patient(**patient_data.model_dump())
db.add(patient)
await db.commit()
await db.refresh(patient)
return patient
@router.get("/", response_model=List[PatientResponse], operation_id="get_patients")
async def get_patients(
skip: int = 0,
limit: int = 100,
db: AsyncSession = Depends(get_async_session),
current_user: User = Depends(get_current_active_user),
):
result = await db.execute(select(Patient).offset(skip).limit(limit))
patients = result.scalars().all()
return patients
@router.get("/{patient_id}", response_model=PatientResponse, operation_id="get_patient")
async def get_patient(
patient_id: int,
db: AsyncSession = Depends(get_async_session),
current_user: User = Depends(get_current_active_user),
):
result = await db.execute(select(Patient).where(Patient.id == patient_id))
patient = result.scalar_one_or_none()
if not patient:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Patient not found"
)
return patient
@router.put("/{patient_id}", response_model=PatientResponse, operation_id="update_patient")
async def update_patient(
patient_id: int,
patient_data: PatientUpdate,
db: AsyncSession = Depends(get_async_session),
current_user: User = Depends(get_current_active_user),
):
result = await db.execute(select(Patient).where(Patient.id == patient_id))
patient = result.scalar_one_or_none()
if not patient:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Patient not found"
)
update_data = patient_data.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(patient, field, value)
await db.commit()
await db.refresh(patient)
return patient
@router.delete("/{patient_id}", operation_id="delete_patient")
async def delete_patient(
patient_id: int,
db: AsyncSession = Depends(get_async_session),
current_user: User = Depends(get_current_active_user),
):
result = await db.execute(select(Patient).where(Patient.id == patient_id))
patient = result.scalar_one_or_none()
if not patient:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Patient not found"
)
await db.delete(patient)
await db.commit()
return {"message": "Patient deleted successfully"}