From 839d64fb2cc6866b9e9ed663eb2029250db80bca Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Fri, 28 Mar 2025 13:53:12 +0000 Subject: [PATCH] Add helper functions for Food --- helpers/food_helpers.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 helpers/food_helpers.py diff --git a/helpers/food_helpers.py b/helpers/food_helpers.py new file mode 100644 index 0000000..d5c7fca --- /dev/null +++ b/helpers/food_helpers.py @@ -0,0 +1,40 @@ +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 \ No newline at end of file