v0.2 - 19 features: comparator, allergies, gamification, shopping list, achievements, stats, profile, share, bottom nav
This commit is contained in:
51
backend/app/routers/profile.py
Normal file
51
backend/app/routers/profile.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
import json
|
||||
from app.database import get_db
|
||||
from app.models.user import User
|
||||
from app.utils.security import get_current_user
|
||||
|
||||
router = APIRouter(prefix="/api/auth", tags=["profile"])
|
||||
|
||||
class ProfileUpdate(BaseModel):
|
||||
allergies: Optional[List[str]] = None
|
||||
health_profile: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
|
||||
VALID_PROFILES = ["normal", "crianca", "gestante", "diabetico", "hipertenso"]
|
||||
|
||||
@router.put("/profile")
|
||||
async def update_profile(data: ProfileUpdate, user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
||||
if data.allergies is not None:
|
||||
user.allergies = json.dumps(data.allergies)
|
||||
if data.health_profile is not None:
|
||||
if data.health_profile not in VALID_PROFILES:
|
||||
raise HTTPException(status_code=400, detail=f"Perfil inválido. Use: {', '.join(VALID_PROFILES)}")
|
||||
user.health_profile = data.health_profile
|
||||
if data.name is not None:
|
||||
user.name = data.name
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(user)
|
||||
|
||||
return {
|
||||
"id": user.id,
|
||||
"email": user.email,
|
||||
"name": user.name,
|
||||
"is_premium": user.is_premium,
|
||||
"allergies": json.loads(user.allergies or "[]"),
|
||||
"health_profile": user.health_profile or "normal",
|
||||
}
|
||||
|
||||
@router.get("/profile")
|
||||
async def get_profile(user: User = Depends(get_current_user)):
|
||||
return {
|
||||
"id": user.id,
|
||||
"email": user.email,
|
||||
"name": user.name,
|
||||
"is_premium": user.is_premium,
|
||||
"allergies": json.loads(user.allergies or "[]"),
|
||||
"health_profile": user.health_profile or "normal",
|
||||
}
|
||||
Reference in New Issue
Block a user