2025-05-30 20:35:55 +00:00

78 lines
2.3 KiB
Python

"""
Calorie calculator endpoints
"""
from datetime import date
from typing import Any, Dict
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
from sqlalchemy.orm import Session
from app import models
from app.core.deps import get_current_active_user
from app.db.session import get_db
from app.utils.calories import (
ActivityLevel,
WeightGoal,
calculate_recommended_calories
)
router = APIRouter()
class CalorieCalculationRequest(BaseModel):
"""Request model for calorie calculation"""
weight_kg: float
height_cm: float
birth_date: date
gender: str
activity_level: ActivityLevel
goal: WeightGoal
@router.post("/calculate", response_model=Dict[str, Any])
def calculate_calories(
*,
calculation_in: CalorieCalculationRequest,
current_user: models.User = Depends(get_current_active_user),
) -> Any:
"""
Calculate recommended daily calories based on personal data and goals.
"""
result = calculate_recommended_calories(
weight_kg=calculation_in.weight_kg,
height_cm=calculation_in.height_cm,
birth_date=calculation_in.birth_date,
gender=calculation_in.gender,
activity_level=calculation_in.activity_level,
goal=calculation_in.goal
)
return result
@router.get("/calculate-from-profile", response_model=Dict[str, Any])
def calculate_calories_from_profile(
*,
db: Session = Depends(get_db),
activity_level: ActivityLevel,
goal: WeightGoal,
current_user: models.User = Depends(get_current_active_user),
) -> Any:
"""
Calculate recommended daily calories based on user profile data.
"""
if not current_user.weight_kg or not current_user.height_cm or not current_user.date_of_birth or not current_user.gender:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Profile data incomplete. Please update your profile with weight, height, date of birth, and gender."
)
result = calculate_recommended_calories(
weight_kg=current_user.weight_kg,
height_cm=current_user.height_cm,
birth_date=current_user.date_of_birth,
gender=current_user.gender,
activity_level=activity_level,
goal=goal
)
return result