Add helper functions for Food
This commit is contained in:
parent
7e0e17401e
commit
839d64fb2c
40
helpers/food_helpers.py
Normal file
40
helpers/food_helpers.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user