Spaces:
Sleeping
Sleeping
Update api/routes.py
Browse files- api/routes.py +49 -5
api/routes.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from fastapi import APIRouter, HTTPException, Depends, Body
|
| 2 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 3 |
-
from models.schemas import SignupForm, TokenResponse, PatientCreate, DoctorCreate
|
| 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
|
|
@@ -11,7 +11,7 @@ from pydantic import BaseModel
|
|
| 11 |
|
| 12 |
router = APIRouter()
|
| 13 |
|
| 14 |
-
# --- SIGNUP
|
| 15 |
@router.post("/signup")
|
| 16 |
async def signup(data: SignupForm):
|
| 17 |
if data.role != "patient":
|
|
@@ -107,11 +107,55 @@ async def list_patients(current_user: dict = Depends(get_current_user)):
|
|
| 107 |
})
|
| 108 |
return patients
|
| 109 |
|
| 110 |
-
# --- COUNT PATIENTS
|
| 111 |
@router.get("/patients-count")
|
| 112 |
async def count_patients(current_user: dict = Depends(get_current_user)):
|
| 113 |
if current_user.get("role") != "doctor":
|
| 114 |
raise HTTPException(status_code=403, detail="Only doctors can count patients")
|
| 115 |
-
|
| 116 |
count = await patients_collection.count_documents({"created_by": current_user["email"]})
|
| 117 |
return {"count": count}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, HTTPException, Depends, Body
|
| 2 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 3 |
+
from models.schemas import SignupForm, TokenResponse, PatientCreate, DoctorCreate, AppointmentCreate
|
| 4 |
+
from db.mongo import users_collection, patients_collection, appointments_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
|
|
|
|
| 11 |
|
| 12 |
router = APIRouter()
|
| 13 |
|
| 14 |
+
# --- SIGNUP ---
|
| 15 |
@router.post("/signup")
|
| 16 |
async def signup(data: SignupForm):
|
| 17 |
if data.role != "patient":
|
|
|
|
| 107 |
})
|
| 108 |
return patients
|
| 109 |
|
| 110 |
+
# --- COUNT PATIENTS ---
|
| 111 |
@router.get("/patients-count")
|
| 112 |
async def count_patients(current_user: dict = Depends(get_current_user)):
|
| 113 |
if current_user.get("role") != "doctor":
|
| 114 |
raise HTTPException(status_code=403, detail="Only doctors can count patients")
|
|
|
|
| 115 |
count = await patients_collection.count_documents({"created_by": current_user["email"]})
|
| 116 |
return {"count": count}
|
| 117 |
+
|
| 118 |
+
# =========================
|
| 119 |
+
# APPOINTMENT ROUTES
|
| 120 |
+
# =========================
|
| 121 |
+
|
| 122 |
+
# --- CREATE APPOINTMENT (doctor only) ---
|
| 123 |
+
@router.post("/appointments")
|
| 124 |
+
async def create_appointment(data: AppointmentCreate, current_user: dict = Depends(get_current_user)):
|
| 125 |
+
if current_user.get("role") != "doctor":
|
| 126 |
+
raise HTTPException(status_code=403, detail="Only doctors can create appointments")
|
| 127 |
+
|
| 128 |
+
appointment_doc = {
|
| 129 |
+
"patient_id": ObjectId(data.patient_id),
|
| 130 |
+
"doctor_id": ObjectId(data.doctor_id),
|
| 131 |
+
"date": data.date,
|
| 132 |
+
"time": data.time,
|
| 133 |
+
"reason": data.reason,
|
| 134 |
+
"created_by": current_user["email"],
|
| 135 |
+
"created_at": datetime.utcnow()
|
| 136 |
+
}
|
| 137 |
+
await appointments_collection.insert_one(appointment_doc)
|
| 138 |
+
return {"message": "Appointment created successfully"}
|
| 139 |
+
|
| 140 |
+
# --- LIST DOCTOR'S APPOINTMENTS ---
|
| 141 |
+
@router.get("/appointments/doctor")
|
| 142 |
+
async def list_doctor_appointments(current_user: dict = Depends(get_current_user)):
|
| 143 |
+
if current_user.get("role") != "doctor":
|
| 144 |
+
raise HTTPException(status_code=403, detail="Only doctors can view this")
|
| 145 |
+
|
| 146 |
+
cursor = appointments_collection.find({"doctor_id": {"$exists": True}})
|
| 147 |
+
return [{**a, "_id": str(a["_id"]), "patient_id": str(a["patient_id"]), "doctor_id": str(a["doctor_id"])} async for a in cursor]
|
| 148 |
+
|
| 149 |
+
# --- LIST PATIENT'S APPOINTMENTS ---
|
| 150 |
+
@router.get("/appointments/patient")
|
| 151 |
+
async def list_my_appointments(current_user: dict = Depends(get_current_user)):
|
| 152 |
+
if current_user.get("role") != "patient":
|
| 153 |
+
raise HTTPException(status_code=403, detail="Only patients can view their appointments")
|
| 154 |
+
|
| 155 |
+
user = await users_collection.find_one({"email": current_user["email"]})
|
| 156 |
+
if not user:
|
| 157 |
+
raise HTTPException(status_code=404, detail="User not found")
|
| 158 |
+
|
| 159 |
+
patient_id = user.get("_id")
|
| 160 |
+
cursor = appointments_collection.find({"patient_id": patient_id})
|
| 161 |
+
return [{**a, "_id": str(a["_id"]), "patient_id": str(a["patient_id"]), "doctor_id": str(a["doctor_id"])} async for a in cursor]
|