from typing import List, Optional from pydantic import BaseModel from datetime import datetime class FoodItem(BaseModel): id: int name: str description: Optional[str] = None price: float category: str available: bool = True created_at: datetime updated_at: Optional[datetime] = None def validate_food_item(food_item: FoodItem) -> dict: errors = {} if not food_item.name.strip(): errors["name"] = "Name is required" if food_item.price <= 0: errors["price"] = "Price must be greater than zero" if not food_item.category.strip(): errors["category"] = "Category is required" return errors def get_available_food_items(food_items: List[FoodItem]) -> List[FoodItem]: return [item for item in food_items if item.available] def filter_food_items_by_category(food_items: List[FoodItem], category: str) -> List[FoodItem]: return [item for item in food_items if item.category.lower() == category.lower()] def sort_food_items_by_price(food_items: List[FoodItem], ascending: bool = True) -> List[FoodItem]: return sorted(food_items, key=lambda x: x.price, reverse=not ascending) def get_food_menu(food_items: List[FoodItem]) -> dict: menu = {} for item in food_items: category = item.category if category not in menu: menu[category] = [] menu[category].append({ "id": item.id, "name": item.name, "description": item.description, "price": item.price, "available": item.available }) return menu