From fad49d7a496a3fa4516540c4e97bb99228000a4a Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Fri, 28 Mar 2025 16:01:03 +0000 Subject: [PATCH] Add helper functions for Food --- helpers/food_helpers.py | 88 ++++++++--------------------------------- 1 file changed, 17 insertions(+), 71 deletions(-) diff --git a/helpers/food_helpers.py b/helpers/food_helpers.py index dc0fa3e..5696d27 100644 --- a/helpers/food_helpers.py +++ b/helpers/food_helpers.py @@ -2,87 +2,33 @@ from typing import List, Dict, Optional from pydantic import BaseModel, validator from datetime import datetime -class FoodItem(BaseModel): +class Food(BaseModel): + id: int name: str description: Optional[str] = None price: float category: str is_available: bool = True - - @validator('name') - def validate_name(cls, value): - if len(value) < 3: - raise ValueError('Name must be at least 3 characters long') - return value + created_at: datetime + updated_at: Optional[datetime] = None @validator('price') - def validate_price(cls, value): + def price_must_be_positive(cls, value): if value <= 0: - raise ValueError('Price must be greater than zero') + raise ValueError('Price must be a positive value') return value -def create_menu_item(food_item: FoodItem) -> Dict: - food_data = food_item.dict() - food_data['created_at'] = datetime.utcnow() - # Save food item to database or perform any other necessary operations - return food_data +def get_all_foods(foods: List[Food]) -> List[Dict]: + return [food.dict() for food in foods] -def get_menu_items(category: Optional[str] = None) -> List[Dict]: - # Retrieve menu items from database based on category filter - menu_items = [ - { - 'name': 'Pizza', - 'description': 'Delicious pizza with tomato sauce and cheese', - 'price': 12.99, - 'category': 'Main Course', - 'is_available': True, - 'created_at': datetime(2023, 5, 1, 12, 0, 0) - }, - { - 'name': 'Salad', - 'description': 'Fresh garden salad', - 'price': 6.99, - 'category': 'Appetizer', - 'is_available': True, - 'created_at': datetime(2023, 5, 1, 12, 0, 0) - }, - # Add more sample data as needed - ] - if category: - menu_items = [item for item in menu_items if item['category'] == category] - return menu_items +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 update_menu_item(item_name: str, updated_data: Dict) -> Dict: - # Retrieve the menu item from the database based on the item_name - menu_item = { - 'name': 'Pizza', - 'description': 'Delicious pizza with tomato sauce and cheese', - 'price': 12.99, - 'category': 'Main Course', - 'is_available': True, - 'created_at': datetime(2023, 5, 1, 12, 0, 0) - } - - # Update the menu item with the provided updated_data - for key, value in updated_data.items(): - if key in menu_item: - menu_item[key] = value - - # Save the updated menu item to the database - - return menu_item +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 delete_menu_item(item_name: str) -> bool: - # Retrieve the menu item from the database based on the item_name - menu_item = { - 'name': 'Pizza', - 'description': 'Delicious pizza with tomato sauce and cheese', - 'price': 12.99, - 'category': 'Main Course', - 'is_available': True, - 'created_at': datetime(2023, 5, 1, 12, 0, 0) - } - - # Delete the menu item from the database - - return True \ No newline at end of file +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] \ No newline at end of file