Add helper functions for Meal
This commit is contained in:
parent
df02240386
commit
4d5a3b16ab
@ -1,76 +1,107 @@
|
|||||||
from typing import List, Dict, Optional
|
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 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:
|
Args:
|
||||||
db: Database session
|
name (str): The meal name to validate.
|
||||||
|
|
||||||
Returns:
|
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:
|
Args:
|
||||||
db: Database session
|
db_session: The database session.
|
||||||
category: Category to filter by
|
|
||||||
|
|
||||||
Returns:
|
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:
|
Args:
|
||||||
db: Database session
|
db_session: The database session.
|
||||||
min_price: Minimum price
|
meal_id (int): The ID of the meal to retrieve.
|
||||||
max_price: Maximum price
|
|
||||||
|
|
||||||
Returns:
|
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:
|
Args:
|
||||||
db: Database session
|
db_session: The database session.
|
||||||
meal_id: ID of the meal
|
meal_data (MealCreate): The data for creating a new meal.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Meal object if found, None otherwise
|
Meal: The created meal object.
|
||||||
"""
|
|
||||||
return db.query(Meal).filter(Meal.id == meal_id).first()
|
|
||||||
|
|
||||||
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:
|
Args:
|
||||||
db: Database session
|
db_session: The database session.
|
||||||
meal_id: ID of the meal to update
|
meal_id (int): The ID of the meal to update.
|
||||||
meal_data: Dictionary containing updated meal data
|
meal_data (MealUpdate): The updated data for the meal.
|
||||||
|
|
||||||
Returns:
|
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)
|
meal = get_meal_by_id(db_session, meal_id)
|
||||||
if meal:
|
if not meal:
|
||||||
for key, value in meal_data.items():
|
return None
|
||||||
setattr(meal, key, value)
|
|
||||||
db.commit()
|
if not validate_meal_name(meal_data.name):
|
||||||
db.refresh(meal)
|
raise HTTPException(status_code=400, detail="Invalid meal name")
|
||||||
return meal
|
|
||||||
return None
|
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
|
Loading…
x
Reference in New Issue
Block a user