Add helper functions for FoodMenu
This commit is contained in:
parent
48c810983d
commit
5be2e33b6d
119
helpers/foodmenu_helpers.py
Normal file
119
helpers/foodmenu_helpers.py
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
from typing import List, Dict, Optional, Union, Any
|
||||||
|
from decimal import Decimal
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import or_
|
||||||
|
from models.food_menu import FoodMenu
|
||||||
|
from schemas.food_menu import FoodMenuCreate, FoodMenuUpdate
|
||||||
|
|
||||||
|
def validate_price(price: Decimal) -> bool:
|
||||||
|
"""
|
||||||
|
Validate if price is within acceptable range and format.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
price: Price to validate
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if price is valid, False otherwise
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if price <= 0 or price > 99999.99:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def get_available_menu_items(
|
||||||
|
db: Session,
|
||||||
|
category: Optional[str] = None,
|
||||||
|
price_range: Optional[tuple[Decimal, Decimal]] = None
|
||||||
|
) -> List[FoodMenu]:
|
||||||
|
"""
|
||||||
|
Get available menu items with optional filtering.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db: Database session
|
||||||
|
category: Optional category filter
|
||||||
|
price_range: Optional tuple of (min_price, max_price)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of available menu items matching criteria
|
||||||
|
"""
|
||||||
|
query = db.query(FoodMenu).filter(FoodMenu.is_available == True)
|
||||||
|
|
||||||
|
if category:
|
||||||
|
query = query.filter(FoodMenu.category == category)
|
||||||
|
|
||||||
|
if price_range:
|
||||||
|
min_price, max_price = price_range
|
||||||
|
query = query.filter(FoodMenu.price.between(min_price, max_price))
|
||||||
|
|
||||||
|
return query.all()
|
||||||
|
|
||||||
|
def search_menu_items(
|
||||||
|
db: Session,
|
||||||
|
search_term: str,
|
||||||
|
include_unavailable: bool = False
|
||||||
|
) -> List[FoodMenu]:
|
||||||
|
"""
|
||||||
|
Search menu items by name, description or ingredients.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db: Database session
|
||||||
|
search_term: Term to search for
|
||||||
|
include_unavailable: Whether to include unavailable items
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of menu items matching search criteria
|
||||||
|
"""
|
||||||
|
query = db.query(FoodMenu)
|
||||||
|
|
||||||
|
if not include_unavailable:
|
||||||
|
query = query.filter(FoodMenu.is_available == True)
|
||||||
|
|
||||||
|
return query.filter(
|
||||||
|
or_(
|
||||||
|
FoodMenu.name.ilike(f"%{search_term}%"),
|
||||||
|
FoodMenu.description.ilike(f"%{search_term}%"),
|
||||||
|
FoodMenu.ingredients.ilike(f"%{search_term}%")
|
||||||
|
)
|
||||||
|
).all()
|
||||||
|
|
||||||
|
def get_menu_items_by_allergens(
|
||||||
|
db: Session,
|
||||||
|
exclude_allergens: List[str]
|
||||||
|
) -> List[FoodMenu]:
|
||||||
|
"""
|
||||||
|
Get menu items excluding specific allergens.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db: Database session
|
||||||
|
exclude_allergens: List of allergens to exclude
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of menu items without specified allergens
|
||||||
|
"""
|
||||||
|
query = db.query(FoodMenu).filter(FoodMenu.is_available == True)
|
||||||
|
|
||||||
|
for allergen in exclude_allergens:
|
||||||
|
query = query.filter(~FoodMenu.allergens.ilike(f"%{allergen}%"))
|
||||||
|
|
||||||
|
return query.all()
|
||||||
|
|
||||||
|
def get_quick_meals(
|
||||||
|
db: Session,
|
||||||
|
max_prep_time: int = 15
|
||||||
|
) -> List[FoodMenu]:
|
||||||
|
"""
|
||||||
|
Get available meals with preparation time under specified minutes.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
db: Database session
|
||||||
|
max_prep_time: Maximum preparation time in minutes
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of quick-to-prepare menu items
|
||||||
|
"""
|
||||||
|
return db.query(FoodMenu).filter(
|
||||||
|
FoodMenu.is_available == True,
|
||||||
|
FoodMenu.preparation_time <= max_prep_time
|
||||||
|
).all()
|
Loading…
x
Reference in New Issue
Block a user