40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
from typing import List, Dict, Optional
|
|
from pydantic import BaseModel
|
|
from datetime import datetime
|
|
|
|
class FoodItem(BaseModel):
|
|
id: int
|
|
name: str
|
|
description: Optional[str] = None
|
|
price: float
|
|
category: str
|
|
is_available: bool = True
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
|
|
def validate_food_item(food_item: FoodItem) -> Dict[str, str]:
|
|
errors = {}
|
|
if not food_item.name.strip():
|
|
errors["name"] = "Name cannot be empty"
|
|
if food_item.price <= 0:
|
|
errors["price"] = "Price must be greater than zero"
|
|
if not food_item.category.strip():
|
|
errors["category"] = "Category cannot be empty"
|
|
return errors
|
|
|
|
def get_available_food_items(food_items: List[FoodItem]) -> List[FoodItem]:
|
|
return [item for item in food_items if item.is_available]
|
|
|
|
def get_food_items_by_category(food_items: List[FoodItem], category: str) -> List[FoodItem]:
|
|
return [item for item in food_items if item.category.lower() == category.lower()]
|
|
|
|
def get_food_item_by_id(food_items: List[FoodItem], item_id: int) -> Optional[FoodItem]:
|
|
for item in food_items:
|
|
if item.id == item_id:
|
|
return item
|
|
return None
|
|
|
|
def update_food_item_availability(food_item: FoodItem, is_available: bool) -> FoodItem:
|
|
food_item.is_available = is_available
|
|
food_item.updated_at = datetime.now()
|
|
return food_item |