Add helper functions for Food

This commit is contained in:
Backend IM Bot 2025-03-28 15:59:16 +00:00
parent e4d9a9812a
commit 3ee9ef650b

View File

@ -1,64 +1,88 @@
from typing import List, Dict, Optional
from pydantic import BaseModel, validator
from datetime import datetime
class FoodItem(BaseModel):
name: str
description: Optional[str] = None
price: float
category: str
is_vegetarian: bool = False
is_vegan: bool = False
is_available: bool = True
@validator('name')
def name_must_not_be_blank(cls, value):
if not value.strip():
raise ValueError('Food item name cannot be blank')
def validate_name(cls, value):
if len(value) < 3:
raise ValueError('Name must be at least 3 characters long')
return value
@validator('price')
def price_must_be_positive(cls, value):
def validate_price(cls, value):
if value <= 0:
raise ValueError('Price must be a positive value')
raise ValueError('Price must be greater than zero')
return value
class Menu(BaseModel):
items: List[FoodItem]
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
@validator('items')
def items_must_not_be_empty(cls, value):
if not value:
raise ValueError('Menu must contain at least one food item')
return value
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 validate_food_item(food_item: FoodItem) -> Dict[str, str]:
errors = {}
if not food_item.name.strip():
errors['name'] = 'Food item name cannot be blank'
if food_item.price <= 0:
errors['price'] = 'Price must be a positive value'
return errors
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 validate_menu(menu: Menu) -> Dict[str, str]:
errors = {}
if not menu.items:
errors['items'] = 'Menu must contain at least one food item'
for item in menu.items:
item_errors = validate_food_item(item)
if item_errors:
errors[item.name] = item_errors
return errors
def create_menu_categories(menu: Menu) -> Dict[str, List[FoodItem]]:
categories = {}
for item in menu.items:
category = item.category
if category not in categories:
categories[category] = []
categories[category].append(item)
return categories
def get_vegetarian_menu(menu: Menu) -> List[FoodItem]:
return [item for item in menu.items if item.is_vegetarian]
def get_vegan_menu(menu: Menu) -> List[FoodItem]:
return [item for item in menu.items if item.is_vegan]
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