Add helper functions for Food
This commit is contained in:
parent
e4d9a9812a
commit
3ee9ef650b
@ -1,64 +1,88 @@
|
|||||||
from typing import List, Dict, Optional
|
from typing import List, Dict, Optional
|
||||||
from pydantic import BaseModel, validator
|
from pydantic import BaseModel, validator
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
class FoodItem(BaseModel):
|
class FoodItem(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
price: float
|
price: float
|
||||||
category: str
|
category: str
|
||||||
is_vegetarian: bool = False
|
is_available: bool = True
|
||||||
is_vegan: bool = False
|
|
||||||
|
|
||||||
@validator('name')
|
@validator('name')
|
||||||
def name_must_not_be_blank(cls, value):
|
def validate_name(cls, value):
|
||||||
if not value.strip():
|
if len(value) < 3:
|
||||||
raise ValueError('Food item name cannot be blank')
|
raise ValueError('Name must be at least 3 characters long')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@validator('price')
|
@validator('price')
|
||||||
def price_must_be_positive(cls, value):
|
def validate_price(cls, value):
|
||||||
if value <= 0:
|
if value <= 0:
|
||||||
raise ValueError('Price must be a positive value')
|
raise ValueError('Price must be greater than zero')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
class Menu(BaseModel):
|
def create_menu_item(food_item: FoodItem) -> Dict:
|
||||||
items: List[FoodItem]
|
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 get_menu_items(category: Optional[str] = None) -> List[Dict]:
|
||||||
def items_must_not_be_empty(cls, value):
|
# Retrieve menu items from database based on category filter
|
||||||
if not value:
|
menu_items = [
|
||||||
raise ValueError('Menu must contain at least one food item')
|
{
|
||||||
return value
|
'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]:
|
def update_menu_item(item_name: str, updated_data: Dict) -> Dict:
|
||||||
errors = {}
|
# Retrieve the menu item from the database based on the item_name
|
||||||
if not food_item.name.strip():
|
menu_item = {
|
||||||
errors['name'] = 'Food item name cannot be blank'
|
'name': 'Pizza',
|
||||||
if food_item.price <= 0:
|
'description': 'Delicious pizza with tomato sauce and cheese',
|
||||||
errors['price'] = 'Price must be a positive value'
|
'price': 12.99,
|
||||||
return errors
|
'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]:
|
def delete_menu_item(item_name: str) -> bool:
|
||||||
errors = {}
|
# Retrieve the menu item from the database based on the item_name
|
||||||
if not menu.items:
|
menu_item = {
|
||||||
errors['items'] = 'Menu must contain at least one food item'
|
'name': 'Pizza',
|
||||||
for item in menu.items:
|
'description': 'Delicious pizza with tomato sauce and cheese',
|
||||||
item_errors = validate_food_item(item)
|
'price': 12.99,
|
||||||
if item_errors:
|
'category': 'Main Course',
|
||||||
errors[item.name] = item_errors
|
'is_available': True,
|
||||||
return errors
|
'created_at': datetime(2023, 5, 1, 12, 0, 0)
|
||||||
|
}
|
||||||
def create_menu_categories(menu: Menu) -> Dict[str, List[FoodItem]]:
|
|
||||||
categories = {}
|
# Delete the menu item from the database
|
||||||
for item in menu.items:
|
|
||||||
category = item.category
|
return True
|
||||||
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]
|
|
Loading…
x
Reference in New Issue
Block a user