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 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
|
||||
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]
|
Loading…
x
Reference in New Issue
Block a user