Add helper functions for Meal

This commit is contained in:
Backend IM Bot 2025-03-28 08:37:32 +00:00
parent 060c7ca0b2
commit 0bcf4903d8

76
helpers/meal_helpers.py Normal file
View File

@ -0,0 +1,76 @@
from typing import List, Dict, Optional
from sqlalchemy.orm import Session
from models.meal import Meal
def get_all_meals(db: Session) -> List[Meal]:
"""
Get a list of all available meals.
Args:
db: Database session
Returns:
List of Meal objects
"""
return db.query(Meal).all()
def filter_meals_by_category(db: Session, category: str) -> List[Meal]:
"""
Get a list of meals filtered by category.
Args:
db: Database session
category: Category to filter by
Returns:
List of Meal objects matching the category
"""
return db.query(Meal).filter(Meal.category == category).all()
def filter_meals_by_price_range(db: Session, min_price: float, max_price: float) -> List[Meal]:
"""
Get a list of meals filtered by price range.
Args:
db: Database session
min_price: Minimum price
max_price: Maximum price
Returns:
List of Meal objects within the specified price range
"""
return db.query(Meal).filter(Meal.price >= min_price, Meal.price <= max_price).all()
def get_meal_by_id(db: Session, meal_id: int) -> Optional[Meal]:
"""
Get a meal by its ID.
Args:
db: Database session
meal_id: ID of the meal
Returns:
Meal object if found, None otherwise
"""
return db.query(Meal).filter(Meal.id == meal_id).first()
def update_meal_details(db: Session, meal_id: int, meal_data: Dict) -> Optional[Meal]:
"""
Update details of a meal.
Args:
db: Database session
meal_id: ID of the meal to update
meal_data: Dictionary containing updated meal data
Returns:
Updated Meal object if successful, None otherwise
"""
meal = get_meal_by_id(db, meal_id)
if meal:
for key, value in meal_data.items():
setattr(meal, key, value)
db.commit()
db.refresh(meal)
return meal
return None