ewere / api /routine_routes.py
andevs's picture
Create routine_routes.py
b5672ec verified
Raw
History Blame Contribute Delete
5.2 kB
from fastapi import APIRouter, HTTPException, Depends
from pydantic import BaseModel
from typing import List, Optional
from datetime import datetime
from app import verify_token, db_service
router = APIRouter()
class RoutineRequest(BaseModel):
wake_time: str
sleep_time: str
skin_type: str
budget: str
sensitivity: str
conditions: Optional[List[str]] = []
class RoutineResponse(BaseModel):
id: str
morning: List[str]
night: List[str]
weekly: List[str]
products: List[dict]
tips: List[str]
@router.post("/create")
async def create_routine(
request: RoutineRequest,
user_id: str = Depends(verify_token)
):
"""
Create personalized skincare routine
"""
try:
# Generate routine based on inputs
routine = generate_routine(
wake_time=request.wake_time,
sleep_time=request.sleep_time,
skin_type=request.skin_type,
budget=request.budget,
sensitivity=request.sensitivity,
conditions=request.conditions
)
# Save to database
routine_id = f"routine_{datetime.utcnow().timestamp()}"
db_service.save_routine(
user_id=user_id,
routine_id=routine_id,
routine=routine,
preferences=request.dict()
)
return {
"id": routine_id,
**routine
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/current")
async def get_current_routine(
user_id: str = Depends(verify_token)
):
"""
Get user's current routine
"""
try:
routine = db_service.get_current_routine(user_id)
if not routine:
return None
return routine
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/history")
async def get_routine_history(
user_id: str = Depends(verify_token)
):
"""
Get user's routine history
"""
try:
history = db_service.get_routine_history(user_id)
return {"history": history}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.post("/{routine_id}/complete")
async def complete_routine_step(
routine_id: str,
step: str,
user_id: str = Depends(verify_token)
):
"""
Mark routine step as completed
"""
try:
db_service.mark_step_completed(user_id, routine_id, step)
return {"success": True}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
def generate_routine(wake_time, sleep_time, skin_type, budget, sensitivity, conditions):
"""
Generate personalized routine based on inputs
"""
morning = []
night = []
weekly = []
# Base routine for all skin types
morning = [
f"Cleanse at {wake_time}",
"Apply vitamin C serum",
"Moisturize with SPF 30+"
]
night = [
f"Double cleanse at {sleep_time}",
"Apply treatment serum",
"Use night cream"
]
weekly = [
"Exfoliate twice a week",
"Use face mask on weekends"
]
# Customize based on skin type
if skin_type == "oily":
morning[0] = "Cleanse with foaming cleanser"
morning.insert(1, "Apply salicylic acid toner")
night[1] = "Apply niacinamide serum"
elif skin_type == "dry":
morning[2] = "Apply rich moisturizer with SPF"
night[2] = "Use overnight hydrating mask"
weekly.append("Use facial oil 2-3 times a week")
elif skin_type == "sensitive":
morning = [
f"Cleanse with gentle cleanser at {wake_time}",
"Apply soothing serum",
"Use mineral SPF"
]
night[1] = "Apply calming treatment"
weekly = ["Use gentle exfoliation once a week"]
# Add condition-specific treatments
if "acne" in str(conditions):
night.insert(1, "Apply acne treatment (benzoyl peroxide or salicylic acid)")
if "hyperpigmentation" in str(conditions):
morning[1] = "Apply vitamin C serum for brightening"
night.insert(1, "Apply retinoid for pigmentation")
# Budget adjustments
if budget == "low":
products = ["Drugstore cleanser", "Basic moisturizer", "SPF"]
elif budget == "medium":
products = ["Mid-range cleanser", "Treatment serum", "Quality moisturizer"]
else:
products = ["Premium cleanser", "Advanced serum", "Luxury moisturizer"]
# Tips
tips = [
"Always patch test new products",
"Apply products from thinnest to thickest consistency",
"Wait 30 seconds between each product",
"Don't forget your neck and chest",
"Stay consistent for best results"
]
if sensitivity == "very":
tips.append("Stick to fragrance-free products")
tips.append("Introduce one new product at a time")
return {
"morning": morning,
"night": night,
"weekly": weekly,
"products": products,
"tips": tips
}