88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
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_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
|
|
|
|
@validator('price')
|
|
def validate_price(cls, value):
|
|
if value <= 0:
|
|
raise ValueError('Price must be greater than zero')
|
|
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_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 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 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 |