Ali2206 commited on
Commit
aa65e6a
·
verified ·
1 Parent(s): 2275d26

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +46 -3
api/routes.py CHANGED
@@ -3,7 +3,9 @@ from fastapi.security import OAuth2PasswordRequestForm
3
  from models.schemas import SignupForm, TokenResponse, PatientCreate
4
  from db.mongo import users_collection, patients_collection
5
  from core.security import hash_password, verify_password, create_access_token, get_current_user
6
- from datetime import datetime, date
 
 
7
 
8
  router = APIRouter()
9
 
@@ -53,12 +55,53 @@ async def get_me(current_user: dict = Depends(get_current_user)):
53
  async def add_patient(data: PatientCreate, current_user: dict = Depends(get_current_user)):
54
  patient_doc = {
55
  "full_name": data.full_name,
56
- "date_of_birth": datetime.combine(data.date_of_birth, datetime.min.time()), # ✅ fix date type
57
  "gender": data.gender,
58
  "notes": data.notes,
59
  "contact": data.contact.dict() if data.contact else {},
60
  "created_by": current_user["email"],
61
- "created_at": datetime.utcnow() # ✅ use datetime instead of date
62
  }
63
  result = await patients_collection.insert_one(patient_doc)
64
  return { "id": str(result.inserted_id), "message": "Patient created successfully" }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from models.schemas import SignupForm, TokenResponse, PatientCreate
4
  from db.mongo import users_collection, patients_collection
5
  from core.security import hash_password, verify_password, create_access_token, get_current_user
6
+ from datetime import datetime
7
+ from bson import ObjectId
8
+ from bson.errors import InvalidId
9
 
10
  router = APIRouter()
11
 
 
55
  async def add_patient(data: PatientCreate, current_user: dict = Depends(get_current_user)):
56
  patient_doc = {
57
  "full_name": data.full_name,
58
+ "date_of_birth": datetime.combine(data.date_of_birth, datetime.min.time()),
59
  "gender": data.gender,
60
  "notes": data.notes,
61
  "contact": data.contact.dict() if data.contact else {},
62
  "created_by": current_user["email"],
63
+ "created_at": datetime.utcnow()
64
  }
65
  result = await patients_collection.insert_one(patient_doc)
66
  return { "id": str(result.inserted_id), "message": "Patient created successfully" }
67
+
68
+
69
+ # --- GET ALL PATIENTS CREATED BY CURRENT USER ---
70
+ @router.get("/patients")
71
+ async def list_patients(current_user: dict = Depends(get_current_user)):
72
+ patients_cursor = patients_collection.find({"created_by": current_user["email"]})
73
+ patients = []
74
+ async for patient in patients_cursor:
75
+ patients.append({
76
+ "id": str(patient["_id"]),
77
+ "full_name": patient.get("full_name", ""),
78
+ "date_of_birth": patient.get("date_of_birth"),
79
+ "gender": patient.get("gender", ""),
80
+ "notes": patient.get("notes", ""),
81
+ })
82
+ return patients
83
+
84
+
85
+ # --- GET PATIENT BY ID ---
86
+ @router.get("/patients/{patient_id}")
87
+ async def get_patient_by_id(patient_id: str, current_user: dict = Depends(get_current_user)):
88
+ try:
89
+ patient = await patients_collection.find_one({
90
+ "_id": ObjectId(patient_id),
91
+ "created_by": current_user["email"]
92
+ })
93
+ except InvalidId:
94
+ raise HTTPException(status_code=400, detail="Invalid patient ID")
95
+
96
+ if not patient:
97
+ raise HTTPException(status_code=404, detail="Patient not found")
98
+
99
+ return {
100
+ "id": str(patient["_id"]),
101
+ "full_name": patient.get("full_name", ""),
102
+ "date_of_birth": patient.get("date_of_birth"),
103
+ "gender": patient.get("gender", ""),
104
+ "notes": patient.get("notes", ""),
105
+ "contact": patient.get("contact", {}),
106
+ "created_at": patient.get("created_at"),
107
+ }