51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
# database
|
|
from database import get_db
|
|
from models.Patients import Patients
|
|
from schemas.CreateSchemas import PatientCreate
|
|
from schemas.ResponseSchemas import Patient
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Patient])
|
|
def read_patients(
|
|
name: str | None = None,
|
|
dob: str | None = None,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Get a list of patients with optional pagination.
|
|
"""
|
|
query = db.query(Patients)
|
|
if name:
|
|
query = query.filter(Patients.name.ilike(f"%{name}%"))
|
|
if dob:
|
|
query = query.filter(Patients.dob == dob)
|
|
patients = query.offset(skip).limit(limit).all()
|
|
return patients
|
|
|
|
|
|
@router.post("/", response_model=Patient, status_code=status.HTTP_201_CREATED)
|
|
def create_patient(patient: PatientCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Create a new patient.
|
|
"""
|
|
try:
|
|
db_patient = Patients(**patient.model_dump())
|
|
db.add(db_patient)
|
|
db.commit()
|
|
db.refresh(db_patient)
|
|
return db_patient
|
|
except Exception as e:
|
|
db.rollback()
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail=str(e.__cause__),
|
|
) from e
|