Add helper functions for Food
This commit is contained in:
parent
7ce496de56
commit
fad49d7a49
@ -2,87 +2,33 @@ from typing import List, Dict, Optional
|
|||||||
from pydantic import BaseModel, validator
|
from pydantic import BaseModel, validator
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
class FoodItem(BaseModel):
|
class Food(BaseModel):
|
||||||
|
id: int
|
||||||
name: str
|
name: str
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
price: float
|
price: float
|
||||||
category: str
|
category: str
|
||||||
is_available: bool = True
|
is_available: bool = True
|
||||||
|
created_at: datetime
|
||||||
@validator('name')
|
updated_at: Optional[datetime] = None
|
||||||
def validate_name(cls, value):
|
|
||||||
if len(value) < 3:
|
|
||||||
raise ValueError('Name must be at least 3 characters long')
|
|
||||||
return value
|
|
||||||
|
|
||||||
@validator('price')
|
@validator('price')
|
||||||
def validate_price(cls, value):
|
def price_must_be_positive(cls, value):
|
||||||
if value <= 0:
|
if value <= 0:
|
||||||
raise ValueError('Price must be greater than zero')
|
raise ValueError('Price must be a positive value')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def create_menu_item(food_item: FoodItem) -> Dict:
|
def get_all_foods(foods: List[Food]) -> List[Dict]:
|
||||||
food_data = food_item.dict()
|
return [food.dict() for food in foods]
|
||||||
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]:
|
def filter_foods_by_category(foods: List[Food], category: str) -> List[Dict]:
|
||||||
# Retrieve menu items from database based on category filter
|
filtered_foods = [food for food in foods if food.category == category]
|
||||||
menu_items = [
|
return [food.dict() for food in filtered_foods]
|
||||||
{
|
|
||||||
'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:
|
def filter_available_foods(foods: List[Food]) -> List[Dict]:
|
||||||
# Retrieve the menu item from the database based on the item_name
|
available_foods = [food for food in foods if food.is_available]
|
||||||
menu_item = {
|
return [food.dict() for food in available_foods]
|
||||||
'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
|
def sort_foods_by_price(foods: List[Food], ascending: bool = True) -> List[Dict]:
|
||||||
for key, value in updated_data.items():
|
sorted_foods = sorted(foods, key=lambda food: food.price, reverse=not ascending)
|
||||||
if key in menu_item:
|
return [food.dict() for food in sorted_foods]
|
||||||
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
|
|
Loading…
x
Reference in New Issue
Block a user