Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Body
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import hashlib
|
| 4 |
+
import asyncpg
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# PostgreSQL connection setup
|
| 8 |
+
DB_URL = "postgresql://patient_db_xa13_user:FKIcfjuDn7HCJfOAsIV43pZsUgeSJtYn@dpg-csmbgh3qf0us73fvpjsg-a.oregon-postgres.render.com/patient_db_xa13"
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# Hash password for storage
|
| 13 |
+
def hash_password(password: str) -> str:
|
| 14 |
+
return hashlib.sha256(password.encode()).hexdigest()
|
| 15 |
+
|
| 16 |
+
# Function to connect to the PostgreSQL database using asyncpg
|
| 17 |
+
async def get_db_connection():
|
| 18 |
+
conn = await asyncpg.connect(DB_URL)
|
| 19 |
+
return conn
|
| 20 |
+
|
| 21 |
+
# Pydantic models for user data
|
| 22 |
+
class UserRegistration(BaseModel):
|
| 23 |
+
email: str
|
| 24 |
+
username: str
|
| 25 |
+
password: str
|
| 26 |
+
phone: str
|
| 27 |
+
dob: str # Date of birth in the format YYYY-MM-DD
|
| 28 |
+
|
| 29 |
+
class UserLogin(BaseModel):
|
| 30 |
+
email: str
|
| 31 |
+
password: str
|
| 32 |
+
|
| 33 |
+
@app.post("/register")
|
| 34 |
+
async def register_user(user: UserRegistration):
|
| 35 |
+
try:
|
| 36 |
+
# Parse date of birth
|
| 37 |
+
dob = datetime.strptime(user.dob, "%Y-%m-%d")
|
| 38 |
+
password_hash = hash_password(user.password)
|
| 39 |
+
|
| 40 |
+
conn = await get_db_connection()
|
| 41 |
+
existing_user = await conn.fetchrow("SELECT * FROM users WHERE email = $1", user.email)
|
| 42 |
+
|
| 43 |
+
if existing_user:
|
| 44 |
+
await conn.close()
|
| 45 |
+
raise HTTPException(status_code=400, detail="Email already registered.")
|
| 46 |
+
|
| 47 |
+
# Insert new user into the database
|
| 48 |
+
await conn.execute(
|
| 49 |
+
"INSERT INTO users (email, username, password_hash, phone, dob) VALUES ($1, $2, $3, $4, $5)",
|
| 50 |
+
user.email, user.username, password_hash, user.phone, dob
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
await conn.close()
|
| 54 |
+
return {"message": "Registration successful!"}
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
| 58 |
+
|
| 59 |
+
@app.post("/login")
|
| 60 |
+
async def login_user(user: UserLogin):
|
| 61 |
+
try:
|
| 62 |
+
conn = await get_db_connection()
|
| 63 |
+
user_data = await conn.fetchrow("SELECT * FROM users WHERE email = $1", user.email)
|
| 64 |
+
|
| 65 |
+
if user_data and user_data['password_hash'] == hash_password(user.password):
|
| 66 |
+
await conn.close()
|
| 67 |
+
return {"message": "Login successful!"}
|
| 68 |
+
else:
|
| 69 |
+
await conn.close()
|
| 70 |
+
raise HTTPException(status_code=400, detail="Invalid email or password.")
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
| 74 |
+
|
| 75 |
+
@app.post("/update_user")
|
| 76 |
+
async def update_user(user: UserRegistration):
|
| 77 |
+
try:
|
| 78 |
+
conn = await get_db_connection()
|
| 79 |
+
|
| 80 |
+
# Update user data
|
| 81 |
+
await conn.execute(
|
| 82 |
+
"UPDATE users SET username = $1, phone = $2, dob = $3 WHERE email = $4",
|
| 83 |
+
user.username, user.phone, user.dob, user.email
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
await conn.close()
|
| 87 |
+
return {"message": "User updated successfully!"}
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
| 91 |
+
|