from typing import List, Dict, Optional from pydantic import BaseModel, validator from datetime import datetime class Food(BaseModel): id: int name: str description: Optional[str] = None price: float category: str is_available: bool = True created_at: datetime updated_at: Optional[datetime] = None @validator('price') def price_must_be_positive(cls, value): if value <= 0: raise ValueError('Price must be a positive value') return value def get_all_foods(foods: List[Food]) -> List[Dict]: return [food.dict() for food in foods] def filter_foods_by_category(foods: List[Food], category: str) -> List[Dict]: filtered_foods = [food for food in foods if food.category == category] return [food.dict() for food in filtered_foods] def filter_available_foods(foods: List[Food]) -> List[Dict]: available_foods = [food for food in foods if food.is_available] return [food.dict() for food in available_foods] def sort_foods_by_price(foods: List[Food], ascending: bool = True) -> List[Dict]: sorted_foods = sorted(foods, key=lambda food: food.price, reverse=not ascending) return [food.dict() for food in sorted_foods]