| import os |
| import bcrypt |
| import certifi |
| from pymongo import MongoClient |
| from datetime import datetime |
|
|
| |
| |
| ca = certifi.where() |
|
|
| MONGO_URI = os.getenv("MONGO_URI") |
|
|
| if not MONGO_URI: |
| raise Exception("MONGO_URI not found. Please set it in Hugging Face Secrets.") |
|
|
| |
| client = MongoClient(MONGO_URI, tlsCAFile=ca) |
|
|
| db = client["fitplan_ai"] |
|
|
| |
| users_col = db["users"] |
| weights_col = db["weights"] |
| workouts_col = db["workouts"] |
| |
| def init_db(): |
| pass |
|
|
| |
| def hash_password(password): |
| return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) |
|
|
| def check_password(password, hashed): |
| return bcrypt.checkpw(password.encode('utf-8'), hashed) |
|
|
| |
| def add_user(name, age, gender, height, weight, email, password, goal): |
| |
| if users_col.find_one({"email": email}): |
| return False |
| |
| users_col.insert_one({ |
| "name": name, |
| "age": age, |
| "gender": gender, |
| "height": height, |
| "weight": weight, |
| "email": email, |
| "password": hash_password(password), |
| "goal": goal, |
| "created_at": datetime.utcnow() |
| }) |
| return True |
|
|
| def verify_user(email, password): |
| user = users_col.find_one({"email": email}) |
| if user and check_password(password, user["password"]): |
| return user |
| return None |
|
|
| def get_user_profile(email): |
| user = users_col.find_one({"email": email}) |
| if user: |
| return ( |
| user.get("name"), |
| user.get("age"), |
| user.get("gender"), |
| user.get("height"), |
| user.get("weight"), |
| user.get("goal") |
| ) |
| return None |
|
|
| def update_profile(name, age, gender, height,weight, goal, email): |
| users_col.update_one( |
| {"email": email}, |
| {"$set": { |
| "name": name, |
| "age": age, |
| "gender": gender, |
| "height": height, |
| "weight": weight, |
| "goal": goal |
| }} |
| ) |
|
|
| |
| def save_weight(email, weight, date): |
| |
| weights_col.insert_one({ |
| "email": email, |
| "weight": weight, |
| "date": date |
| }) |
|
|
| def get_weights(email): |
| data = list(weights_col.find({"email": email}).sort("date", 1)) |
| if not data: |
| return None |
| return { |
| "date": [d["date"] for d in data], |
| "weight": [d["weight"] for d in data] |
| } |
|
|
| |
| def save_workout(email, goal, plan): |
| workouts_col.insert_one({ |
| "email": email, |
| "goal": goal, |
| "plan": plan, |
| "created_at": datetime.utcnow() |
| }) |