Skip to content

Commit

Permalink
Merge branch 'main' into feat/vital-threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
timothy-lew committed Jan 13, 2025
2 parents c28a5c8 + b4e9aa7 commit 168e8c5
Show file tree
Hide file tree
Showing 27 changed files with 584 additions and 463 deletions.
59 changes: 0 additions & 59 deletions app/crud/patient_mobility_crud.py

This file was deleted.

97 changes: 97 additions & 0 deletions app/crud/patient_mobility_list_crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from sqlalchemy.orm import Session
from datetime import datetime
from fastapi import HTTPException
from ..models.patient_mobility_list_model import PatientMobilityList
from ..models.patient_mobility_mapping_model import PatientMobility
from ..schemas.patient_mobility_list import (
PatientMobilityListCreate,
PatientMobilityListUpdate,
)
from ..schemas.patient_mobility_mapping import (
PatientMobilityCreate,
PatientMobilityUpdate,
)

# CRUD for PATIENT_MOBILITY_LIST

# Get all mobility list entries
def get_all_mobility_list_entries(db: Session):
try:
entries = db.query(PatientMobilityList).filter(PatientMobilityList.IsDeleted == "0").all()
if not entries:
raise HTTPException(status_code=404, detail="No mobility list entries found.")
return entries
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error querying mobility list: {str(e)}")


# Get a single mobility list entry by ID
def get_mobility_list_entry_by_id(db: Session, mobility_list_id: int):
entry = db.query(PatientMobilityList).filter(
PatientMobilityList.MobilityListId == mobility_list_id,
PatientMobilityList.IsDeleted == '0'
).first()
if not entry:
raise HTTPException(status_code=404, detail=f"Mobility list entry with ID {mobility_list_id} not found.")
return entry

# Create a new mobility list entry
def create_mobility_list_entry(db: Session, mobility_list_data: PatientMobilityListCreate, created_by: int):
new_entry = PatientMobilityList(
**mobility_list_data.dict(exclude={"CreatedDateTime", "ModifiedDateTime", "CreatedById", "ModifiedById"}), # Corrected set syntax
CreatedDateTime=datetime.utcnow(),
ModifiedDateTime=datetime.utcnow(),
CreatedById=created_by,
ModifiedById=created_by,
)
db.add(new_entry)
db.commit()
db.refresh(new_entry)
return new_entry

# Update a mobility list entry
def update_mobility_list_entry(
db: Session, mobility_list_id: int, mobility_list_data: PatientMobilityListUpdate, modified_by: int
):
db_entry = db.query(PatientMobilityList).filter(
PatientMobilityList.MobilityListId == mobility_list_id,
PatientMobilityList.IsDeleted == '0',
).first()

if db_entry:
for key, value in mobility_list_data.dict(exclude={"MobilityListId"}).items(): # Exclude MobilityListId
setattr(db_entry, key, value)

db_entry.ModifiedDateTime = datetime.utcnow()
db_entry.ModifiedById = modified_by

db.commit()
db.refresh(db_entry)
return db_entry
return None

# Soft delete a mobility list entry (set IsDeleted to '1')

def delete_mobility_list_entry(db: Session, mobility_list_id: int, modified_by: int):
# Query for the entry to be deleted
db_entry = db.query(PatientMobilityList).filter(
PatientMobilityList.MobilityListId == mobility_list_id,
PatientMobilityList.IsDeleted == "0" # Use boolean False for filtering
).first()

# Raise an exception if the entry is not found
if not db_entry:
raise HTTPException(
status_code=404,
detail=f"Mobility list entry with ID {mobility_list_id} not found."
)

# Soft delete the entry by setting IsDeleted to True
db_entry.IsDeleted = "1" # Use boolean True
db_entry.ModifiedDateTime = datetime.utcnow()
db_entry.ModifiedById = modified_by

# Commit the transaction to save the changes
db.commit()
db.refresh(db_entry) # Refresh the entry to return the updated instance
return db_entry
89 changes: 89 additions & 0 deletions app/crud/patient_mobility_mapping_crud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from app.models.patient_mobility_list_model import PatientMobilityList
from app.models.patient_model import Patient
from sqlalchemy.orm import Session
from datetime import datetime
from fastapi import HTTPException
from ..models.patient_mobility_mapping_model import PatientMobility
from ..schemas.patient_mobility_mapping import (
PatientMobilityCreate,
PatientMobilityUpdate,
)

# Get all mobility entries
def get_all_mobility_entries(db: Session):
return db.query(PatientMobility).filter(PatientMobility.IsDeleted == False).all()

# Get a single mobility entry by ID
def get_mobility_entry_by_id(db: Session, mobility_id: int):
entry = db.query(PatientMobility).filter(
PatientMobility.MobilityID == mobility_id,
PatientMobility.IsDeleted == False
).first()
if not entry:
raise HTTPException(status_code=404, detail=f"Mobility entry with ID {mobility_id} not found.")
return entry

# Create a new mobility entry
def create_mobility_entry(db: Session, mobility_data: PatientMobilityCreate, created_by: int):
# Validate PatientID
patient = db.query(Patient).filter(Patient.id == mobility_data.PatientID).first()
if not patient:
raise HTTPException(status_code=400, detail="Invalid PatientID. No matching patient found.")

# Validate MobilityListId
mobility_list = db.query(PatientMobilityList).filter(PatientMobilityList.MobilityListId == mobility_data.MobilityListId).first()
if not mobility_list:
raise HTTPException(status_code=400, detail="Invalid MobilityListId. No matching mobility list found.")

# Create entry
new_entry = PatientMobility(
**mobility_data.dict(),
CreatedDateTime=datetime.utcnow(),
ModifiedDateTime=datetime.utcnow(),
CreatedById=created_by,
ModifiedById=created_by,
)
db.add(new_entry)
db.commit()
db.refresh(new_entry)
return new_entry


# Update an existing mobility entry
def update_mobility_entry(
db: Session, mobility_id: int, mobility_data: PatientMobilityUpdate, modified_by: int
):
db_entry = db.query(PatientMobility).filter(
PatientMobility.MobilityID == mobility_id,
PatientMobility.IsDeleted == False,
).first()

if not db_entry:
raise HTTPException(status_code=404, detail=f"Mobility entry with ID {mobility_id} not found.")

for key, value in mobility_data.dict(exclude_unset=True).items():
setattr(db_entry, key, value)

db_entry.ModifiedDateTime = datetime.utcnow()
db_entry.ModifiedById = modified_by

db.commit()
db.refresh(db_entry)
return db_entry

# Soft delete a mobility entry
def delete_mobility_entry(db: Session, mobility_id: int, modified_by: int):
db_entry = db.query(PatientMobility).filter(
PatientMobility.MobilityID == mobility_id,
PatientMobility.IsDeleted == False,
).first()

if not db_entry:
raise HTTPException(status_code=404, detail=f"Mobility entry with ID {mobility_id} not found.")

db_entry.IsDeleted = True
db_entry.ModifiedDateTime = datetime.utcnow()
db_entry.ModifiedById = modified_by

db.commit()
return db_entry
10 changes: 7 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import FastAPI,Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

from sqlalchemy.orm import clear_mappers # Import clear_mappers
from .database import engine, Base
from app.routers import (
allergy_reaction_type_router,
Expand All @@ -19,12 +19,15 @@
patient_vital_router,
patient_assigned_dementia_list_router,
patient_assigned_dementia_mapping_router,
patient_mobility_mapping_router,
)
from fastapi.middleware.cors import CORSMiddleware
import os
from dotenv import load_dotenv

load_dotenv()
# clear_mappers()


app = FastAPI(
title="NTU FYP PEAR PATIENT SERVICE",
Expand Down Expand Up @@ -60,8 +63,7 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
status_code=400,
content={"detail": exc.errors(), "body": exc.body},
)


# Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)

# Include the routers with prefixes and tags
Expand All @@ -86,6 +88,8 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
)
app.include_router(patient_list_router.router, prefix="/api/v1", tags=["patient lists"])
app.include_router(patient_mobility_router.router, prefix="/api/v1", tags=["mobility"])
app.include_router(patient_mobility_mapping_router.router, prefix="/api/v1", tags=["Patient Mobility Mapping"],
)
app.include_router(patient_photo_router.router, prefix="/api/v1", tags=["photos"])
app.include_router(
patient_prescription_router.router, prefix="/api/v1", tags=["prescriptions"]
Expand Down
2 changes: 1 addition & 1 deletion app/models/patient_list_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ class PatientList(Base):
# allergy_reactions = relationship("PatientAllergy", back_populates="allergy_reaction_list", foreign_keys="[PatientAllergy.allergyReactionListId]")
photos = relationship("PatientPhoto", back_populates="album_category")
# dementia_assignments = relationship("PatientAssignedDementia", back_populates="dementia_type")
mobility_records = relationship("PatientMobility", back_populates="mobility_list")
# mobility_records = relationship("PatientMobility", back_populates="mobility_list")
# prescriptions = relationship("PatientPrescription", back_populates="prescription_list")
# social_history_mappings = relationship("PatientSocialHistoryListMapping", back_populates="list_entry")
23 changes: 14 additions & 9 deletions app/models/patient_mobility_list_model.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime, timezone
from datetime import datetime
from app.database import Base

class PatientMobilityList(Base):
__tablename__ = "PATIENT_MOBILITY_LIST_MAPPING"
__tablename__ = "PATIENT_MOBILITY_LIST"

id = Column(Integer, primary_key=True, index=True)
mobilityListId = Column(Integer, ForeignKey('PATIENT_MOBILITY_LIST_MAPPING.id'), nullable=False)
IsDeleted = Column(Boolean, default=False, nullable=False)
createdDate = Column(DateTime, nullable=False, default=DateTime)
modifiedDate = Column(DateTime, nullable=False, default=DateTime)
value = Column(String(255), nullable=False)
MobilityListId = Column(Integer, primary_key=True, index=True)
IsDeleted = Column(Integer, default=False, nullable=False)
CreatedDateTime = Column(DateTime, nullable=False, default=datetime.utcnow)
ModifiedDateTime = Column(DateTime, nullable=False, default=datetime.utcnow)
CreatedById = Column(Integer, nullable=False) # Changed to Integer
ModifiedById = Column(Integer, nullable=False) # Changed to Integer
Value = Column(String(255), nullable=False)

mobility_records = relationship("PatientMobility", secondary="PATIENT_MOBILITY_PATIENT_MOBILITY_LIST", back_populates="mobility_lists")
mobility_records = relationship(
"PatientMobility",
back_populates="mobility_list",
primaryjoin="PatientMobility.MobilityListId == PatientMobilityList.MobilityListId"
)
27 changes: 27 additions & 0 deletions app/models/patient_mobility_mapping_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from datetime import datetime, timezone
from app.database import Base

class PatientMobility(Base):
__tablename__ = "PATIENT_MOBILITY_MAPPING"

MobilityID = Column(Integer, primary_key=True, index=True)
PatientID = Column(Integer, ForeignKey('PATIENT.id'), nullable=False)
MobilityListId = Column(Integer, ForeignKey('PATIENT_MOBILITY_LIST.MobilityListId'), nullable=False) # Foreign Key
MobilityRemarks = Column(String(255))
IsRecovered = Column(Boolean, default=False, nullable=False)
IsDeleted = Column(Boolean, default=False, nullable=False)
CreatedDateTime = Column(DateTime, nullable=False, default=datetime.utcnow)
ModifiedDateTime = Column(DateTime, nullable=False, default=datetime.utcnow)
CreatedById = Column(Integer, nullable=False)
ModifiedById = Column(Integer, nullable=False)

mobility_list = relationship(
"PatientMobilityList",
back_populates="mobility_records",
primaryjoin="PatientMobility.MobilityListId == PatientMobilityList.MobilityListId"
)

# Relationship with Patient
patient = relationship("Patient", back_populates="mobility_records")
21 changes: 0 additions & 21 deletions app/models/patient_mobility_model.py

This file was deleted.

16 changes: 0 additions & 16 deletions app/models/patient_mobility_patient_mobility_list_model.py

This file was deleted.

Loading

0 comments on commit 168e8c5

Please sign in to comment.