Spaces:
Sleeping
Sleeping
Update api/routes.py
Browse files- api/routes.py +25 -4
api/routes.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
from fastapi import APIRouter, HTTPException, Depends
|
| 2 |
from fastapi.security import OAuth2PasswordRequestForm
|
| 3 |
-
from models.schemas import SignupForm, TokenResponse
|
| 4 |
-
from db.mongo import users_collection
|
| 5 |
from core.security import hash_password, verify_password, create_access_token, get_current_user
|
| 6 |
-
from datetime import datetime
|
| 7 |
|
| 8 |
router = APIRouter()
|
| 9 |
|
|
|
|
| 10 |
@router.post("/signup")
|
| 11 |
async def signup(data: SignupForm):
|
| 12 |
email = data.email.lower().strip()
|
|
@@ -24,6 +25,8 @@ async def signup(data: SignupForm):
|
|
| 24 |
await users_collection.insert_one(user_doc)
|
| 25 |
return {"success": True, "message": "Account created"}
|
| 26 |
|
|
|
|
|
|
|
| 27 |
@router.post("/login", response_model=TokenResponse)
|
| 28 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
| 29 |
email = form_data.username.lower().strip()
|
|
@@ -34,10 +37,28 @@ async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
| 34 |
access_token = create_access_token(data={"sub": user["email"]})
|
| 35 |
return {"access_token": access_token, "token_type": "bearer"}
|
| 36 |
|
|
|
|
|
|
|
| 37 |
@router.get("/me")
|
| 38 |
async def get_me(current_user: dict = Depends(get_current_user)):
|
| 39 |
return {
|
| 40 |
"email": current_user["email"],
|
| 41 |
-
"full_name": current_user.get("full_name", "")
|
|
|
|
| 42 |
}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import APIRouter, HTTPException, Depends
|
| 2 |
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 |
|
| 10 |
+
# --- SIGNUP ---
|
| 11 |
@router.post("/signup")
|
| 12 |
async def signup(data: SignupForm):
|
| 13 |
email = data.email.lower().strip()
|
|
|
|
| 25 |
await users_collection.insert_one(user_doc)
|
| 26 |
return {"success": True, "message": "Account created"}
|
| 27 |
|
| 28 |
+
|
| 29 |
+
# --- LOGIN ---
|
| 30 |
@router.post("/login", response_model=TokenResponse)
|
| 31 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
| 32 |
email = form_data.username.lower().strip()
|
|
|
|
| 37 |
access_token = create_access_token(data={"sub": user["email"]})
|
| 38 |
return {"access_token": access_token, "token_type": "bearer"}
|
| 39 |
|
| 40 |
+
|
| 41 |
+
# --- GET CURRENT USER ---
|
| 42 |
@router.get("/me")
|
| 43 |
async def get_me(current_user: dict = Depends(get_current_user)):
|
| 44 |
return {
|
| 45 |
"email": current_user["email"],
|
| 46 |
+
"full_name": current_user.get("full_name", ""),
|
| 47 |
+
"created_at": current_user.get("created_at", "")
|
| 48 |
}
|
| 49 |
|
| 50 |
+
|
| 51 |
+
# --- ADD NEW PATIENT ---
|
| 52 |
+
@router.post("/patients")
|
| 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": data.date_of_birth,
|
| 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": date.today()
|
| 62 |
+
}
|
| 63 |
+
result = await patients_collection.insert_one(patient_doc)
|
| 64 |
+
return { "id": str(result.inserted_id), "message": "Patient created successfully" }
|