From 4d5a3b16ab8f7c9796b317aca297c0af347ea6a0 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Fri, 28 Mar 2025 09:41:25 +0000 Subject: [PATCH] Add helper functions for Meal --- helpers/meal_helpers.py | 111 +++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 40 deletions(-) diff --git a/helpers/meal_helpers.py b/helpers/meal_helpers.py index c4c4b78..cad5fe9 100644 --- a/helpers/meal_helpers.py +++ b/helpers/meal_helpers.py @@ -1,76 +1,107 @@ from typing import List, Dict, Optional -from sqlalchemy.orm import Session +from datetime import datetime +from fastapi import HTTPException from models.meal import Meal +from schemas.meal import MealCreate, MealUpdate -def get_all_meals(db: Session) -> List[Meal]: +def validate_meal_name(name: str) -> bool: """ - Get a list of all available meals. + Validate the meal name. Args: - db: Database session + name (str): The meal name to validate. Returns: - List of Meal objects + bool: True if the meal name is valid, False otherwise. """ - return db.query(Meal).all() + return bool(name.strip()) -def filter_meals_by_category(db: Session, category: str) -> List[Meal]: +def get_available_meals(db_session) -> List[Meal]: """ - Get a list of meals filtered by category. + Get the list of available meals from the database. Args: - db: Database session - category: Category to filter by + db_session: The database session. Returns: - List of Meal objects matching the category + List[Meal]: A list of available meals. """ - return db.query(Meal).filter(Meal.category == category).all() + return db_session.query(Meal).filter(Meal.is_available == True).all() -def filter_meals_by_price_range(db: Session, min_price: float, max_price: float) -> List[Meal]: +def get_meal_by_id(db_session, meal_id: int) -> Optional[Meal]: """ - Get a list of meals filtered by price range. + Get a meal by its ID from the database. Args: - db: Database session - min_price: Minimum price - max_price: Maximum price + db_session: The database session. + meal_id (int): The ID of the meal to retrieve. Returns: - List of Meal objects within the specified price range + Optional[Meal]: The meal object if found, None otherwise. """ - return db.query(Meal).filter(Meal.price >= min_price, Meal.price <= max_price).all() + return db_session.query(Meal).filter(Meal.id == meal_id).first() -def get_meal_by_id(db: Session, meal_id: int) -> Optional[Meal]: +def create_meal(db_session, meal_data: MealCreate) -> Meal: """ - Get a meal by its ID. + Create a new meal in the database. Args: - db: Database session - meal_id: ID of the meal + db_session: The database session. + meal_data (MealCreate): The data for creating a new meal. Returns: - Meal object if found, None otherwise - """ - return db.query(Meal).filter(Meal.id == meal_id).first() + Meal: The created meal object. -def update_meal_details(db: Session, meal_id: int, meal_data: Dict) -> Optional[Meal]: + Raises: + HTTPException: If the meal name is invalid. """ - Update details of a meal. + if not validate_meal_name(meal_data.name): + raise HTTPException(status_code=400, detail="Invalid meal name") + + new_meal = Meal( + name=meal_data.name, + description=meal_data.description, + price=meal_data.price, + is_available=meal_data.is_available, + created_at=datetime.utcnow(), + updated_at=datetime.utcnow(), + ) + + db_session.add(new_meal) + db_session.commit() + db_session.refresh(new_meal) + + return new_meal + +def update_meal(db_session, meal_id: int, meal_data: MealUpdate) -> Optional[Meal]: + """ + Update an existing meal in the database. Args: - db: Database session - meal_id: ID of the meal to update - meal_data: Dictionary containing updated meal data + db_session: The database session. + meal_id (int): The ID of the meal to update. + meal_data (MealUpdate): The updated data for the meal. Returns: - Updated Meal object if successful, None otherwise + Optional[Meal]: The updated meal object if found, None otherwise. + + Raises: + HTTPException: If the meal name is invalid. """ - meal = get_meal_by_id(db, meal_id) - if meal: - for key, value in meal_data.items(): - setattr(meal, key, value) - db.commit() - db.refresh(meal) - return meal - return None \ No newline at end of file + meal = get_meal_by_id(db_session, meal_id) + if not meal: + return None + + if not validate_meal_name(meal_data.name): + raise HTTPException(status_code=400, detail="Invalid meal name") + + meal.name = meal_data.name or meal.name + meal.description = meal_data.description or meal.description + meal.price = meal_data.price or meal.price + meal.is_available = meal_data.is_available or meal.is_available + meal.updated_at = datetime.utcnow() + + db_session.commit() + db_session.refresh(meal) + + return meal \ No newline at end of file